I would like to use apply with a data.frame that contains lists in one column. That is, one column contains vectors and I wand to perform various operations on these vectors. For example, I would like to find out how many elements there are with a certain value.
What's my mistake here?
some_list <- replicate(40, sample(c(1:8), size=sample(3:6, 1), replace=TRUE))
exdf <- expand.grid(id=c(1:10), content=c(1:4))
exdf$dv <- some_list
exdf$edge <- replicate(40, sample(3:5, size=1))
#Prolem: get elements with lesser value than "edge" in "dv"
result <- sapply(dv~., data=exdf, function(x) which(x[, "dv"] < x[, "edge"]))
We can use
Map to do the comparison between corresponding elements in the columns
Map(function(x,y) which(x <y), exdf$dv, exdf$edge)
If we are only interested in the number of elements, use the
sum and with
mapply it returns a
mapply(function(x,y) sum(x <y), exdf$dv, exdf$edge)
Map is just a wrapper for
mapply which always returns a
Or another option is
library(dplyr) library(tidyr) unnest(exdf, .id = "grp") %>% group_by(grp) %>% summarise(n = sum(dv < edge))