R. 将字符类型的向量转换为字符串
R. Converting a vector of type character, into a string
我在将矢量类型对象转换为字符串时遇到一些问题。
我试过了:
x <- paste(x, sep = " ", collapse = NULL)
和各种不同类型的粘贴功能,但 is.String(x)
的 return 仍然是 FALSE
并且 is.vector
的 return 仍然是 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."
如果有人能帮助我将向量的元素转换为字符串,我将不胜感激。
字符串(在 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
所以如果你想要一个字符串对象,你应该使用 as.String
而不是 as.character
我在将矢量类型对象转换为字符串时遇到一些问题。
我试过了:
x <- paste(x, sep = " ", collapse = NULL)
和各种不同类型的粘贴功能,但 is.String(x)
的 return 仍然是 FALSE
并且 is.vector
的 return 仍然是 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."
如果有人能帮助我将向量的元素转换为字符串,我将不胜感激。
字符串(在 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
所以如果你想要一个字符串对象,你应该使用 as.String
而不是 as.character