简体   繁体   中英

Error: dim(X) must have a positive length when using “lapply”

When running this command:

tmp <- lapply(tmp, function(y) apply(sapply(y, function(x) unique(grpproc[,2]) %in% x), 1, sum))

I get the error:

Error in apply(sapply(y, function(x) unique(grpproc[, 2]) %in% x), 1,  :     
dim(X) must have a positive length

I believe I possibly need a different apply statement there maybe? Thank you for the help!

sapply() returns a vector, but apply() is looking for a matrix

xx <- c(0,1,2,3,4,5)
dim(sapply(xx, mean)) # NULL
xxx <- matrix(rep(xx,3), nrow = 3) # what happens if we give sapply a matrix?
dim(sapply(xxx, mean)) # why?
sapply(xxx, mean) # because it returns a vector

Possibly your colleague may want to use sum() instead of apply()? Or a different apply function that returns the correct matrix or data.frame.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM