R.将字符类型的向量转换为字符串

时间:2018-06-28 09:44:31

标签: r string vector type-conversion opennlp

将向量类型的对象转换为字符串时遇到麻烦。

我已经尝试过:

x <- paste(x, sep = " ", collapse = NULL) 

和各种不同类型的粘贴函数,但是is.String(x)的返回仍然是FALSE,而is.vector的返回仍然是TRUE。这是我的代码如下:

bio_sentences <- sent_detect(bio) #Using openNLP to get the sentences from a bio
is.vector(bio_sentences) #Returns TRUE
sentisimo <- bio_sentences[1] #Needed as I want to do analysis sentence by sentence
sentisimo <- paste(sentisimo, sep = " ", collapse = NULL)
as.character(sentisimo) 
is.vector(sentisimo) #Returns TRUE
is.character(sentisimo) #Returns TRUE
sentisimo <- paste(bio_sentences[1], sep = "")
as.String(sentisimo)
is.String(sentisimo) #Returns FALSE
str(sentisimo) Returns chr "1st sentence of the bio"
dput(sentisimo) #Returns "Dennis Muilenburg is chairman of the board, president and chief executive officer of The Boeing Company." 

如果有人可以帮助我将向量的元素转换为字符串,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

字符串(按照NLP软件包的定义)与base-R字符不同。

library(NLP)
xchar <- "abc"
xstring <- as.String("abc")

> xchar
[1] "abc"
> xstring
abc

由此您已经可以看到NLP字符串和基本R字符的区别之一,即打印属性。 另外:

> is.character(xstring)
[1] TRUE
> is.String(xstring)
[1] TRUE
> is.character(xchar)
[1] TRUE
> is.String(xchar)
[1] FALSE

因此,如果要使用String对象,则应使用as.String而不是as.character