2
votes

My dataframe looks like this

data = data.frame(ID=c(1,2,3,4,5,6,7,8,9,10),
              Gender=c('Male','Female','Female','Female','Male','Female','Male','Male','Female','Female'))

And I have a reference list that looks like this -

ref=list(Male=1,Female=2)

I'd like to replace values in the Gender column using this reference list, without adding a new column to my dataframe.

Here's my attempt

do.call(dplyr::recode, c(list(data), ref))

Which gives me the following error -

no applicable method for 'recode' applied to an object of class "data.frame"

Any inputs would be greatly appreciated

5

5 Answers

2
votes

An option would be do a left_join after stacking the 'ref' list to a two column data.frame

library(dplyr)
left_join(data, stack(ref), by = c('Gender' = 'ind')) %>%
    select(ID, Gender = values)

A base R approach would be

unname(unlist(ref)[as.character(data$Gender)])
#[1] 1 2 2 2 1 2 1 1 2 2
2
votes

In base R:

data$Gender = sapply(data$Gender, function(x) ref[[x]])

2
votes

You can use factor, i.e.

factor(data$Gender, levels = names(ref), labels = ref)
#[1] 1 2 2 2 1 2 1 1 2 2
2
votes

You can unlist ref to give you a named vector of codes, and then index this with your data:

transform(data,Gender=unlist(ref)[as.character(Gender)])
   ID Gender
1   1      1
2   2      2
3   3      2
4   4      2
5   5      1
6   6      2
7   7      1
8   8      1
9   9      2
10 10      2
1
votes

Surprisingly, that one works as well:

data$Gender <- ref[as.character(data$Gender)]

#> data
#    ID Gender
# 1   1      1
# 2   2      2
# 3   3      2
# 4   4      2
# 5   5      1
# 6   6      2
# 7   7      1
# 8   8      1
# 9   9      2
# 10 10      2