0

I am new to R and trying to create a new data frame ("B") from an existing data frame ("A") by participant ID/observation number. I have tried using the subset function and it doesn't seem to be recognizing that I want rows of data, not columns. The subset I am looking for does not necessarily follow any logic (a subset of participants consented to an additional component of the study).

What I tried:

B <- subset.data.frame(A, A$ParticipantID == "1", "7", "10", )

B <- subset(A[A$participantID == 1, 7, 10])

And just about every variation of this code I was able to think of given the examples I've seen. Thanks in advance!

David E.S.
  • 87
  • 1
  • 7
Paul
  • 83
  • 6
  • Try `A[A$ParticipantID %in% c(1, 7, 10), ]` should subset all columns of data frame A where ParticipantID is 1, 7, or 10. Assuming ParticipantID is numeric. – Peter Apr 28 '21 at 22:49

2 Answers2

0

I recommend you to try this simple code. Create separated databases for each participant ID and then, merge them into a single database:

ParticipantID1 <- A[A$ParticipantID=="1",]
ParticipantID7 <- A[A$ParticipantID=="7",]
ParticipantID10 <- A[A$ParticipantID=="10",]
B <- rbind(ParticipantID1, ParticipantID7, ParticipantID10)

This code is valid when the Participant ID is a string vector. In case of being numeric, just use the following code:

ParticipantID1 <- A[A$ParticipantID==1,]
ParticipantID7 <- A[A$ParticipantID==7,]
ParticipantID10 <- A[A$ParticipantID==10,]
B <- rbind(ParticipantID1, ParticipantID7, ParticipantID10)

Hope it works!

David E.S.
  • 87
  • 1
  • 7
0

I really like the tidy approach using piping in the dplyr package. It helps to keep the code very readable

library(dplyr)

B<-A %>%
 filter(ParticipantID %in% c(1,7,10))
Joe Erinjeri
  • 1,200
  • 1
  • 7
  • 15