Suppose I have the following dataframes (essentially 4 similar datasets):
q1_hosp <- data.frame(dxe1 = c(1,NULL, NA, NULL, 1), dxe2 = c(1,NULL, NULL, NULL, 1))
q2_hosp <- data.frame(dxe1 = c(NULL,1, 1, NA, 1), dxe2 = c(1,1, 1, NULL, 1))
q3_hosp <- data.frame(dxe1 = c(NA,NULL, 1 1, 1), dxe2 = c(1,NA, NA, NULL, 1))
q4_hosp <- data.frame(dxe1 = c(1,1, 1 NULL, 1), dxe2 = c(1,NULL, 1, 1, 1))
What I would like is to turn the NULL entries into missing values in R. I can do them by writing the following codes in a tedious manner:
any(is.na(q1_hosp$dxe1)) # there are missing rows, keep NAs
q1_hosp$dxe1 <- na_if(q1_hosp$dxe1, "NULL") #convert NULLto NA
any(is.na(q1_hosp$dxe2)) # there are missing rows, keep NAs
q1_hosp$dxe2 <- na_if(q1_hosp$dxe2, "NULL") #convert NULLto NA
any(is.na(q2_hosp$dxe1)) # there are missing rows, keep NAs
q2_hosp$dxe1 <- na_if(q2_hosp$dxe1, "NULL") #convert NULLto NA
any(is.na(q2_hosp$dxe2)) # there are missing rows, keep NAs
q2_hosp$dxe2 <- na_if(q2_hosp$dxe2, "NULL") #convert NULLto NA
any(is.na(q3_hosp$dxe1)) # there are missing rows, keep NAs
q3_hosp$dxe1 <- na_if(q3_hosp$dxe1, "NULL") #convert NULLto NA
any(is.na(q3_hosp$dxe2)) # there are missing rows, keep NAs
q3_hosp$dxe2 <- na_if(q3_hosp$dxe2, "NULL") #convert NULLto NA
any(is.na(q4_hosp$dxe1)) # there are missing rows, keep NAs
q4_hosp$dxe1 <- na_if(q4_hosp$dxe1, "NULL") #convert NULLto NA
any(is.na(q4_hosp$dxe2)) # there are missing rows, keep NAs
q4_hosp$dxe2 <- na_if(q4_hosp$dxe2, "NULL") #convert NULLto NA
What I would like is to create a loop so that the process is less tedious.
I have been trying with this
for(i in 1:4) {
##Clean the dxe variables
any(is.na(q[i]_hosp$dxe1)) # there are missing rows, keep NAs
q[i]_hosp$dxe1 <- na_if(q[i]_hosp$dxe1, "NULL") #convert NULLto NA
any(is.na(q[i]_hosp$dxe2)) # there are missing rows, keep NAs
q[i]_hosp$dxe1 <- na_if(q[i]_hosp$dxe2, "NULL") #convert NULLto NA
}
But my code doesn't work and I am getting errors. Can anyone help write the for loop for what I want to achieve?