Replace multiple values over multiple columns of a data frame in R

alex

I am trying to replace certain numbers with others over multiple columns of a data set.

1=1
2=1
3=2
4=3
5=3

So if I have:


id      age        A1       weight     Var1       A2    
  
3       5          2         50         1         4
7       23         1         67         5         3
9       78         4         90         3         2
12      14         1         17         2         3

I want to replace those numbers in columns A1, Var1, and A2


id      age        A1       weight     Var1       A2    
  
3       5          1         50         1         3
7       23         1         67         3         2
9       78         3         90         2         1
12      14         1         17         1         2



I think I can do something with mutate_across but I'm not sure

onyambu
df1 %>%
   mutate(across(c(A1, Var1, A2), recode, "1" = 1,
                 "2" = 1, `3` = 2, `4` = 3, `5` = 3))

  id age A1 weight Var1 A2
1  3   5  1     50    1  3
2  7  23  1     67    3  2
3  9  78  3     90    2  1
4 12  14  1     17    1  2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

replace multiple columns of data frame with indexing in r

How to replace values in multiple columns in a data.frame with values from a vector in R?

Replace values in data.frame for multiple columns and merge

R aggregate rows of a data frame over multiple columns with different operators

Merge R data frame or data table and overwrite values of multiple columns

tallying up the number of values in data frame in R from multiple columns

R: Removing multiple columns from data frame based on vector values

R data.table replace values in multiple columns

find and replace values in multiple columns in R

Pandas, how to replace mean values in python data frame using multiple grouped columns

R data frame - aggregating multiple columns at once

Unlisting multiple columns in R data frame

How to merge data frame with multiple columns with R?

Recoding multiple columns in a R data frame

Compare data frame multiple columns with row in R

Looking to transpose a data frame on multiple columns in R

R: Replace multiple values in multiple columns of dataframes with values in another column

Multiple columns data frame

R data frame: select rows that meet logical conditions over multiple columns (variables) indexed by name

R - transform columns containing key values into multiple columns, in a data.frame object

R - create matrix with values based on the relationship between values in multiple columns of another data frame

Look up value in data frame stored over multiple columns

convert same labels over multiple columns in a data frame to numbers

rstatix::anova_test() over multiple columns of data frame

R replace multiple columns by merging another data

Adding new columns to data frame based on the values of multiple columns

Check if any value ( multiple columns) of one data frame exists in any values (multiple columns) of another data frame

R: Replace multiple values in multiple columns of dataframes with NA

For with if loop in R, running on the data frame with multiple columns and multiple rows