Home > OS >  Create a new column populated by level names in R
Create a new column populated by level names in R

Time:06-02

I have an R data frame with a column of integers. This column has a set of levels:

my_data <- data.frame(x=c(1,2,3,1,2,3,1,2,3))
levels(my_data$x) <- c("group1", "group2", "group3")

How can I create a new character column populated with the level names? Desired output:

> my_data
  x      y
1 1 group1
2 2 group2
3 3 group3
4 1 group1
5 2 group2
6 3 group3
7 1 group1
8 2 group2
9 3 group3

CodePudding user response:

The column 'x' is a numeric column with additional attribute levels added (not a factor column per se)

 str(my_data$x)
 num [1:9] 1 2 3 1 2 3 1 2 3
 - attr(*, "levels")= chr [1:3] "group1" "group2" "group3"

thus we use the 'x' values (in sequence) as index for levels attribute

library(dplyr)
my_data <- my_data %>%
     mutate(y = levels(x)[x])

-output

my_data
x      y
1 1 group1
2 2 group2
3 3 group3
4 1 group1
5 2 group2
6 3 group3
7 1 group1
8 2 group2
9 3 group3

Or in base R

mydata$y <- with(my_data, levels(x)[x])

If the values of 'x' are different, use match

mydata$y <- with(my_data, levels(x)[match(x, unique(x))])

CodePudding user response:

You could do:

my_data <- transform(my_data, y = factor(x, label = levels(x)))

  x      y
1 1 group1
2 2 group2
3 3 group3
4 1 group1
5 2 group2
6 3 group3
7 1 group1
8 2 group2
9 3 group3

Note that with this method, x does not have to be indices ie 1,2,3 but can be anything. Take a look below:

my_data1 <- data.frame(x=c(1,2,3,1,2,3,1,2,3)   10)
levels(my_data1$x) <- c("group1", "group2", "group3")
transform(my_data1, y = factor(x, label = levels(x)))
   
  x      y
1 11 group1
2 12 group2
3 13 group3
4 11 group1
5 12 group2
6 13 group3
7 11 group1
8 12 group2
9 13 group3
  •  Tags:  
  • r
  • Related