1

(仍然)是 r 的新手,对于如何完成数据的多次融合非常困惑。这是一个子集:

df <- structure(list(Subject = c(101L, 101L, 101L, 102L, 102L, 102L
), Condition = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("apass", 
"vpas"), class = "factor"), FreqCode = structure(c(1L, 1L, 1L, 
2L, 2L, 2L), .Label = c("LessVerbal", "MoreVerbal"), class = "factor"), 
Item = c(1L, 4L, 7L, 1L, 4L, 7L), Len = c(80L, 68L, 85L, 
68L, 85L, 79L), R1_1.RT = c(237L, 203L, 207L, 336L, 487L, 
340L), R1_2.RT = c(177L, 225L, 162L, 634L, 590L, 347L), R1_3.RT = c(200L, 
226L, 212L, 707L, 653L, 379L), R1.RT = c(614L, 654L, 581L, 
1677L, 1730L, 1066L), R1_1 = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = "The", class = "factor"), R1_2 = structure(c(3L, 
1L, 2L, 1L, 2L, 4L), .Label = c("antique", "course", "new", 
"road"), class = "factor"), R1_3 = structure(c(4L, 1L, 2L, 
1L, 2L, 3L), .Label = c("car", "materials", "surfaces", "technology"
), class = "factor"), R1 = structure(c(3L, 1L, 2L, 1L, 2L, 
4L), .Label = c("The antique car", "The course materials", 
"The new technology", "The road surfaces"), class = "factor")), .Names = c("Subject", 
"Condition", "FreqCode", "Item", "Len", "R1_1.RT", "R1_2.RT", 
"R1_3.RT", "R1.RT", "R1_1", "R1_2", "R1_3", "R1"), class = "data.frame", row.names =  
c(NA, 
-6L))

我的目标是获得(部分)如下所示的输出:

Region RT WordRegion Word
R1_1.RT 237 R1_1 the
...
R1_2.RT 177 R1_2 new
...

编辑:以“.RT”结尾的变量(例如,R1_1.RT)是区域名称,将被融合到区域列中。以数字结尾的变量(例如,R1_1)与区域名称及其相关值完全对应。我希望它们与区域名称一起融合,以便我可以根据区域列分析它们

在代码的第一部分,我将所有值融合到一个 Region 列中,并将值更改为 RT。这似乎工作正常:

#long transform (with individual regions at end)
SmallMelt1 = melt(df, measure.vars = c("R1_1.RT", "R1_2.RT", "R1_3.RT", "R1.RT"), var = "Region")
#change newly created column name to "RT" (note:you have to change the number in [] to match your data)
colnames(SmallMelt1)[11 ] <- "RT"

但我不知道如何同时融化另一个变量跨度,使它们与第一个跨度垂直排列。我想做这样的事情,在第一次融化之后,但它不起作用:

#Second Melt for region names (doesn't work)
SmallMelt2 = melt(SmallMelt1, measure.vars = c("R1_1", "R1_2", "R1_3", "R1"), var = "WordRegion")

#Change name to Word
colnames(SmallMelt2)[9] <- "Word" #add col number for "value" here

如果您需要任何澄清,请告诉我。我希望有人可以提供帮助...在此先感谢-DT

4

1 回答 1

1

所以,在咨询了名单外的人之后,我找到了解决方案。我的错误是我试图在第一步的输出上运行第二步。通过在原始数据上独立运行这两个步骤然后连接,我得到了正确的结果。

SmallMelt1 = melt(df, measure.vars = c("R1_1.RT", "R1_2.RT", "R1_3.RT", "R1.RT"), var =   "Region")
SmallMelt2 = melt(df, measure.vars = c("R1_1", "R1_2", "R1_3", "R1"), var = "WordRegion")
SmallMelt3=cbind(SmallMelt1,SmallMelt2[,11])
于 2012-10-10T21:04:44.847 回答