如何在数据框中有条件地将一组行传输到第一行的末尾
How to transfer a set of rows to the end of the first row with conditional in a data frame
我需要将每个参与者的所有行都放在一行中,因为我想 运行 重复测量方差分析。
我现在的数据框:
eyetracking <- data.frame(
participant = c("s001","s001","s001","s002","s002","s002","s003","s003","s003"),
trial = c(1,2,3,1,2,3,1,2,3),
picture = c("A","C","B","C","A","B","B","A","C"),
response_time = c("2315","2513","2145","1902","2562","2723","2465","2317","2156"))
participant trial picture response_time
s001 1 A 2315
s001 2 C 2513
s001 3 B 2145
s002 1 C 1902
s002 2 A 2562
s002 3 B 2723
s003 1 B 2465
s003 2 A 2317
s003 3 C 2156
我需要的样子
participant trial_1 picture_1 response_time_1 trial_2 picture_2 response_time_2 trial_3 picture_3 response_time_3
s001 1 A 2315 2 C 2513 3 B 2145
s002 1 C 1902 2 A 2562 3 B 2723
s003 1 B 2465 2 A 2317 3 C 2156
我尝试做的但失败了,因为那时我必须为每 122 名参与者手动编写此代码并再次加入他们:
s001a <- data.frame(eyetracking[1,])
s001b <- data.frame(eyetracking[2,])
s001c <- data.frame(eyetracking[3,])
colnames(s001a) <- paste(colnames(s001a),"1",sep="_")
colnames(s001a)[1] <- "participant"
colnames(s001b) <- paste(colnames(s001b),"2",sep="_")
colnames(s001b)[1] <- "participant"
colnames(s001c) <- paste(colnames(s001c),"3",sep="_")
colnames(s001c)[1] <- "participant"
library(plyr)
s001all <- join_all(list(s001a,s001b,s001c), by='participant', type='left')
这是一个快速的方法:
library(tidyr)
pivot_wider(eyetracking,
names_from = trial,
values_from = 3:4)
# A tibble: 3 × 7
participant picture_1 picture_2 picture_3 response_time_1 response_time_2 response_time_3
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 s001 A C B 2315 2513 2145
2 s002 C A B 1902 2562 2723
3 s003 B A C 2465 2317 2156
我在这里冒昧地删除了以 trial
开头的(现已过时的)列。如果您想保留它们,只需将 values_from = 3:4
替换为 values_from = 2:4
。
我需要将每个参与者的所有行都放在一行中,因为我想 运行 重复测量方差分析。
我现在的数据框:
eyetracking <- data.frame(
participant = c("s001","s001","s001","s002","s002","s002","s003","s003","s003"),
trial = c(1,2,3,1,2,3,1,2,3),
picture = c("A","C","B","C","A","B","B","A","C"),
response_time = c("2315","2513","2145","1902","2562","2723","2465","2317","2156"))
participant trial picture response_time
s001 1 A 2315
s001 2 C 2513
s001 3 B 2145
s002 1 C 1902
s002 2 A 2562
s002 3 B 2723
s003 1 B 2465
s003 2 A 2317
s003 3 C 2156
我需要的样子
participant trial_1 picture_1 response_time_1 trial_2 picture_2 response_time_2 trial_3 picture_3 response_time_3
s001 1 A 2315 2 C 2513 3 B 2145
s002 1 C 1902 2 A 2562 3 B 2723
s003 1 B 2465 2 A 2317 3 C 2156
我尝试做的但失败了,因为那时我必须为每 122 名参与者手动编写此代码并再次加入他们:
s001a <- data.frame(eyetracking[1,])
s001b <- data.frame(eyetracking[2,])
s001c <- data.frame(eyetracking[3,])
colnames(s001a) <- paste(colnames(s001a),"1",sep="_")
colnames(s001a)[1] <- "participant"
colnames(s001b) <- paste(colnames(s001b),"2",sep="_")
colnames(s001b)[1] <- "participant"
colnames(s001c) <- paste(colnames(s001c),"3",sep="_")
colnames(s001c)[1] <- "participant"
library(plyr)
s001all <- join_all(list(s001a,s001b,s001c), by='participant', type='left')
这是一个快速的方法:
library(tidyr)
pivot_wider(eyetracking,
names_from = trial,
values_from = 3:4)
# A tibble: 3 × 7
participant picture_1 picture_2 picture_3 response_time_1 response_time_2 response_time_3
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 s001 A C B 2315 2513 2145
2 s002 C A B 1902 2562 2723
3 s003 B A C 2465 2317 2156
我在这里冒昧地删除了以 trial
开头的(现已过时的)列。如果您想保留它们,只需将 values_from = 3:4
替换为 values_from = 2:4
。