crangos - 27 days ago 4x

R Question

I have data frame with some numerical variables and some categorical

`factor`

`numbers <- 1:4`

letters <- factor(c("a", "b", "c", "d"))

df <- data.frame(numbers, letters)

df

# numbers letters

# 1 1 a

# 2 2 b

# 3 3 c

# 4 4 d

If I change the order of the levels, the letters no longer are with their corresponding numbers (my data is total nonsense from this point on).

`levels(df$letters) <- c("d", "c", "b", "a")`

df

# numbers letters

# 1 1 d

# 2 2 c

# 3 3 b

# 4 4 a

I simply want to change the

Answer

Use the `levels`

argument of `factor`

:

```
df <- data.frame(f = 1:4, g = letters[1:4])
df
# f g
# 1 1 a
# 2 2 b
# 3 3 c
# 4 4 d
levels(df$g)
# [1] "a" "b" "c" "d"
df$g <- factor(df$g, levels = letters[4:1])
# levels(df$g)
# [1] "d" "c" "b" "a"
df
# f g
# 1 1 a
# 2 2 b
# 3 3 c
# 4 4 d
```

Source (Stackoverflow)

Comments