gpt4 book ai didi

r - rbind (deparse.level, ...) 中的错误 : invalid list argument: all variables should have the same length

转载 作者:行者123 更新时间:2023-12-04 10:26:37 26 4
gpt4 key购买 nike

我正在尝试使用“rbind”来获取结果,但显示错误
“rbind(deparse.level, ...) 中的错误:
无效的列表参数:所有变量都应该具有相同的长度”

pm2 <- function(directory, id=1:322)
{
files <- list.files(path = directory, full.names = TRUE)
df <- data.frame()
for (i in 1:322)
{
fil <- read.csv(files[i])
df <- rbind(df, fil)
}
df2 <- data.frame(matrix(nrow = length(id), ncol = 2))
colnames(df2) <- c("id", "nobs")
for(i in id){

rbind(df2, c(df[[id[i]]],count((df[[id[i]]])),na.rm = TRUE))

}
df2
}
pm2("specdata", 1:10)

最佳答案

听起来您的各个数据框的宽度(列数)不同

一个简单的解决方法是使用 plyr::rbind.fill这将用 NA 填充缺失的列(尽管您可能想重新考虑为什么要绑定(bind)不同宽度的数据帧)。请参阅下面的可重现示例

test <- mtcars[1:2,]
ncol(test)
# [1] 11

modified <- test[,1:9]
ncol(modified)
# [1] 9

rbind(test, modified)
# Error in rbind(deparse.level, ...) :
# numbers of columns of arguments do not match

library(plyr)
rbind.fill(test, modified)
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1 21 6 160 110 3.9 2.620 16.46 0 1 4 4
# 2 21 6 160 110 3.9 2.875 17.02 0 1 4 4
# 3 21 6 160 110 3.9 2.620 16.46 0 1 NA NA
# 4 21 6 160 110 3.9 2.875 17.02 0 1 NA NA

关于r - rbind (deparse.level, ...) 中的错误 : invalid list argument: all variables should have the same length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46263982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com