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
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.
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).
Firstly, we use brackets with complete. cases() function to exclude missing values in R. Secondly, we omit missing values with na. omit() function.
data<-na.omit(data)
then
max(data)
If you do not wish to change the data frame then
max(na.omit(data))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With