Error404
Error404

Reputation: 7121

Troubleshooting "undefined columns selected" in R

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

Upvotes: 4

Views: 14200

Answers (1)

Error404
Error404

Reputation: 7121

The input file was a CSV file and read via the read.csv command, it had an extra empty column at the end of the table that was causing this problem, removing this last column via this command, solved the issue.

lastcol <- ncol(file)
file[,lastcol] <- NULL

Upvotes: 12

Related Questions