I have a working code. When I copy the same things in a different dataset, I get errors :(
df <- data.frame(age=c(5,NA,9), marks=c(1,2,7), story=c(2,9,NA))
df
df$colMax <- apply(df[,1:3], 1, function(x) max(x[x != 9],na.rm=TRUE))
df
I tried to do the same on larger data, and I get warnings, why?
maindata$max_pc_age <- apply(maindata[,c(paste("Q2",1:18,sep="_"))], 1, function(x) max(x[x != 9],na.rm=TRUE))
50: In max(x[x != 9], na.rm = TRUE) :
no non-missing arguments to max; returning -Inf
to better understand the problem, I made the changes as shown below, but still get warnings
maindata$max_pc_age <- apply(maindata[,c(paste("Q2",1:18,sep="_"))], 1, function(x) max(x,na.rm=TRUE))
1: In max(x, na.rm = TRUE) : no non-missing arguments to max; returning -Inf