Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting "undefined columns selected" in R

Tags:

dataframe

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
like image 625
Error404 Avatar asked Dec 26 '22 01:12

Error404


1 Answers

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
like image 94
Error404 Avatar answered Jan 08 '23 21:01

Error404