0

I have a dataframe within R. This is a simple example:

39  30  29  39  46  51  47  44  21
39  30  29  39  46  51  47  44  21
38  30  29  39  46  51  47  43  21
38  30  29  39  46  50  47  43  20

Let's say I want to replace 39 with 's' and 41 with #, etc. I know that I could do this manually, but I have 57 symbol replacements and I'm looking for an automated way to go about this.

I have one data frame with the numbers as above and another with the symbol replacements like this:

34  ^
38  m
39  s
41  #
43  ✎
Litmon
  • 247
  • 3
  • 18

2 Answers2

0

You can check plyr, mapvalues`

df[]=plyr::mapvalues(as.matrix(df),from=c(34,38),to=c('^','m'))
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Here's a way using lapply and match from base R. Note that I used letters instead of symbols just for ease in creating dummy data.

df1[] <- lapply(df1, function(x) {
  m <- match(x, df2$value)
  ifelse(is.na(m), x, df2$let[m])
})

df1

  V1 V2 V3 V4 V5 V6 V7 V8 V9
1  c 30 29  c 46 51 47 44 21
2  c 30 29  c 46 51 47 44 21
3  b 30 29  c 46 51 47  e 21
4  b 30 29  c 46 50 47  e 20

Data -

df1 <- read.table(text = "39  30  29  39  46  51  47  44  21
39  30  29  39  46  51  47  44  21
38  30  29  39  46  51  47  43  21
38  30  29  39  46  50  47  43  20")

df2 <- data.frame(value = c(34,38,39,41,43), let = letters[1:5], stringsAsFactors = F)
Shree
  • 10,835
  • 1
  • 14
  • 36