r/rstats 18h 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 Upvotes

0 comments sorted by