Having a data frame ( df
) in which the components of one of the columns ( df$list
) are lists with different lengths, what is the best way to apply a function on this column and save the results in a new column?
Following is what I have tried, but it is extremely slow for my data frame (10k rows, not too large). I'm looking for alternative better ways to do this task.
df$new <- apply(df, 1, FUN = function(x) myFunc(x$list))
Example:
# constructing df & DF
a <- c(rep("A", 3), rep("B", 3), rep("A",2))
b <- c(1,1,2,4,1,1,2,2)
df <- data.frame(a,b)
DF <- data.frame(c = c(1:8), d = c(8:1))
row.names(DF) <- c("A", "B", "C", "D", "E", "F", "G", "H")
# list of lists
df_red <- aggregate(list(track = 1:NROW(df)), df[,1:2], '[')
df_red$list_1 <- apply(df_red, 1, FUN = function(x) row.names(DF[(x$track),]))
# Function
searchInDF <- function(list){DF[list,]$d}
# apply function on a list of list
df_red$list_2 <- apply(df_red, 1, FUN = function(x) searchInDF(x$list_1))
Here we create such a data frame DF
and then find the length of each component of column b
. This assumes that the sapply returns a simple vector.
DF <- data.frame(a = 1:2)
DF$b <- list(list("a", "b"), list("c", "d", "e"))
DF$c <- sapply(DF$b, length)
or if the new column is itself a list:
DF$c <- lapply(DF$b, rev)
Also try these alternatives:
replace(DF, "c", sapply(DF$b, length))
replace(DF, "c", list(lapply(DF$b, rev)))
transform(DF, c = sapply(b, length))
(Of course, in the particular case of length
we could have replaced sapply(...)
with just lengths(DF$b)
.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.