SAPPLY function for COLUMN NULL
colnames(dd) #[1] "col1" "colb" null_vector<- colnames(dd) sapply(null_vector,makeNull,dd) #???? col1 colb #[1,]?? NA??? 4 #[2,]??? 2?? NA #[3,]??? 3??? 2 #[4,]??? 4?? NA #[5,]??? 1??? 4 #[6,]?? NA??? 5 #[7,]??? 1??? 6 A.K.
I am trying to make a column value in a dataframe = NA if there is a 0
or high value in that column. I need to do this process repeatedly, hence I >have to define a function. Here is the code, that I am using and is not working. Please advise on where I am making an error.
makeNull <- function(col, data=dd) {
is.na(data[[col]]) <- data[[col]] ==0
is.na(data[[col]]) <- data[[col]] > 999999
return(data[[col]])
}
dd <- data.frame(col1=c(0,2,3,4,1,0,1),colb=c(4,0,2,0,4,5,6))
null_vector=c("cola","colb")
sapply(null_vector,function(x) makeNull(x,dd))
Error in `[[<-.data.frame`(`*tmp*`, col, value = logical(0)) : ?replacement has 0 rows, data has 7
Thank you in advance.
-Sanjeev