Error messages from factors.

, , Comments Off on Error messages from factors.

Dear Miranda,
Thanks for the insightful material under data management. Kindly note that:
1. When I use as.factor(Pedigree) it will not work for me. I get an error message in RStudio.
2. When I use as.factor(EIHYB$Pedigree) I do not get the error message, but my variable still remains a character.
3. When I use EIHYB$Pedigree=as.factor(EIHYB$Pedigree) it works and my variable is converted into a factor.

Can we discuss this?
kind regards,

Hi Rumbi,

Thanks for the comment! I’ll go through your comment item by item:
1. as.factor(Pedigree) would indeed produce an error as R (either on its own or through RStudio) does not know which data frame (where the data is read into) contains the variable Pedigree.

2. as.factor(EIHYB$Pedigree) does work as should be expected. This time the data frame is stated and the variable Pedigree selected. However this only temporarily converts the variable to a factor and so in the original data frame it stays as a character variable. This is because the output variable has not be stated and so the output of the command is only stored for that execution. In other words, this is a good option if you only want to have Pedigree as a factor for some commands during the analysis.

3. EIHYB$Pedigree=as.factor(EIHYB$Pedigree) outputs the factor levels back to the original variable and, in doing so permanently changes the variable from a character to a factor level. EIHYB$Pedigree=factor(EIHYB$Pedigree) would work equally well. This would be good if you wanted to make Pedigree a factor for the entire analysis rather than on a command by command basis. An alternative to point 2 is to create a new variable using EIHYB$PedigreeF=as.factor(EIHYB$Pedigree) which then allows the original variable to remain as well a copy which will be treated as a factor.

Hopefully this helps clear up the responses you were getting in RStudio. Feel free to ask further questions if my explanation is not clear.

Thank you for using our training programme, and I hope you are enjoying it.

Kind regards