简体   繁体   English

错误:使用“lapply”时,dim(X)必须具有正长度

[英]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 sapply()返回一个向量,但apply()正在寻找一个矩阵

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()? 可能你的同事可能想使用sum()而不是apply()? Or a different apply function that returns the correct matrix or data.frame. 或者是另一个返回正确矩阵或data.frame的apply函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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