我无法正确地重塑我的数据集:我有一些IV,然后60列包含6个块的重复测量(10次)数据。因此,我想重新整形数据以创建一个因变量和一个额外的新时变量(1-10)以及一个包含块(1-6)信息的新变量。
我已经尝试过两次重塑,但我失败了。数据如下:
sbj someIV OfferCero.1 OfferCero.2 OfferCero.3 .... OfferFive.1 OfferFive.10
1 10 6 1 4 1 4
2 12 5 7 1 2 3
3 20 7 2 8 5 2
4 22 8 2 4 4 1
应该看起来像:
sbj someIV DV Timepoint Offersize
1 10 6 1 0
1 10 1 2 0
1 10 4 3 0
1 10 5 4 0
到目前为止我的代码:
First Reshape:
dl <- reshape(df, varying=list(CeroCent= c(32:41), OneCent= c(42:51), TwoCent= c(52:61), ThreeCent= c(62:71),FourCent= c(72:81), FiveCent= c(82:91) ),
v.names=c("CeroCent", "OneCent", "TwoCent", "ThreeCent", "FourCent", "FiveCent"),
direction="long",
times=1:10,
timevar="Timepoint")
Second One:
dl2 <- reshape(dl, direction="long",
varying= c(33:38),
v.names="Emotion",
times = 1:6, timevar="Offer")
一些示例数据(只有两个“块”,重复5次):
example <- data.frame(
Sub = c(1:5),
IV1 = sample(1:5),
IV2 = sample(1:5),
CeroCent.1 = sample(1:5),
CeroCent.2 = sample(1:5),
CeroCent.3 = sample(1:5),
CeroCent.4 = sample(1:5),
CeroCent.5 = sample(1:5),
FiveCent.1 = sample(1:5),
FiveCent.2 = sample(1:5),
FiveCent.3 = sample(1:5),
FiveCent.4 = sample(1:5),
FiveCent.5 = sample(1:5)
)
谢谢!
答案 0 :(得分:0)
我不是百分百肯定我理解你的问题,但我认为这正是你所寻找的。如果这不对,请告诉我。我没有在“时间点”列中重新编码因子级别;我会把它作为练习留给你
library(tidyr)
library(magrittr)
mydata <- gather(example, key = "temp", value = "DV", -Sub, -IV1,
-IV2) %>%
separate(temp, c("Timepont", "Offersize"))
提供
的输出 Sub IV1 IV2 Timepont Offersize DV
1 4 3 CeroCent 1 4
2 3 1 CeroCent 1 1
3 1 2 CeroCent 1 2
4 5 4 CeroCent 1 5
5 2 5 CeroCent 1 3
1 4 3 CeroCent 2 3