1

I have a df with two columns where the elements in them are codes:

> head(listaNombres)
  ocupacion1 ocupacion2
1    11-2020    11-9190
2    11-2020    41-1010
3    11-2020    41-2030
4    11-2020    41-3090
5    11-2020    41-4010
6    11-3030    11-9190

And then a separate df with the meaning for each code:

> head(descripcion)
# A tibble: 6 x 2
  broadGroup Desc                                     
  <chr>      <chr>                                    
1 11-1010    Chief Executives                         
2 11-1020    General and Operations Managers          
3 11-1030    Legislators                              
4 11-2010    Advertising and Promotions Managers      
5 11-2020    Marketing and Sales Managers             
6 11-2030    Public Relations and Fundraising Managers

How can I convert the codes in the first df with the Desc column in the second?

Martin Gal
  • 16,640
  • 5
  • 21
  • 39

1 Answers1

0

This question has been answered a few times but non seem to have an answer that is simple and also uses base R. I'm not a fan of making people use unnecessary packages, so I'll write this up since there is an easy and straight forward solution that requires no extra packages.

Using the 'match' function we can

oldvalues <- descripcion$broadGroup 
# sets up values we wish to change from

newvalues <- descripcion$Desc 
# sets up the values we want to change to

listaNombres$ocupacion1 = newvalues[ match(listaNombres$ocupacion1, oldvalues) ]
# Overwrite current ocupacion1 values with desired recode

listaNombres$ocupacion2 = newvalues[ match(listaNombres$ocupacion2, oldvalues) ]
# Overwrite current ocupacion2 values with desired recode

Say we have

v3$recode = v2[ match(v3$recod, v1) ]

What this does is, is takes our three vectors, v1, v2 and v3 and using match(v3,v1), match returns a vector of positions in v1 where the first match between an element of v3 and v1 occurs. We then select elements from v2 using this vector of positions, which gives us the recoded version of v3$record. We then feed this recoded vector of values straight back into v3$record overwriting the old values.

edit: I've since had a look using R and this solution works using the following mockup dataset

ocupacion1 = c(1,2,3,4)
ocupacion2 = c(3,4,4,2)
listaNombres = data.frame(ocupacion1,ocupacion2)

broadGroup = c(1,2,3,4)
Desc = c("one","two","three","four")
descripcion = data.frame(broadGroup,Desc)

combining everything gives the following

> ocupacion1 = c(1,2,3,4)
> ocupacion2 = c(3,4,4,2)
> listaNombres = data.frame(ocupacion1,ocupacion2)
> head(listaNombres)

  ocupacion1 ocupacion2
1          1          3
2          2          4
3          3          4
4          4          2

> broadGroup = c(1,2,3,4)
> Desc = c("one","two","three","four")
> descripcion = data.frame(broadGroup,Desc)
> head(descripcion)

  broadGroup  Desc
1          1   one
2          2   two
3          3 three
4          4  four

> oldvalues <- descripcion$broadGroup 
> newvalues <- descripcion$Desc 
> listaNombres$ocupacion1 = newvalues[ match(listaNombres$ocupacion1, oldvalues) ]
> listaNombres$ocupacion2 = newvalues[ match(listaNombres$ocupacion2, oldvalues) ]
> # Overwrite current ocupacion2 values with desired recode
> head(listaNombres)

  ocupacion1 ocupacion2
1        one      three
2        two       four
3      three       four
4       four        two
  • 1
    Imho, base R's `merge` function is a simple solution for this special problem. – Martin Gal Aug 08 '21 at 01:09
  • 1
    Hi, thank you very much for your answer. Sadly, it didn't work though. I got this error: listaNombres$ocupacion1 = newvalues[ match(listaNombres$ocupacion1, oldvalues)] # Overwrite current ocupacion1 values with desired recode Error in `$<-.data.frame`(`*tmp*`, ocupacion1, value = character(0)) : replacement has 0 rows, data has 237 – Martín Storni Aug 08 '21 at 06:09
  • try that again @MartínStorni, I've fixed the code. The issue was I had defined "newvalues" but then later I used "newvals". – Frithjof Herb Aug 09 '21 at 07:18