我有Excel表格中的数据,我需要一种方法来清理它.我想删除不一致的值,如分支名称被指定为(计算机科学与工程,CSE,CS,计算机科学).那么我怎样才能将它们全部带入单一符号?
汽车包装有一个recode功能.查看工作示例的帮助页面.
事实上可以说这应该是一个封闭的问题:
还有一些问题可以通过搜索轻松识别:[r]重新编码
编辑: 我非常喜欢Marek的评论,我决定制作一个实现它的功能.(因素一直是我的R-traps之一,他的方法似乎非常直观.)该函数旨在获取字符或因子类输入并返回分组结果,该结果也对"all_others"级别进行分类.
my_recode <- function(fac, levslist){ nfac <- factor(fac);
inlevs <- levels(nfac);
othrlevs <- inlevs[ !inlevs %in% unlist(levslist) ]
# levslist of the form :::: list(
# animal = c("cow", "pig"),
# bird = c("eagle", "pigeon") )
levels(nfac)<- c(levslist, all_others =othrlevs); nfac}
df <- data.frame(name = c('cow','pig','eagle','pigeon', "zebra"),
stringsAsFactors = FALSE)
df$type <- my_recode(df$name, list(
animal = c("cow", "pig"),
bird = c("eagle", "pigeon") ) )
df
#-----------
name type
1 cow animal
2 pig animal
3 eagle bird
4 pigeon bird
5 zebra all_others
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3038 次 |
| 最近记录: |