Home > Blockchain >  Rename and recode range of new variables in dataframe in R
Rename and recode range of new variables in dataframe in R

Time:05-06

I essentially want recode and rename a range of variables in a dataframe. I am looking for a way to do this in the single step.

Example in pseudo-code:

require(dplyr)

df <- iris %>% head()

df %>% mutate(
   paste0("x", 1:3) = across(       # In the example I want to rename 
      Sepal.Length:Petal.Length,    # the variables I've selected
      ~ .x   1                      # and recoded to "x1" ... "x5"
   )
)
df

Desired output:

     x1    x2    x3 Petal.Width Species
  <dbl> <dbl> <dbl>       <dbl>   <fct>
1   5.1   3.5   1.4         0.2  setosa
2   4.9   3.0   1.4         0.2  setosa
3   4.7   3.2   1.3         0.2  setosa
4   4.6   3.1   1.5         0.2  setosa
5   5.0   3.6   1.4         0.2  setosa
6   5.4   3.9   1.7         0.4  setosa

CodePudding user response:

Maybe rename_with() is what you want. After that you can manipulate these renamed columns with mutate(across(...)).

library(dplyr)

df %>%
  rename_with(~ paste0("x", seq_along(.x)), Sepal.Length:Petal.Length) %>%
  mutate(across(x1:x3, ~ .x * 10))

  x1 x2 x3 Petal.Width Species
1 51 35 14         0.2  setosa
2 49 30 14         0.2  setosa
3 47 32 13         0.2  setosa
4 46 31 15         0.2  setosa
5 50 36 14         0.2  setosa
6 54 39 17         0.4  setosa

If you want to manipulate and rename a range of columns in one step, try the argument .names in across().

df %>%
  mutate(across(Sepal.Length:Petal.Length, ~ .x * 10,
                .names = "x{seq_along(.col)}"),
         .keep = "unused", .after = 1)

  x1 x2 x3 Petal.Width Species
1 51 35 14         0.2  setosa
2 49 30 14         0.2  setosa
3 47 32 13         0.2  setosa
4 46 31 15         0.2  setosa
5 50 36 14         0.2  setosa
6 54 39 17         0.4  setosa

Hint: You can use seq_along() to create a sequence 1, 2, ... along with the selected columns, or match() to get the positions of the selected columns in the data, i.e.
.names = "x{match(.col, names(df))}".

CodePudding user response:

The below code allows you to just input the column numbers into a for loop, not sure if this is what you're going for.

require(dplyr)

df <- iris %>% head()


for(i in 1:3){
  names(df)[i] <- paste0("x",i)
}

df

Outputs:

   x1  x2  x3 Petal.Width Species
1 5.1 3.5 1.4         0.2  setosa
2 4.9 3.0 1.4         0.2  setosa
3 4.7 3.2 1.3         0.2  setosa
4 4.6 3.1 1.5         0.2  setosa
5 5.0 3.6 1.4         0.2  setosa
6 5.4 3.9 1.7         0.4  setosa

CodePudding user response:

You could add consecutive numbers to n columns with the same prefix this way:

df <- iris %>% head()

n <- 3
colnames(df)[1:n] <- sprintf("x%s",1:n)

output:

# x1  x2  x3 Petal.Width Species
# 1 5.1 3.5 1.4         0.2  setosa
# 2 4.9 3.0 1.4         0.2  setosa
# 3 4.7 3.2 1.3         0.2  setosa
# 4 4.6 3.1 1.5         0.2  setosa
# 5 5.0 3.6 1.4         0.2  setosa
# 6 5.4 3.9 1.7         0.4  setosa

Of any nonconsecutive number of columns by:

n <- c(1,3,5)
colnames(df)[n] <- sprintf("x%s",n)

#   x1 Sepal.Width  x3  Petal.Width     x5
# 1 5.1         3.5 1.4         0.2 setosa
# 2 4.9         3.0 1.4         0.2 setosa
# 3 4.7         3.2 1.3         0.2 setosa
# 4 4.6         3.1 1.5         0.2 setosa
# 5 5.0         3.6 1.4         0.2 setosa
# 6 5.4         3.9 1.7         0.4 setosa
  • Related