【问题标题】:Loop to rename variables using numbers循环使用数字重命名变量
【发布时间】:2020-09-29 15:14:28
【问题描述】:

我有名为 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],[j]_, sep="") <- ifelse(df$paste([i],[j],sep=") == 1, 4, ifelse(df$paste([i],[j],sep=") == 2, 3,  ifelse(df$paste([i],[j],sep=")== 3, 2,  ifelse(df$paste([i],[j],sep=") == 4, 1, NA)))) # recode the new variables based on values in old variables
df$paste([i],[j],_, sep="") <- factor(paste([i],[j],_, 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 
}

希望这有足够的意义,以便您理解我的意思:)

【问题讨论】:

  • 您能否提供一个使用dput 输出的list/data.frame 示例?这将使您更容易掌握原始数据的外观。添加具有预期结果(仅几行)的 list/data.frame 也会更容易为您提供帮助。

标签: r loops recode


【解决方案1】:

像这样以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"), ~factor(., 4:1, c("Something", "Something else", "Something else again", "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("_"), 
    ~factor(., 4:1, c("Something", "Something else", "Something else again", "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      

【讨论】:

  • 这太完美了!有没有办法可以调整您建议的代码以将原始变量 (xa_n) 保留在同一数据框中并操纵它们的重复项 (xa_n_)?
  • 是的,有可能。 @henrik
猜你喜欢
  • 2021-08-26
  • 2019-07-05
  • 1970-01-01
  • 2021-03-31
  • 2016-09-28
  • 2016-11-13
  • 1970-01-01
  • 2015-06-09
  • 2014-06-17
相关资源
最近更新 更多