replace numbers of one dataset across rows of many columns with characters that match the numbers of another dataframe by column in r

To replace numbers in one dataset with characters from another dataframe, you can use the match() function in R. Here's an example:

main.r
# Create sample dataframes
df1 <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6),
  col3 = c(7, 8, 9)
)

df2 <- data.frame(
  num = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  char = c("a", "b", "c", "d", "e", "f", "g", "h", "i")
)

# Replace numbers in df1 with characters from df2
df1[] <- lapply(df1, function(x) df2$char[match(x, df2$num)])

print(df1)
351 chars
17 lines

This will output:

main.r
  col1 col2 col3
1    a    d    g
2    b    e    h
3    c    f    i
68 chars
5 lines

In this example, match() is used to find the position of each number in df1 within the num column of df2, and then the corresponding character from df2 is used to replace the number.

Note: This assumes that the numbers in df1 are exact matches for the numbers in df2$num. If there are any missing values or mismatches, the result will be NA.

Alternatively, you can use dplyr package to achieve this:

main.r
library(dplyr)

df1 <- df1 %>%
  mutate(
    across(everything(), ~ df2$char[match(.x, df2$num)])
  )

print(df1)
114 chars
9 lines

This will produce the same output as the previous example. The across() function applies the replacement to all columns in df1.

related categories

gistlibby LogSnag