Select data from df according to data from a list

Asked

Viewed 89 times

4

Dear(as)
as given below:

nr <- c(100:199)
dt <- rep("data",100)
x <- data.frame(nr,dt)
y <- c(110:115)

I need to get,
the common numbers between the first column of the date.frame x and the list y

match <- x[x[1] == (lapply(x[1], function(z) which(y %in% z))),]
print(match)

but I’m wrong:

"Warning message:
  In FUN(left, right) :
  longer object length is not a multiple of shorter object length"

The expected result should be like this:

110 "data"
111 "data"
112 "data"
113 "data"
114 "data"
115 "data"

1 answer

4

follow code that returns the result you expect

match <- x[which(x$nr %in% y), ]
  • thanks. It worked, I’ll use it in my context.

Browser other questions tagged

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