根据其他列在R中创建多个列

时间:2016-04-14 12:46:54

标签: r data-manipulation

我在数据框中有2列,请参阅下面的

no  value
1   A_0.9
1   B_0.8
1   C_0.7
1   D_0.7
2   B_0.9
2   D_0.8
2   A_0.7
2   C_0.7

我想创建新数据框,如下所示

no  value1  value2  value3  value4
1   A_0.9   B_0.8   C_0.7   D_0.7
2   B_0.9   D_0.8   A_0.7   C_0.7

即:对于“no”列中的每个唯一值,将使用“value”列中的数据创建多个列

3 个答案:

答案 0 :(得分:2)

t(unstack(df, value ~ no))
#   [,1]    [,2]    [,3]    [,4]   
#X1 "A_0.9" "B_0.8" "C_0.7" "D_0.7"
#X2 "B_0.9" "D_0.8" "A_0.7" "C_0.7"

整理上述输出以适合您的数据,

 library(dplyr)
 df1 <- as.data.frame(t(unstack(df, value ~ no)))
 names(df1)[-1] <- paste0('value', 2:ncol(df1)-1)
 rownames(df1) <- NULL
 df1 <- add_rownames(df1, 'no')  #from dplyr package
 #    no value1 value2 value3 value4
 #  (chr) (fctr) (fctr) (fctr) (fctr)
 #1     1  A_0.9  B_0.8  C_0.7  D_0.7
 #2     2  B_0.9  D_0.8  A_0.7  C_0.7

答案 1 :(得分:1)

使用value,我们可以no创建一个序列rleid() dcast(),然后用library(data.table) dcast(setDT(df)[, nr := rleid(value),by = no], no ~ nr) # no 1 2 3 4 #1 1 A_0.9 B_0.8 C_0.7 D_0.7 #2 2 B_0.9 D_0.8 A_0.7 C_0.7 数据格式化为宽格式

data.table

dcast(setDT(df), no ~ rowid(no, prefix = 'value')) # no value1 value2 value3 value4 #1: 1 A_0.9 B_0.8 C_0.7 D_0.7 #2: 2 B_0.9 D_0.8 A_0.7 C_0.7 的{​​{3}},以下是可能的,感谢@Arun!

{{1}}

答案 2 :(得分:0)

我会使用reshape库,它包含一组很好的数据操作函数。完成任务的示例:

n = c(1,1,1,1,2,2,2,2)
x = c('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D')
# Just to create the column names you showed in the example
columns = rep(paste("value", 1:4, sep=""), 2)
data = data.frame(n, columns, x)
cast(data, n~columns)