How can I make column names like "X_re" after recoding?

41 Views Asked by At

I recoded columns 'satface1, satface2,..., satface6' and I want to make new columns in my data name 'satface1_re, satface_2, ..., satface6_re'. How can I make it?

What I tried was:

data$re <- data %>% select((num_range("satface",1:6,width = 1))) %>%
  mutate_all(function(x) recode(x,'1=;2=5;3=4;4=3;5=1;6=1'))

Then new columns were made like 're.satface1, re.satface2,..., re.satface6'

1

There are 1 best solutions below

0
On

Sample data:

df <- data.frame(
  satface1 = NA,
  satface2 = NA,
  satface3 = NA,
  satface4 = NA
)

To create new columns with the suffix _re, you can subset dfand use paste:

df[paste(names(df), "_re", sep = "")] <- NA

Result:

df
  satface1 satface2 satface3 satface4 satface1_re satface2_re satface3_re satface4_re
1       NA       NA       NA       NA          NA          NA          NA          NA