简体   繁体   English

将向量元素与 R 中的字符匹配?

[英]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.

相关问题 为 R 中的字符向量中的每个元素匹配 substring - Match a substring for each element in a character vector in R 在R中按元素拆分字符向量? - Split character vector by element in R? 子集 R 按子列表元素与字符向量的部分字符串匹配列出,使用基数 R - Subset R list by partial string match of sublist element against character vector, using base R R - 使用 grep 和 gsub 在同一(字符)向量元素中返回多个匹配项 - R - Using grep and gsub to return more than one match in the same (character) vector element 在R中有条理地删除向量元素的字符 - Conditionally Remove Character of a Vector Element in R 将字符向量的每个元素与第二个向量r的所有元素连接起来 - concatenate each element of character vector with all elements of a second vector r 比较是否较短的字符向量与R中较长的字符向量以确定任何匹配(以及哪个匹配) - Compare if a shorter character vector to a longer in R to determine any match (and which) 条件字符串匹配R字符向量折叠选择元素 - Conditional String Match R Character Vector Collapse Select Elements R:将字符向量与数据帧中的文本描述匹配并返回值 - R: Match a character vector to text description in dataframe and return value 确定字符向量中与匹配向量匹配的最大匹配数的位置[R] - Determine the location(s) in character vector that match maximum number of matches from a match vector [R]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM