简体   繁体   中英

R: 'Missing Value where True/False needed'

So I know this has been asked before, but from what I've searched I can't really find an answer to my problem. I should also add I'm relatively new to R (and any type of coding at all) so when it comes to fixing problems in code I'm not too sure what I'm looking for.

My code is:

education_ge <- data.frame(matrix(ncol=2, nrow=1))
colnames(education_ge) <- c("Education","Genetic.Engineering")

for (i in 1:nrow(survey))
if (survey[i,12]=="Bachelors")  
education_ge$Education <- survey[i,12]

To give more info, 'survey' is a data frame with 12 columns and 26 rows, and the 12th column, 'Education', is a factor which has levels such as 'Bachelors', 'Masters', 'Doctorate' etc.

This is the error as it appears in R:

for (i in 1:nrow(survey))
  if (survey[i,12]=="Bachelors")  
    education_ge$Education <- survey[i,12]
Error in if (survey[i, 12] == "Bachelors") education_ge$Education <- survey[i,  : 
  missing value where TRUE/FALSE needed

Any help would be greatly appreciated!

If you just want to ignore any records with missing values and get on with your analysis, try inserting this at the beginning:

survey <- survey[ complete.cases(survey), ]

It basically finds the indexes of all the rows where there are no NAs anywhere, and then subsets survey to have only those rows.

For more information on subsetting, try reading this chapter: http://adv-r.had.co.nz/Subsetting.html

The command:

sapply(survey,function (x) sum(is.na(x)))

will show you how many NAs you have in each column. That might help your data cleaning.

You can try this:

sub<-subset(survey,survey$Education=="Bachelors") 

education_ge$Education<-sub$Education

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM