Troubleshoot "undefined selected columns" in R

I tried to remove all zero-variance columns from my data using this command

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

This command worked perfectly for my previous datasets, now I am trying to use in a new dataset and this 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 that I did not select any columns, I just applied this function to all columns! Why did I get an error indicating that undefined columns were selected! Any idea what could go wrong?

The data looks that 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
source share
1 answer

The input file was a CSV file and was read using the read.csv command, it had an extra empty column at the end of the table that caused this problem, deleting this last column with this command solved the problem.

 lastcol <- ncol(file) file[,lastcol] <- NULL 
+10
source

Source: https://habr.com/ru/post/1487008/


All Articles