shrimp32
shrimp32

Reputation: 147

Replace a factor in a data frame column into a numeric value in R?

I have a data frame df1 such that:

group=c("Group 1", "Group 2", "Group3","Group 1", "Group 2", "Group3")
year=c("2000","2000","2000", "2015", "2015", "2015")
items=c(12, 10, 15, 5, 10, 7)
df1=data.frame(group, year, items)

In the column "group", how do I replace each factor with a numerical value? For example, I want to replace "Group 1" with 0, "Group 2" with 5, and "Group 3" with 3 so it looks like this:

  group year items
1     0 2000    12
2     5 2000    10
3     3 2000    15
4     0 2015     5
5     5 2015    10
6     3 2015     7

Thanks!

Upvotes: 1

Views: 1144

Answers (2)

Mostafa Rezaei
Mostafa Rezaei

Reputation: 629

Use an auxiliary named-vector "g" to transform the vector "group":

group <- c("Group 1", "Group 2", "Group3","Group 1", "Group 2", "Group3")
g <- c(0, 5, 3)
names(g) <- c("Group 1", "Group 2", "Group3")
new_group <- g[group]

Upvotes: 1

Rorschach
Rorschach

Reputation: 32426

You can use as.integer

transform(df1, group=as.integer(group))
#   group year items
# 1     1 2000    12
# 2     2 2000    10
# 3     3 2000    15
# 4     1 2015     5
# 5     2 2015    10
# 6     3 2015     7

In response to the updated question, you can use a key and index by the integer of your factor

key <- c(0, 5, 3)
transform(df1, group=key[as.integer(group)])

#   group year items
# 1     0 2000    12
# 2     5 2000    10
# 3     3 2000    15
# 4     0 2015     5
# 5     5 2015    10
# 6     3 2015     7

And as @SimonG says, you can reorder your factor however you want using factor and its argument levels,

Upvotes: 2

Related Questions