【发布时间】:2019-08-13 16:56:29
【问题描述】:
我正在寻找一个函数,它允许我添加一个新列来将称为 ID 的值添加到一个字符串中,即:
我有一个带有你 ID 的单词列表:
car = 9112
red = 9512
employee = 6117
sky = 2324
words<- c("car", "sky", "red", "employee", "domestic")
match<- c("car", "red", "domestic", "employee", "sky")
比较是通过读取一个excel文件进行的,如果它发现值等于我的向量单词,它会用它的ID替换单词,但保留原始单词
x10<- c(words)# string
words.corpus <- c(L4$`match`) # pattern
idwords.corpus <- c(L4$`ID`) # replace
words.corpus <- paste0("\\A",idwords.corpus, "\\z|\\A", words.corpus,"\\z")
vect.corpus <- idwords.corpus
names(vect.corpus) <- words.corpus
data15 <- str_replace_all(x10, vect.corpus)
结果:
数据15:
" 9112", "2324", "9512", "6117", "employee"
我正在寻找的是用 ID 添加一个新列,而不是用 ID 替换单词
words ID
car 9112
red 9512
employee 6117
sky 2324
domestic domestic
【问题讨论】:
-
很难在没有更多信息的情况下完全帮助您。但您可以使用
data.frame或data.table。data15 <- cbind(x10,ID=str_replace_all(x10, vect.corpus))