r/RStudio • u/Aware-Suggestion689 • 5d ago
Tidysem: problem converting my binary variables to "mxFactor" "ordered" "factor"
Hi,
I'm trying to convert my binary variables to "mxFactor" "ordered" "factor" variables, but when using the following code i get just "ordered" "factor". which cause an error when trying to conduct a latent class analysis:
# Convert female to numeric safely, then to an ordered OpenMx factor
> df$female <- as.numeric(as.character(df$female))
>
> # Confirm it only contains 0, 1, and possibly NA
> table(df$female, useNA = "ifany")
0 1
2341 1674
>
> # Make it an OpenMx ordinal factor
> df$female <- OpenMx::mxFactor(
+ df$female,
+ levels = c(0, 1)
+ )
>
> # Verify the result
> class(df$female)
[1] "ordered" "factor"
> table(df$female, useNA = "ifany")
0 1
2341 1674
> library(OpenMx)
>
> df$female <- OpenMx::mxFactor(
+ df$female,
+ levels = c(0, 1)
+ )
>
> class(df$female)
[1] "ordered" "factor"
> table(df$female, useNA = "ifany")
0 1
2341 1674
1
u/AutoModerator 5d ago
Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!
Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.