在 table 中合并不同行的值

combine different row's values in table in r

我需要在 R 中重新格式化 table。

我有一个 table 这样的。

ID  category   
1   a   
1   b   
2   c   
3   d   
4   a   
4   c  
5   a   

我想改成

ID  category1   category2  
1           a           b  
2           c        null  
3           d        null  
4           a           c  
5           a        null  

这在 R 中可行吗?

这是一个非常简单的 "long to wide" 类型的整形问题,但您需要一个辅助 "id"(或 "time")变量。

您可以尝试使用我的 "splitstackshape" 包中的 getanID 并使用 dcast 将形状从长改成宽。 getanID 将创建一个名为“.id”的新列,用作您的 "time" 变量:

library(splitstackshape)
dcast.data.table(getanID(mydf, "ID"), ID ~ .id, value.var = "category")
#    ID 1  2
# 1:  1 a  b
# 2:  2 c NA
# 3:  3 d NA
# 4:  4 a  c
# 5:  5 a NA

与 Ananda 的相同,但使用 dplyrtidyr:

library(tidyr)
library(dplyr)
mydf %>% group_by(ID) %>%
    mutate(cat_row = paste0("category", 1:n())) %>%
    spread(key = cat_row, value = category)

# Source: local data frame [5 x 3]
# 
#   ID category1  category2
# 1  1         a          b
# 2  2         c         NA
# 3  3         d         NA
# 4  4         a          c
# 5  5         a         NA