Marro
Marro

Reputation: 105

R: factor(x, levels, labels) returns unexpected <NA>

I'm not sure how else to describe this problem. I hope someone knows of this issue already.

Reprex:

t_fac <- factor(c("A", "A"))
t_lev <- factor(c("B", "C"))
t_lab <- c(1, 1)

factor(t_fac, levels = t_lev, labels = t_lab)

Returns:

[1] <NA> <NA>
Levels: 1

Where I would expect:

[1] 1 1
Levels: 1

How to get expected result? Any ideas are welcome.

Upvotes: 0

Views: 67

Answers (2)

Luigi
Luigi

Reputation: 277

Reading from the help page for factor, levels is "an optional vector of the unique values (as character strings) that x might have taken". You have the value "A" in your data vector (t_fac), but you include only "B" and "C" as levels.

I think you can adjust this simply by including "A" as a possible level:

t_fac <- c("A", "A")
t_lev <- c("A", "B") # "A" needs to be here
t_lab <- c(1, 1)

factor(t_fac, levels = t_lev, labels = t_lab)

[1] 1 1
Levels: 1

If you want to have "A", "B", and "C" as possible levels, you will need to add a 1 in the labels vector:

t_fac <- c("A", "A")
t_lev <- c("A", "B", "C")
t_lab <- c(1, 1, 1)

factor(t_fac, levels = t_lev, labels = t_lab)

Upvotes: 2

ThomasIsCoding
ThomasIsCoding

Reputation: 101034

Do you mean this?

> factor(t_fac, labels = unique(t_lab))
[1] 1 1
Levels: 1

Upvotes: 0

Related Questions