How to subset data in data.frame X based on values from data.frame Y

Hi

I'm trying to subset rows of the column "LNUid" in "My dataset" that are equal to the columns of the second dataset of the same column. Any idea how to solve that? So, the result should be a dataset where I have only values 3, 4 and 5 as well as the the other information connected to that (Name, score)

#My dataset
data.frame(
stringsAsFactors = FALSE,
LNUid = c("value1","value2","value3",
"value4","value5","value6","value7","value8","value9",
"value10"),
Name = c("Ron","Jon","Mon","Kon",
"Fon","Bon","Son","Aon","Con","Eon"),
Score = c(5, 5, 3, 2, 4, 5, 2, 3, 4, 2)
)

#Second dataset
data.frame(
stringsAsFactors = FALSE,
LNUid = c("value3", "value4", "value5")
)

Thank you!

You need to do inner join.

df_1 <- data.frame(
stringsAsFactors = FALSE,
LNUid = c("value1","value2","value3",
"value4","value5","value6","value7","value8","value9",
"value10"),
Name = c("Ron","Jon","Mon","Kon",
"Fon","Bon","Son","Aon","Con","Eon"),
Score = c(5, 5, 3, 2, 4, 5, 2, 3, 4, 2)
)

df_2 <- data.frame(
stringsAsFactors = FALSE,
LNUid = c("value3", "value4", "value5")
)

result <- merge(df_1, df_2, by="LNUid", all=FALSE)

result

Hope this helps.

1 Like

Perfect, thank you!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.