I have a function that takes a column p
of a data.frame dat
and sums the similar values:
response <- function(dat, p){
y = dat[p]
sums = table(y)
sums_df = as.data.frame(sums)
#sums_df$rfreq = (sums_df$Freq/sum(sums_df$Freq))*100
#sums_df$rfreq = round(sums_df$rfreq, digits = 0)
#sums_df = sums_df[1:4, c(1,3)]
return(sums_df)
}
This works fine when run individually.
> response(SOM_C, "f76a")
f76a Freq
1 1 5
2 2 51
3 3 14
4 4 6
5 5 29
6 97 1
7 99 7
But when I try to run it with through a list of separate data.frames (which include 'SOM_C')
plist <- c(SOM_V, SOM_SD, SOM_C, SOM_L, SOM_M, SOM_KD, SOM_MP, SOM_SvD)
It throws the following error.
> output <- lapply(plist, response, p = "f76a")
Error in `vec_slice()`:
! Can't use character names to index an unnamed vector.
Run `rlang::last_error()` to see where the error occurred.
CodePudding user response:
You should not collect several data frames using c()
. You should use list()
. So the following fix should work:
plist <- list(SOM_V, SOM_SD, SOM_C, SOM_L, SOM_M, SOM_KD, SOM_MP, SOM_SvD)
output <- lapply(plist, response, p = "f76a")
Compare the differences:
dat <- data.frame(a = 1:2, b = c("x", "y"))
## becomes a list of vectors
c(dat, dat)
#$a
#[1] 1 2
#
#$b
#[1] "x" "y"
#
#$a
#[1] 1 2
#
#$b
#[1] "x" "y"
## correct: a list of two data frames
list(dat, dat)