Ignore / delete NA values ​​in read.csv

I have a csv file as shown below, which I read in R using read.csv, where column C has 12/30 null values. I want to work out the maximum of each column, but the function "max" R returns "NA" when used in column C. How do I get R to ignore empty values ​​/ NA, I do not see "rm.na" in read.csv?

data<-data.frame(read.csv("test.csv")) data ABC 1 5 6 15 2 3 8 3 3 7 5 4 5 3 8 4 1 4 5 3 4 2 2 10 4 3 8 6 5 2 1 4 4 10 8 4 0 6 0 7 3 8 5 3 3 13 12 13 6 0 0 0 0 2 5 2 NA 7 3 NA 1 8 NA 11 1 NA 1 4 NA 0 7 NA 4 5 NA 3 10 NA 2 0 NA 6 4 NA 0 19 NA 1 5 NA > max(C) [1] NA 
+7
source share
4 answers

you have two options that I can think of

  apply(data,2,max,na.rm=TRUE); # this will remove the NA from columns that contain them 

OR

 apply(na.omit(data),2,max); ## this will remove the NA rows from the data frame and then calculate the max values 
+8
source
  data<-na.omit(data) 

then

  max(data) 

If you do not want to change the data frame, then

  max(na.omit(data)) 
+12
source

I suggest removing NA after reading as others suggested. If, however, you insist on reading only lines other than NA, you can use the bash tool linux to delete them and create a new file:

 grep -Ev file_with_NA.csv NA > file_without_NA.csv 

If you run linux or mac, you already have this tool. On Windows, you must install MinGW or Cygwin to get the tools.

+1
source

You should be able to use

 max(x,na.rm=TRUE) 
-one
source

All Articles