【问题标题】:Rename special character / symbols from all variables in the dataset重命名数据集中所有变量的特殊字符/符号
【发布时间】:2018-12-25 01:49:41
【问题描述】:

我正在尝试更改所有数据集以替换特殊符号和字符,但我想使用 dplyr 来执行此操作,但我没有找到答案。 当我说“特殊”时,我指的是外语中常见的符号,例如葡萄牙语。

ds <- data.frame(group=c("american", "canadian"), 
                 queçtão.1=rnorm(n=50,mean=100,sd=15),
                 queçtão.2=rnorm(n=50, mean=1500, sd=300),
                 queçtão.18=rnorm(n=50, mean=5, sd=2))

类似

         quectao1
         quectao2
         quectão18

为什么我的问题:

一位论坛成员帮我use dplyr to gather variables and summarise its results,但它不适用于带有特殊符号的变量:

set.see(123)
ds <- data.frame(group=c("american", "canadian"), 
                 queçtão.1=rnorm(n=50,mean=100,sd=15),
                 queçtão.2=rnorm(n=50, mean=1500, sd=300),
                 queçtão.18=rnorm(n=50, mean=5, sd=2))

ds %>% 
  group_by(group) %>% 
  summarise_at(vars(queçtão.1,queçtão.2,queçtão.18),funs(mean, sd)) %>% 
  gather(key, val, queçtão.1_mean:queçtão.18_sd) %>% 
  separate(key, into = c('key1', 'key2')) %>% 
  unite(group, group, key2) %>%
  spread(group, val)

Error: Duplicate identifiers for rows (1, 7), (5, 11), (3, 9), (2, 8), (6, 12), (4, 10)
In addition: Warning message:
Expected 2 pieces. Additional pieces discarded in 12 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]. 

【问题讨论】:

  • 创建一列以使观察结果不同,然后重试
  • @Hack-R,当然!我改变了文字。谢谢
  • 您好,@Sotos,谢谢,您能帮我更改数据集中的变量名称吗?我不知道如何使观察结果与众不同。
  • 看看this link

标签: r dplyr special-characters


【解决方案1】:

如果您在这里并想查看此问题的解决方案,我有两个选择。

首先,使用 dplyr:

ds <- ds %>% setNames(tolower(gsub("\\.","",names(.)))) %>% 
   setNames(tolower(gsub("\\_","",names(.)))) %>% 
   setNames(tolower(gsub("ç","c",names(.)))) %>% 
   setNames(tolower(gsub("ã","a",names(.))))

二、使用看门人包

library(janitor)
ds <- ds %>% clean_names()

这个社区是寻找我们问题答案的好地方,我希望我的回答可以帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 2019-09-21
    • 2021-05-10
    相关资源
    最近更新 更多