我正在尝试更改所有数据集以替换特殊的符号和字符,但是我想使用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].
答案 0 :(得分: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()
这个社区是寻找我们问题的答案的好地方,希望我的回答对您有所帮助。