Reputation: 3
In R I would like to search each cell in a column, with the column containing a series of lists. I would like to return the index (or return T/F) for the cells that contain a certain value.
For example - I created this test dataframe.
test <- data.frame(rows = 1:5, values = 0)
test$values <- list(1, c(2,3), c(4:6), 4, 0)
Attempting to query it using:
test[4 %in% test$values,]
In this example I would like to return the cells that contain the value 4 (so row 3 and 4 should be true). How do I do this? My current query returns just one TRUE as it is just testing the entire column (not each cell in that column).
I have in the past solved this using a for loop (roughly as below). eg:
test$result <- FALSE
for (i in 1:nrow(test)){
if (4 %in% test$values[i]){
test$result[i] <- TRUE
}
}
I don't really want this to be my solution each time I need to query like this.
Upvotes: 0
Views: 387
Reputation: 388972
As it is a list you need to loop over it using sapply
/lapply
test$result <- sapply(test$values, function(x) 4 %in% x)
test
# rows values result
#1 1 1 FALSE
#2 2 2, 3 FALSE
#3 3 4, 5, 6 TRUE
#4 4 4 TRUE
#5 5 0 FALSE
If you want to subset those rows you could do
test[sapply(test$values, function(x) 4 %in% x), ]
# rows values
#3 3 4, 5, 6
#4 4 4
Upvotes: 1
Reputation: 60070
The map
functions in the purrr
package are good for working with nested data like this:
purrr::map_lgl(test$values, ~ 4 %in% .)
[1] FALSE FALSE TRUE TRUE FALSE
Upvotes: 0