Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore/remove NA values in read.csv

Tags:

r

na

read.csv

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

data<-data.frame(read.csv("test.csv"))

data

A   B   C   
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
like image 537
moadeep Avatar asked Apr 04 '13 10:04

moadeep


People also ask

How do you ignore a function na?

First, if we want to exclude missing values from mathematical operations use the na. rm = TRUE argument. If you do not exclude these values most functions will return an NA . We may also desire to subset our data to obtain complete observations, those observations (rows) in our data that contain no missing data.

How do I ignore rows with NA in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do I exclude missing data in R?

Firstly, we use brackets with complete. cases() function to exclude missing values in R. Secondly, we omit missing values with na. omit() function.


1 Answers

    data<-na.omit(data)

then

    max(data)

If you do not wish to change the data frame then

    max(na.omit(data))
like image 60
Anurag Priyadarshi Avatar answered Oct 24 '22 13:10

Anurag Priyadarshi