I'm fairly new to R, and I'm trying to make my recoding script somewhat more effective and "correct". I've tried searching the forums but that got me nowhere - perhaps I'm using the wrong terminology and missed it, so please bear with me if the question has already been put up.
I have two factor-variables that I wish to collapse into one factor variable. They stem from the same survey and both measure educational level. The reason I have two variables in the first place is because of an unfortunate survey-construction, but thats beside the point. The main point to be made is that they are mutually exclusive (you can only be in one).
giving me this:
Error in as.character(df$education)[9th grade] | as.character(df$education2)[9th grade]: operations are possible only for numeric, logical or complex types
the post: 'Joining factor levels of two columns in R' seems to be going for another result
again, thank you
La solution
Once they're character strings it's easy
# make them character types
ed <- levels(df$education)[df$education]
ed2 <- levels(df$education2)[df$education2]
# make one new factor that integrates them
ed[is.na(ed)] <- ed2[is.na(ed)]
# make it a factor again
ed <- factor(ed)
You could accelerate the process by reading them in as characters in the first place, especially if you already set column types in read.table.
Autres conseils
You must ensure that all factor levels are present in the result:
Basically you need to make sure the levels are both the "union" or "intersection" of the unique levels and in the same order, then you can join them using c. Search on: [r] factor union levels.