R 使用 " 将字符串转换为向量标记化" [英] R convert string to vector tokenize using " "

查看:26
本文介绍了R 使用 " 将字符串转换为向量标记化"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

string1 <- "This is my string"

我想将其转换为如下所示的向量:

I would like to convert it to a vector that looks like this:

vector1
"This"
"is"
"my"
"string"

我该怎么做?我知道我可以使用 tm 包转换为 termDocumentMatrix 然后转换为矩阵,但它会按字母顺序排列单词,我需要它们保持相同的顺序.

How do I do this? I know I could use the tm package to convert to termDocumentMatrix and then convert to a matrix but it would alphabetize the words and I need them to stay in the same order.

推荐答案

您可以使用 strsplit 来完成此任务.

You can use strsplit to accomplish this task.

string1 <- "This is my string"
strsplit(string1, " ")[[1]]
#[1] "This"   "is"     "my"     "string"

这篇关于R 使用 &quot; 将字符串转换为向量标记化&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆