Search code examples
rstringvectortype-conversionopennlp

R. Converting a vector of type character, into a string


I am having some trouble converting a vector type object into a string.

I have tried :

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

and various different types of paste function but the return of is.String(x) is still FALSE and the return for is.vector is still TRUE. Here's my code below:

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." 

If anyone could help me convert the elements of a vector into a string I'd appreciate it.


Solution

  • a string (as defined in the sense of the NLP package) is not the same as a base-R character.

    library(NLP)
    xchar <- "abc"
    xstring <- as.String("abc")
    
    > xchar
    [1] "abc"
    > xstring
    abc
    

    From this you can already see one of the differences of NLP stings and base R characters, namely the printing properties. Also:

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

    So if you want a String object you should use as.String instead of as.character