0

I am trying to subset a large data frame based on matching IDs from another data frame. I read the answer to this question posted:

R: subset a data frame based on conditions from another data frame

However, my data differs from this in that for one of my data sets I have the linker ID set up in a column, and in the other, it goes across rows

Data set 1

*ID* time stat
*1* 3.21 6
*4* 7.9 3
...

Data set 2

*ID 1 2 4* ...
y yellow brown yellow
z 3 5 2
a yes yes no

What I ultimately would like to get is a data frame where I only have the data from data set 2 that has a matching sample ID in data set 1. So in this example:

*ID 1 4* ...
y yellow yellow
z 3 2
a yes no

Any help is much appreciated.

Andrea

1 Answer 1

0

all you need to do is

newDf<-df2[,df2[1,]%in%df1[,1]]

where df1[,1] is the data set with the id column in column position 1, and df2[1,] is the dataset with the id column in row position 1.

df2[1,]
#[1] 1 3 4

df1[,1]
#[1] 1 4

df2[1,]%in%df1[,1]
#[1] TRUE FALSE TRUE
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.