I have the following data set:
df<-data.frame(read.table(header = TRUE, text = "
ID N1 N2 N3 N4
1 2 3 4 5
11 NA -12 14 55
21 12 SON 34 14"))
I want to find out what is the max entry in each row. This would be, for example, 5 in the first row. Obviously, the situation is more complicated because of missing values ('NA') and a string ('SON').
I first tried the following command:
df$Result<-apply(df,1, max, na.rm= TRUE)
The results are [5,55, SON]! Not what I wanted. I therefore then tried:
checkd<- function(x) if(is.integer(x)== TRUE)max(x)
df$Result<-apply(df,1, checkd)
Funnily, it removed the last column df$Result
. Does anyone know what did I do wrong? Also, what would be the solution to my problem?
Also, of I try the following code:
checkd<- function(x) if(is.integer(x)== TRUE)max(x)
df$Result<-apply(df,1, checkd, na.rm= TRUE)
it gives me Error in FUN(newX[, i], ...) : unused argument (na.rm = TRUE)
! Why is that? My function checkd
does generally not seem to cause any problems to R. Why does R reject na.rm= TRUE
when I use checkd
but not when I use max
in apply?
Thanks,
Dom