-1

Say I have a variable in a data frame which can take values "a", "b", "c", "d" and "e". It may look something like this:

V1
c
d
e
e
d
e
a
c
b
b

However, I know/suspect that "a" and "b" describes pretty much the samt thing, as well as "c" and "d". Therefore, I want to create a new variable where these have the same value, i.e.

V1 V2
c  c
d  c
e  e
e  e
d  c
e  e
a  a
c  c
b  a
b  a

I thought this would be easy, but I can't find a way to do it!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Gufis
  • 21
  • 4

3 Answers3

2

we can use

library(car)
df1$V2 <- recode(df1$V1, "'d'='c';'b'='a'")
df1$V2
#[1] "c" "c" "e" "e" "c" "e" "a" "c" "a" "a"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Is there a reason why you couldn't just use:

df$V2 <- df$V1
df$V2[df$V2=="d"] <- "c"
df$V2[df$V2=="b"] <- "a"
Caroline
  • 450
  • 1
  • 5
  • 15
0

I post a similar question a day before.

My solution was:

l <- c("a","a","c","c","e")
factor(l[V1])

Using dplyr packages:

library(dplyr)
df %>% mutate(V2 = factor(l[V1]))

Please refer to this thread for more answer:

Combining factor level in R

Community
  • 1
  • 1
alberthkcheng
  • 143
  • 1
  • 1
  • 6