Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace values with table map in R

I have two dataframes, one with 'encoded' values and another one that acts as a dictionary:

> head( encoded_values )
value
1
2
1
3

> head( dict )
id    name
1     foo
2     bar
3     baz

I want to replace the values in the first data frame with the 'decoded' values by looking up the second data frame. This should be the result:

> head( encoded_values )
foo
bar
foo
baz

I've found many similar posts but nothing specific to my case. Maybe it's quite a common operation but I'm very new to R and quite lost with all the many possibilities I've tried so far (none of which worked).

Many thanks.

like image 677
sampei Avatar asked Aug 21 '14 17:08

sampei


People also ask

How do I replace specific values in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values.

How do you change the values in a column based on condition in R?

Replace column values based on checking logical conditions in R DataFrame is pretty straightforward. All you need to do is select the column vector you wanted to update and use the condition within [] . The following example demonstrates how to update DataFrame column values by checking conditions on a numeric column.

How do I replace values with R in NA?

Using R replace() function to update 0 with NA R has a built-in function called replace() that replaces values in a vector with another value, for example, zeros with NAs.


1 Answers

This is what match (note, much faster than merge) is made for:

dict[match(encoded_values$value, dict$id), 2, drop=F]

produces (we need drop=F so that a data.frame is returned instead of a vector since we're selecting only one column):

    name
1    foo
2    bar
1.1  foo
3    baz

match returns the location of the values in it's first argument, in the second argument. You can then use this to index the second argument.

To actually replace:

encoded_values$value <- with(dict, name[match(encoded_values$value, id)])

Note, in this simple case, because your ids match with row numbers in dict, you can also do:

dict[encoded_values$value, 2, drop=F]

but this only works because of the special nature of the id variable in dict (starts at 1, increments by 1 each).

like image 153
BrodieG Avatar answered Oct 08 '22 07:10

BrodieG