I need to put all the rows that contain a date in a variable as 1 and all the Null or Na's as 0. I need this in a loop.
I have already tried this and it doesn't work
p$n <- ifelse(is.na(n),0,1)
The lubridate package features a function is.Date, which can check if a value is a valid R date:
p$isdate <- ifelse(is.Date(p$n), 1, 0)
I store the result into a new column isdate, under the assumption that you don't want to clobber your original n column.
The reason your code did not work above is that n is probably not an object in your global environment. This should work instead:
p$n <- ifelse(is.na(p$n),0,1)
p <- data.frame(list(n = c("date",NA,"date",NA,NA, "date")))
p$n <-ifelse(is.na(n), 0, 1)
Error in ifelse(is.na(n), 0, 1) : object 'n' not found
#use the '$' operator instead
p$n <-ifelse(is.na(p$n), 0, 1)