vanao veneri vanao veneri - 11 months ago 62
R Question

apply to data.frame with list column

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?

#reproducible code
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"]))

Answer Source

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 vector

mapply(function(x,y) sum(x <y), exdf$dv, exdf$edge)

NOTE: Map is just a wrapper for mapply which always returns a list output.

Or another option is unnest (from tidyr) with dplyr functions

unnest(exdf, .id = "grp") %>%
        group_by(grp) %>%
        summarise(n = sum(dv < edge))