I am trying to build a function as part of a larger function in R. Some of the pieces are working fine but others are not. Here is the piece of the code that is giving me issues.
This part of the function is designed to identify if a variable in a dataframe is missing, then generate a new variable which records if that specific case is missing or present. I want the new variable to have the suffix .zero (q1 becomes q1_zero, q2 becomes q2_zero, etc.). I can generate the suffix without any issues. Creating the new variable is causing some problems. Any insight would be greatly appreciated.
function1 <- function (x, data) {
# new variable name
temp <- paste (x, .zero, sep="", collapse = NULL)
temp
# is variable missing
# I don't know if I should use this method or ifelse()
data$temp [is.na (data$x)]<- 0
data$temp [!is.na (data$x)]<- 1
return (data$temp)
}