Here's a data frame I'm working with:
c1 = c('a', 'b', 'c', 'd')
c2 = c('d', 'a', 'd', 'c')
c3 = c('a', 'c', 'd', 'b')
c4 = c('a', 'c', 'b', 'd')
df = data.frame(c1, c2, c3, c4)
c1 c2 c3 c4
a d a a
b a c c
c d d b
d c b d
I would like to convert using this scale: a=1, b=2, c=3, d=4. So that I get something like this:
c1 c2 c3 c4
1 4 1 1
2 1 3 3
3 4 4 2
4 3 2 4
This is what I have come up with:
for(i in colnames(df)){
df$i = gsub("a", 1, df$i)
df$i = gsub("b", 2, df$i)
df$i = gsub("c", 3, df$i)
df$i = gsub("d", 4, df$i)
}
But it doesn't work. Should I use gsub here, or is there a simpler way to do this?