我怎样才能重塑我的数据帧?

时间:2014-06-24 11:09:35

标签: r dataframe reshape

我有一个巨大的数据框,在一个简单的版本中它看起来像这样:

    trials=c("1","2","3","4","5","6","7","8","9","10")
    co =c(rep ("1",10))
    stim=c("8","9","11","2","4","7","8","1","12","16")
    ansbin=c("1","0","1","0","0","1","0","1","1","0")
    stim.1=c("11","2","11","7","4","3","9","1","4","16")
    ansbin.1=c("0","0","1","0","0","1","0","1","1","1")
    trials.1=c("1","2","3","4","5","6","7","8","9","10")
    co.1 =c(rep ("2",10))
    stim1.1=c("11","2","11","2","5","7","8","15","17","10")
    ansbin1.1=c("1","1","1","0","0","1","1","1","0","1")
    stim2.1=c("11","2","14","1","4","8","9","10","4","12")
    ansbin2.1=c("0","1","1","0","0","1","0","0","1","0")
    ID<-     data.frame(trials,co,stim,ansbin,stim.1,ansbin.1,trials.1,co.1,stim1.1,ansbin1.1,stim2.1,ansbin2.1)

    View(ID)

现在我希望以“刺激”,“刺激1”,“刺激1.1”和“刺激2.1”在同一列“刺激”的方式形成我的新数据框架,并且答案是一样的:我希望所有“ansbin”,“ansbin.1”,“ansbin1.1”和“ansbin2.1”在同一列的“答案”下。 试验和试验.1同时应该在同一列,但不同之处是“co”栏。

我试图像这样使用“重塑”:

     df<-reshape(ID, direction="long",
           idvar=c("trials", "co"),
           varying= c("stim","stim.1",    "stim1.1","stim2.1","ansbin","ansbin.1","ansbin1.1","ansbin2.1"
           v.names=c("stimulus","answer"),
           timevar="num",           

但我每次都有一些问题和警告。我认为它应该是与列名相关联的问题。

你能帮帮我吗? 先感谢您! :)

2 个答案:

答案 0 :(得分:0)

以下是我采取的方法:

library(data.table)
melt(
  rbindlist(split.default(ID, cumsum(grepl("^trials", names(ID))))), 
  measure.vars = patterns("^stim", "^ansbin"), value.name = c("stim", "ansbin"))
#     trials co variable stim ansbin
#  1:      1  1        1    8      1
#  2:      2  1        1    9      0
#  3:      3  1        1   11      1
#  4:      4  1        1    2      0
#  5:      5  1        1    4      0
# ---                               
# 36:      6  2        2    8      1
# 37:      7  2        2    9      0
# 38:      8  2        2   10      0
# 39:      9  2        2    4      1
# 40:     10  2        2   12      0

基本上,听起来你正在看两轮&#34;重塑&#34;。

  1. 堆叠&#34;试验&#34;第二组&#34; ansbin&#34;在彼此之上。我已经用rbindlist(split.default(...))部分答案了。
  2. 堆叠每一对&#34;刺激&#34;和&#34; ansbin&#34;列彼此叠加。我已经用melt(...)部分答案了。

答案 1 :(得分:0)

考虑为每个集合构建一个重新整形的数据框列表: co <​​/ em>,试验刺激答案 ,然后将它们合并在一起。但是,因为 co <​​/ em>和试验只带有两列,而后两个带有四列,所以在重新整形之前要考虑重复列:

ID$co2 <- ID$co
ID$co3 <- ID$co.1

ID$trials.2 <- ID$trials
ID$trials.3 <- ID$trials.1

df_list <- lapply(c("co", "trials", "stim", "ans"), function(s)      
  reshape(ID, direction="long",
          varying= grep(s, names(ID)),
          v.names=c(s),
          drop = grep(paste0("^", s), names(ID), invert=TRUE),
          timevar="num",
          new.row.names = 1:1000)      
)

# CHAIN MERGE
finaldf <- Reduce(function(x, y) merge(x, y,  by=c('id', 'num')), df_list)
finaldf <- with(finaldf, finaldf[order(num, id),])    # SORT DATAFRAME
rownames(finaldf) <- NULL                             # RESET ROWNAMES 

head(finaldf)
#   id num co trials stim ans
# 1  1   1  1      1    8   1
# 2  2   1  1      2    9   0
# 3  3   1  1      3   11   1
# 4  4   1  1      4    2   0
# 5  5   1  1      5    4   0
# 6  6   1  1      6    7   1