4

I was trying to remove all columns with a zero variance from my data, using this command

file <- file[,sapply(file, function(v) var(v, na.rm=TRUE)!=0)]

This command was working perfectly for my previous datasets, now I am trying to use on a new dataset and it gives me the following error:

Error in `[.data.frame`(file, , sapply(file, function(v) var(v, na.rm = TRUE) !=  : 
undefined columns selected
In addition: Warning message:
In var(v, na.rm = TRUE) : NAs introduced by coercion

The problem is I did not select any columns, I just applied the function to all columns! How come I get an error telling me undefined columns selected! Any idea what could have gone wrong??

The data looks exactly this way

    col1   col2   col3   col4
1   FIA    3.5     2.4    NA
2   DWF    2.1     NA     3.7
3   LIK    0.25    2.3    1.38
4   JUW    2.1     4.0    3.2
4

1 回答 1

10

输入文件是一个 CSV 文件并通过 read.csv 命令读取,它在导致此问题的表末尾有一个额外的空列,通过此命令删除最后一列,解决了问题。

lastcol <- ncol(file)
file[,lastcol] <- NULL
于 2013-06-19T11:01:25.897 回答