重命名数据集中所有变量的特殊字符/符号

时间:2022-05-14 10:53:09

I'm trying to change all dataset to replace special symbols and characters, but I want to use dplyr to perform that, and I did not find an answer. When I say "special", I mean symbols typical found in foreign languages, such as Portuguese.

我正在尝试更改所有数据集以替换特殊符号和字符,但我想使用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))

to something like

喜欢的东西

         quectao1
         quectao2
         quectão18

Why my question:

为什么我的问题:

A forum member helped me to use dplyr to gather variables and summarise its results, but it's not working in variables with special symbols:

一个论坛成员帮助我使用dplyr来收集变量并总结其结果,但它不适用于带有特殊符号的变量:

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]. 

1 个解决方案

#1


0  

if you are here and want to check the solution to this questions, I have two options.

如果你在这里并且想要检查这个问题的解决方案,我有两个选择。

First, using dplyr:

首先,使用dplyr:

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

Second, using the janitor package

第二,使用看门人包

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

This community is a great place to find answers to our questions and I hope my answer could help you.

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

#1


0  

if you are here and want to check the solution to this questions, I have two options.

如果你在这里并且想要检查这个问题的解决方案,我有两个选择。

First, using dplyr:

首先,使用dplyr:

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

Second, using the janitor package

第二,使用看门人包

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

This community is a great place to find answers to our questions and I hope my answer could help you.

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