Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

r - Creating list per participant ID with 3 dataframes for each participant in RStudio

I'm super new to R and RStudio, so I'm sorry if this is a silly question!

I have an excel file with 3 columns: PID (participant ID), Cond, Time and Correct. Each PID has 150 trials (so 150 different reaction times to 3 different conditions, and their responses are either correct or incorrect) (see image of Excel file).

I want to import these into RStudio, but organise the data so that each PID is a single list. Within each PID list, I want 3 dataframes which include the respective condition, time and correct response for that specific PID.

Here is what I want the data to LOOK like in RStudio (see the image of example data in R).

Does anyone know how I can rearrange the data to look like this after I have read in the .csv file into R Studio?

Any help would be much appreciated!

Picture of what I WANT the data to look like once it's organised

enter image description here

Picture of how my data is organised as a .csv file

enter image description here

question from:https://stackoverflow.com/questions/65948388/creating-list-per-participant-id-with-3-dataframes-for-each-participant-in-rstud

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try to use split and lapply to get data in your required form.

df <- data.frame(PID = rep(1:4, each = 3), Cond = sample(3, 12, replace = TRUE),
                 Time = rnorm(12), Correct = sample(2, 12, replace = TRUE))

result <- lapply(split(df, df$PID), function(x) as.list(x[-1]))
View(result)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...