NAs are not allowed in subscripted assignments

I'm trying to run the following code , but have error. Please suggest!

df$ppiedu2[df$ppi_mm_educ==12]<- df$education[df$ppi_mm_educ==12]

Error in df$ppiedu2[df$ppi_mm_educ == 12] <- df$education[df$ppi_mm_educ == :
NAs are not allowed in subscripted assignments

What is the result of

any(is.na(df$ppi_mm_educ==12))

If that returns TRUE, then at least one of the subscript values you are passing is NA.

below is my dataset, once ppi_mm_educ==12, replace ppiedu2 with education like df$ppiedu2[df$ppi_mm_educ==12]<- df$education[df$ppi_mm_educ==12]

exdf= data.frame(ppiedu2=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 5, NA, 5, NA, 5, 1, NA, 1, 6, 5),
education=c(5, 5, 5, 7, 6, 6, 5, 5, 7, 5, 5, 6, 5, 5, 5, 5, 6, 5, 5, 1),
ppi_mm_educ=c(6, 7, 6, 5, 5, 5, 1, 5, 6, 12, 13, NA, NA, 6, 12, 5, 12, 12, 3, 4))

Is this what you are trying to do?

exdf= data.frame(ppiedu2=c(1, 	2, 	3, 	4, 	5, 	6, 	7, 	8, 	9, 	5, 	5, 	NA, 	5, 	NA, 	5, 	1, 	NA, 	1, 	6, 	5),
                 education=c(5, 	5, 	5, 	7, 	6, 	6, 	5, 	5, 	7, 	5, 	5, 	6, 	5, 	5, 	5, 	5, 	6, 	5, 	5, 	1),
                 ppi_mm_educ=c(6,  7, 	6, 	5, 	5, 	5, 	1, 	5, 	6, 	12, 	13, 	NA, NA, 6, 12, 	5, 	12, 	12, 	3, 	4))

exdf$ppiedu2 <- ifelse(exdf$ppi_mm_educ == 12, exdf$education, exdf$ppiedu2)

I got the right way without error, and thanks for your help!!

df$ppiedu2[df$ppi_mm_educ==12 & !is.na(df$ppi_mm_educ)]<- df$education[df$ppi_mm_educ==12 & !is.na(df$ppi_mm_educ)]

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.