简体   繁体   English

rbind中的错误(deparse.level,...):无效的列表参数:所有变量应具有相同的长度

[英]Error in rbind(deparse.level, …) : invalid list argument: all variables should have the same length

I am trying to use "rbind" to get the result but I am being shown an error "Error in rbind(deparse.level, ...) : invalid list argument: all variables should have the same length" 我试图使用“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)

It sounds like your individual data frames do not have the same width (number of columns) 听起来你的个人数据框没有相同的宽度(列数)

An easy fix is to use plyr::rbind.fill which will fill in missing columns with NA (although you might want to rethink why you're rbinding data frames of different widths). 一个简单的解决方法是使用plyr::rbind.fill ,它将用NA填充缺少的列(尽管您可能想重新考虑为什么要对不同宽度的数据帧进行链接)。 See the reproducible example below 请参阅下面的可重现示例

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

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

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