[R] Surprising message "Error in FUN(newX[, i], ...) : all arguments must have the same length"

Chris Evans chrishold at psyctc.org
Tue Sep 26 08:53:07 CEST 2017


I am hitting an odd message "Error in FUN(newX[, i], ...) : all arguments must have the same length".  I can't supply the data as it's a huge data frame but I think this has enough diagnostic information to show the issue.  I am sure I am missing something obvious.  I've put some extra comments in but otherwise this is cut and pasted from Rstudio.

### I wanted a table of the values in four columns:
> apply(datTAF[,75:78],2,table,na.rm=TRUE)
Error in FUN(newX[, i], ...) : all arguments must have the same length

### odd, surely as it's a data frame all x going into table() should be same length. Check:
> apply(datTAF[,75:78],2,length)
 RiskSuicide RiskSelfHarm  RiskHarmOth    RiskLegal 
        3009         3009         3009         3009 

### I has reasons to think the tables should all be the same length.  Check:
> apply(datTAF[,75:78],2,function(x){length(table(x))})
 RiskSuicide RiskSelfHarm  RiskHarmOth    RiskLegal 
           6            6            6            6 

### Now I'm a bit baffled.  Try str:
> apply(datTAF[,75:78],2,str)
 Named chr [1:3009] "." "." "." "0" "0" "0" "0" "0" "0" "3" "0" "0" "0" "." "0" "0" "0" "0" "0" "." ...
 - attr(*, "names")= chr [1:3009] "1" "2" "3" "4" ...
 Named chr [1:3009] "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "." "0" "0" "0" "0" "0" "." ...
 - attr(*, "names")= chr [1:3009] "1" "2" "3" "4" ...
 Named chr [1:3009] "0" "0" "0" "0" "0" "0" "0" "0" "0" "3" "0" "0" "0" "." "0" "0" "0" "0" "0" "." ...
 - attr(*, "names")= chr [1:3009] "1" "2" "3" "4" ...
 Named chr [1:3009] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "." "0" "0" "0" "0" "0" "." ...
 - attr(*, "names")= chr [1:3009] "1" "2" "3" "4" ...
NULL

### Not sure where that trailing "NULL" came from:
> str(datTAF[,78])
 chr [1:3009] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "." "0" "0" "0" "0" "0" "." "0" ...

> Sys.info()
       sysname        release        version       nodename        machine          login 
     "Windows"     ">= 8 x64"   "build 9200"      "HPLP166"       "x86-64"  "Chris.Evans" 
          user effective_user 
 "Chris.Evans"  "Chris.Evans" 

Anyone see what I'm missing?  TIA,

Chris



More information about the R-help mailing list