在R中排列数据

马迪

我有一个看起来像的数据集,

 Id       value
 abc      one
 abc      two
 abc      three
 abc      four
 abc      five
 def      two
 def      three
 def      three
 def      four
 def      seven
 def      eight
 def      ten
 def      eleven

我正在尝试获得以下矩阵,

id  value1   value2  value3 value4  value5  value6 value7 value8 value9 value10 value11  value12 value13
abc one      two      three   four   five     0     0      0       0      0         0     0         0
def two      three     four   seven   eight  ten    eleven 0       0      0         0     0         0       

我尝试使用dcast,但它没有填充矩阵中的值,

    result <- dcast(data,id~value,value.var = "value",fill = "value")

有其他选择吗?

蓝宝

试试这个:

x = read.table(text = "
Id       value
abc      one
abc      two
abc      three
abc      four
abc      five
def      two
def      three
def      three
def      four
def      seven
def      eight
def      ten
def      eleven", header = T)

library(dplyr)
library(tidyr)

# spreading the values
x = x[!duplicated(x), ] # remove duplicate
y = x %>% 
  group_by(Id) %>%
  mutate(row.code = 1:n()) %>%
  spread(row.code, value)

# adding the extra NA columns and names
y = cbind(y, matrix(rep(NA, nrow(y) * (nrow(x) - ncol(y))), nrow = nrow(y)))
names(y)[-1] = paste0("value", 1:(nrow(x) - 1))

希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章