library(dplyr)
library(tibble)
library(tidyr)
df1 %>%
rownames_to_column %>%
transmute(mycols = gsub('^.*\\.', '', gsub('.[[:digit:]]+', '', rowname)),
myrows = regmatches(rowname, gregexpr('[0-9]+',rowname)),
value = Value) %>%
spread(key=mycols, value=value)
# myrows K W Z
# 1 20 30 10
# 2 1 5 30 20
# 3 2 23 44 3
library(dplyr)
library(tidyr)
df1 %>%
mutate(mycols = substr(gsub('.[[:digit:]]+', '', rownames(.)), 5, 5),
myrows = as.integer(as.factor(substr(rownames(.),7,7)))-1) %>%
spread(key=mycols, value=Value)
#> myrows K W Z
#> 1 0 20 30 10
#> 2 1 5 30 20
#> 3 2 23 44 3
数据:
df1 <- structure(list(Value = c(10, 20, 30, 20, 5, 30, 3, 23, 44)),
row.names = c("X.Y.Z", "X.Y.K", "X.Y.W", "X.Y.Z.1",
"X.Y.K.1", "X.Y.W.1", "X.Y.Z.2", "X.Y.K.2", "X.Y.W.2"),
class = "data.frame")
正如我在评论中所说,由于$..1
列导致的问题,我们需要清除数据dplyr
。这是使用问题中提供的确切数据的解决方案:
df1 <- structure(list(..1 = c("X.Y.Z", "X.Y.K", "X.Y.W", "X.Y.Z.1",
"X.Y.K.1", "X.Y.W.1", "X.Y.Z.2", "X.Y.K.2", "X.Y.W.2"),
Value = c(10, 20, 30, 20, 5, 30, 3, 23, 44)),
class = "data.frame", row.names = c(NA, -9L))
library(dplyr)
library(janitor)
library(tidyr)
clean_names(df1) %>%
mutate(mycols = substr(gsub('.[[:digit:]]+', '', x1), 5, 5),
myrows = as.integer(as.factor(substr(x1,7,7)))-1) %>%
select(-x1) %>%
spread(key=mycols, value=value)
#> myrows K W Z
#> 1 0 20 30 10
#> 2 1 5 30 20
#> 3 2 23 44 3
由reprex软件包(v0.3.0)创建于2019-07-29
结合其他方法,看看它们是否适用于OP的数据集。(没有可复制的示例,即使不是不可能,也很难解决;因此,这是我最后的努力)。
library(dplyr)
library(tibble)
library(tidyr)
df1 %>%
rownames_to_column %>%
mutate(mycols = gsub('.[[:digit:]]+', '', rowname),
myrows = regmatches(rowname, gregexpr('[0-9]+',rowname))) %>%
select(-rowname) %>%
spread(key=mycols, value=Value)
要么
df1 %>%
rownames_to_column %>%
separate(rowname,sep = "\\.", into = c("A1","B2","C3", "D4")) %>%
select(-A1,-B2) %>%
spread(key=C3, value=Value)