0

let say my df1 and df2 are as below:
df1:

      [,1] [,2] [,3]
 [1,] "n"  "11" "13"
 [2,] "a"  "18" "14"
 [3,] "b"  "13" "10"

df2:

     [,1] [,2] [,3]
[1,] "n"  "11" "13"
[2,] "a"  "ll" "kk"
[3,] "b"  "jj" "ii"

I want a dubset of df2, such that it contains only those rows of df2 who has same values as df1 in column 1.

     [,1] [,2] [,3]
[1,] "a"  "ll" "kk"
[2,] "b"  "jj" "ii"

I thought

df.2.sub <- df2[df2$CertainColumn == df1$Specific.column]

can work. but it does not. would you help me with this please?

1
  • Are those dataframes, or matrices? A reproducible example helps with knowing your exact data types Commented Aug 16, 2021 at 16:47

2 Answers 2

2

close but need to use the "%in%" operator

df.2.sub <- df2[df2$CertainColumn %in% df1$Specific.column,]

Sign up to request clarification or add additional context in comments.

Comments

1

It seems to be a matrix. Thus, the $ wouldn't work. We can use [ for extracting the first column

df2[df2[,1] %in% df1[,1],]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.