Rename special character / symbols from all variab

2019-07-31 11:17发布

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.

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:

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条回答
等我变得足够好
2楼-- · 2019-07-31 12:09

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

First, using 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.

查看更多
登录 后发表回答