1

I want to replace certain values in a data frame column with values from a lookup table. I have the values in a list, stuff.kv, and many values are stored in the list (but some may not be).

stuff.kv <- list()
stuff.kv[["one"]] <- "thing"
stuff.kv[["two"]] <- "another"
#etc

I have a dataframe, df, which has multiple columns (say 20), with assorted names. I want to replace the contents of the column named 'stuff' with values from 'lookup'.

I have tried building various apply methods, but nothing has worked.

I built a function, which process a list of items and returns the mutated list,

stuff.lookup <- function(x) {
  for( n in 1:length(x) ) {
    if( !is.null( stuff.kv[[x[n]]] ) ) x[n] <- stuff.kv[[x[n]]]
  }
  return( x )
}

unlist(lapply(df$stuff, stuff.lookup))

The apply syntax is bedeviling me.

ChuckCottrill
  • 3,999
  • 2
  • 22
  • 34
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions.. – MrFlick Jul 30 '18 at 00:03
  • Maybe you want the `dplyr` replace function? https://stackoverflow.com/questions/28013850/change-value-of-variable-with-dplyr – MrFlick Jul 30 '18 at 00:04
  • 1
    Do you want something like this - [How do I map a vector of values to another vector with my own custom map in R?](https://stackoverflow.com/questions/18456968/how-do-i-map-a-vector-of-values-to-another-vector-with-my-own-custom-map-in-r/18457055) ? – thelatemail Jul 30 '18 at 00:06
  • that is similar, but G5W's answer is much more readable (and I admit that I am trying to write perl or python in R). – ChuckCottrill Jul 30 '18 at 00:21

2 Answers2

2

Since you made such a nice lookup table, You can just use it to change the values. No loops or apply needed.

## Sample Data
set.seed(1234)
DF = data.frame(stuff = sample(c("one", "two"), 8, replace=TRUE))

## Make the change
DF$stuff = unlist(stuff.kv[DF$stuff])
DF
    stuff
1   thing
2 another
3 another
4 another
5 another
6 another
7   thing
8   thing
G5W
  • 32,266
  • 10
  • 31
  • 60
0

Below is a more general solution building on @G5W's answer as it doesn't cover the case where your original data frame has values that don't exist in the lookup table (which would result in length mismatch error):

library(dplyr)

stuff.kv <- list(one = "another", two = "thing")

df <- data_frame(
  stuff = rep(c("one", "two", "three"), each = 3)
)

df <- df %>%
  mutate(stuff = paste(stuff.kv[stuff]))
Ozan147
  • 2,401
  • 1
  • 10
  • 24