larmarange / labelled

Manipulating labelled vectors in R
https://larmarange.github.io/labelled/
GNU General Public License v3.0
73 stars 16 forks source link

set_value_labels -- Error: `x` and `value` must be same type #155

Closed esul1121 closed 6 months ago

esul1121 commented 8 months ago

Hello-

I am using your package to write a sav file and set_value_labels is giving me this error: Error: x and value must be same type

I used a column from a dataframe where a list has value labels corresponding to the numeric values (integer) and for the character values, value labels are all NAs.

The data looks like this: [1] "0=No,1=Yes" [2] "0=Not Selected,1=Selected" [3] NA ... [456]"0=No,1=Yes"

I would appreciate it if this is a user-error from my end!

Thank you,

larmarange commented 8 months ago

Hi

To define no value labels, you should use NULL, not NA.

esul1121 commented 8 months ago

Hi again,

No luck after converting them to empty. It gave me the same error. I had no issue to use "set_variable_labels", possibly indicating that it's not the R environmental problem.

Do you have any other suggestion for me to try?

Just a thought, as those variables having NA values in the dataframe are categorical, not numerical so it's technically Not Applicable (NA) rather than NULL (empty).

larmarange commented 8 months ago

I'm sorry but I will need a reproductive example to understand what you are doing.

If you are using set_value_labels() with the .labels argument, you should pass a named list of named vectors, not a character vector as "0=No,1=Yes"

larmarange commented 8 months ago

for example:

list(
    id_child = NULL,
    sex = c(male = 1, female = 2),
    alive = c(`no, dead` = 0, `yes, alive` = 1),
    age_at_death = NULL
)