【发布时间】:2015-06-19 19:06:43
【问题描述】:
我正在开发一个遵循这个基本逻辑的程序:
- 读取 3 个 CSV 文件,其中前两个包含关键字,最后一个包含排除词。
- 将两个关键字列表合二为一,将所有关键字大写并删除所有少于 3 个字符的关键字。
- 对关键字列表中的重复关键字进行排序并删除。
- 将排除列表中的所有单词大写。
- 删除排除列表中匹配的关键字。
这是我遇到问题的第 2 步。我已经尝试了很多解决方案,但没有任何效果。这是我的代码:
# Read in individual data sets
set1=read.csv("set1.csv",header=FALSE,sep=",")
set2=read.csv("set2.csv",header=FALSE,sep=",")
exclude_list=read.csv("exclude.csv",header=FALSE,sep=",")
# Create a new set with the aggregate of all keyword sets,
# capitalizing all keywords and excluding keywords that are
# less than 2 characters in length
set_agg=rbind(set1,set2)
keywords=set_agg[c("V1")]
keywords = as.data.frame(sapply(keywords, toupper))
??? WHAT GOES HERE ???
# Sort and remove duplicate keywords from the keyword list
as.data.frame(keywords[order(keywords$V1),])
keywords=unique(keywords)
# Modify and capitalize the exclusion list
exclude_list=as.data.frame(exclude_list[c("V1")])
exclude_list=as.data.frame(sapply(exclude_list, toupper))
# Remove keywords matching the exclude list
`%ni%` <- Negate(`%in%`)
keywords=subset(keywords, V1 %ni% exclude_list$V1)
return(keywords)
作为参考,CSV 文件的格式如下:
word1,
word2,
word3,
etc...
【问题讨论】:
-
查看
toupper和nchar函数。另外:c(1,2,3)[ ! c(1,2,3) %in% c(1,2) ]是一种表达方式:给我 {1,2,3} 中不在 {1,2} 中的元素 -
@arvi1000 或
setdiff(c(1,2,3),c(1,2)) -
对,或者那个!显示上述内容,因为 OP 已经在使用
%in%