Saving rows of a data frame based on the values of a column of another data frame

Asked

Viewed 119 times

2

I have a dataframe with 50 rows and 10 columns and another with 10 rows and 5 columns, with the first column of both dataframes can have equal values. How do I select and save the rows, only, from the first data frame following the condition that should be equal to the rows (from the first column) of the second data frame?

1 answer

3


created two dataframe to exemplify

A <- data.frame(X = sample(LETTERS, 50, rep = T), Y = rnorm(50), Z = rpois(50, 3))
B <- data.frame(X1 = sample(LETTERS, 10, rep = T), Y1 = rnorm(10))

for you to pick up the lines from the first column of A which have equal values in the rows in the first column of B, you can use the command below. I saved in a dataframe C

C <- A[which(A$X %in% B$X1),]
  • Thanks! It worked fine! Thank you, Rafael

Browser other questions tagged

You are not signed in. Login or sign up in order to post.