通过删除字符从长到宽重组数据

时间:2018-08-14 15:50:36

标签: r

这是我的数据样本

code    group     type  outcome
  11        A      red      M*P
  11        N   orange      N*P
  11        Z      red        R
  12     AB A     blue      Z*P
  12     AN B    green      Q*P
  12     AA A     gray       AB

可以通过以下方式创建:

df <- data.frame(
    code  = c(rep(11,3), rep(12,3)),
    group = c("A", "N", "Z", "AB A", "AN B", "AA A"),
    type  = c("red", "orange", "red", "blue", "green", "gray"),
    outcome = c("M*P", "N*P", "R", "Z*P", "Q*P", "AB"),
    stringsAsFactors = FALSE
)

我想得到下表

code    group1  group2  group3  type1    type2  type3   outcome
  11         A       N       Z    red   orange     red      MNR
  12      AB A    AN B    AA A   blue    green    gray     ZQAB

我使用了以下代码,但是它不起作用。我想删除结果中的P。谢谢你的帮助。

dcast(df, formula= code +group ~ type, value.var = 'outcome')

1 个答案:

答案 0 :(得分:3)

使用data.table达到预期的输出:

library(data.table)
setDT(df)
# Clean out the Ps before hand
df[, outcome := gsub("*P", "", outcome, fixed = TRUE)]
# dcast but lets leave the outcome for later... (easier)
wdf <- dcast(df, code ~ rowid(code), value.var = c('group', 'type'))
# Now outcome maneuvering separately by code and merge
merge(wdf, df[, .(outcome = paste(outcome, collapse = "")), code])

   code group_1 group_2 group_3 type_1 type_2 type_3 outcome
1:   11       A       N       Z    red orange    red     MNR
2:   12    AB A    AN B    AA A   blue  green   gray    ZQAB