循环使用数字重命名变量

问题描述

我有一些名为xa_1,xa_2,...,xa_n的变量,希望通过某些方式进行更改。

  1. 我想创建一个名为xa_n_的新变量,我将在这些变量上执行
  2. 然后我要重新编码这些(分类)变量的值,例如反转李克特量表

我尝试了以下操作:

for(i in c(xa_1,xa_2,...,xa_n) {
df$[i]_ <- df$[i] # I am sure I need to use paste() in some ways but I have been unable to figure it out
}

我尝试不成功:

a <- 1:n
for (i in seq_along(a)) {
df$paste(xa_,[i]_,sep="") <- df$paste(xa_,[i],sep="")
}

如您所见,我在基本功能和对R中的数据操作的理解上存在问题。简而言之,我想通过结合其名称的两个功能(即它们的共同核心(xa_)和他们的识别号码(1,..,n)。

将所有单独的部分放在一起,我最终想知道的是一种执行以下操作的方法

for(i in c(xa_) {
for( j in 1:4) {
df$paste([i],[j]_,sep="") <- df$paste([i],[j],sep=") # Create a copy of existing variables that runs across variables and numbers
df$paste([i],sep="") <- ifelse(df$paste([i],sep=") == 1,4,ifelse(df$paste([i],sep=") == 2,3,sep=")== 3,2,sep=") == 4,1,NA)))) # recode the new variables based on values in old variables
df$paste([i],_,sep="") <- factor(paste([i],sep=""),levels=c(1:4),labels=c("Something","Something else","Something else again","Something else at last")) # Rename the values in the new variables and make them categorical 
}

希望这很有道理,以便您理解我的意思:)

解决方法

如何像这样dplyr进行操作?

library(dplyr)
df %>% 
  rename_with(
    ~paste0(.,"_"),starts_with("xa")
  ) %>% 
  mutate(across(
    starts_with("xa"),~factor(.,4:1,c("Something","Something else","Something else again","Something at last"))
  ))

假设您的数据框如下所示:

> df
# A tibble: 10 x 6
       y     z ttt       xa_1  xa_2  xa_3
   <dbl> <dbl> <chr>    <int> <int> <int>
 1     1     2 a string     4     2     4
 2     1     2 a string     3    NA     1
 3     1     2 a string     4     1    NA
 4     1     2 a string     1    NA    NA
 5     1     2 a string    NA     1     3
 6     1     2 a string     3     2     2
 7     1     2 a string     2    NA     1
 8     1     2 a string    NA     4    NA
 9     1     2 a string     1     3    NA
10     1     2 a string     2     1     3

上面的代码为您提供:

> df %>% rename_with(~paste0(.,starts_with("xa")) %>% mutate(across(starts_with("xa"),"Something at last"))))
# A tibble: 10 x 6
       y     z ttt      xa_1_                xa_2_                xa_3_               
   <dbl> <dbl> <chr>    <fct>                <fct>                <fct>               
 1     1     2 a string Something            Something else again Something           
 2     1     2 a string Something else       NA                   Something at last   
 3     1     2 a string Something            Something at last    NA                  
 4     1     2 a string Something at last    NA                   NA                  
 5     1     2 a string NA                   Something at last    Something else      
 6     1     2 a string Something else       Something else again Something else again
 7     1     2 a string Something else again NA                   Something at last   
 8     1     2 a string NA                   Something            NA                  
 9     1     2 a string Something at last    Something else       NA                  
10     1     2 a string Something else again Something at last    Something else   

如果您还想保留这些变量,请尝试以下代码:

df %>% 
  mutate(across(
    starts_with("xa"),~.,.names = "{.col}_"
  )) %>% 
  mutate(across(
    starts_with("xa") & ends_with("_"),"Something at last"))
  ))

输出:

# A tibble: 10 x 9
       y     z ttt       xa_1  xa_2  xa_3 xa_1_                xa_2_                xa_3_               
   <dbl> <dbl> <chr>    <int> <int> <int> <fct>                <fct>                <fct>               
 1     1     2 a string     4     2     4 Something            Something else again Something           
 2     1     2 a string     3    NA     1 Something else       NA                   Something at last   
 3     1     2 a string     4     1    NA Something            Something at last    NA                  
 4     1     2 a string     1    NA    NA Something at last    NA                   NA                  
 5     1     2 a string    NA     1     3 NA                   Something at last    Something else      
 6     1     2 a string     3     2     2 Something else       Something else again Something else again
 7     1     2 a string     2    NA     1 Something else again NA                   Something at last   
 8     1     2 a string    NA     4    NA NA                   Something            NA                  
 9     1     2 a string     1     3    NA Something at last    Something else       NA                  
10     1     2 a string     2     1     3 Something else again Something at last    Something else