[英]Match a vector element to a character in R?
so basically what Im trying to figure out is - is there a way to match a character to a numerical vector element.所以基本上我想弄清楚的是 - 有没有办法将字符与数字向量元素匹配。 The examples below will hopefully explain what I mean.
下面的例子有望解释我的意思。
If I have some numerical vectors:如果我有一些数值向量:
numVec <- c(1,2,3,4,5,6,7,8,9)
numVec_2 <- c(1,1,1,2,3,3,4,5,5,5,6,6,7,8,8,9)
And I have a character vector:我有一个字符向量:
words <- c("The", "man", "went", "to", "the", "store", "and", "bought", "bread")
Im trying to match the word to the corresponding number in the numerical vector... The result im trying to achieve would look something like this for numVec
:我试图将单词与数值向量中的相应数字匹配......我试图实现的结果对于
numVec
看起来像这样:
[1] "The" "man" "went" "to" "the" "store" "and" "bought" "bread"
And the result for numVec_2
would look like this: numVec_2
的结果如下所示:
[1] "The" "The" "The" "man" "went" "went" "to" "the" "the" "the" "store" "store" "and" "bought" "bought" "bread"
Im kind of stumped on this one... Any suggestion as to how I would do this?我有点难倒这个......关于我将如何做到这一点的任何建议?
It is an indexing task as @markus said:正如@markus 所说,这是一项索引任务:
words[numVec]
[1] "The" "man" "went" "to" "the" "store" "and" "bought" "bread"
words[numVec_2]
[1] "The" "The" "The" "man" "went" "went" "to" "the" "the" "the" "store"
[12] "store" "and" "bought" "bought" "bread"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.