Kevin Lee
Kevin Lee

Reputation: 351

How was the "sentences" vector made? (creating vectors of strings)

I'm trying to replicate the format of the "sentences" object from the rcorpora package but I'm a little confused as to how it was made.

The sentences object is a vector of 720 sentences, but I am confused as to the exact structure and how it was made.

based on

is.list(sentences) returning FALSE

is.vector(sentences) returning TRUE

is.character(sentences) returning TRUE

I sumrize that the object sentences is a vector and not a list.

Each sentence is on a different line, with a different number as shown:

> head(sentences)
[1] "The birch canoe slid on the smooth planks." 
[2] "Glue the sheet to the dark blue background."
[3] "It's easy to tell the depth of a well."     
[4] "These days a chicken leg is a rare dish."   
[5] "Rice is often served in round bowls."       
[6] "The juice of lemons makes fine punch." 

but when i try to construct my own version of sentences like so

sentence2 <- c("This is the first sentence.\n", "This is the second sentence"

or

sentence3 <- c("This is the first sentence. \n This is the second sentence")

I do not get the same result.

How was the sentence object/vector created such that each sentence is on a different line and with a different number?

Upvotes: 0

Views: 57

Answers (2)

akrun
akrun

Reputation: 887183

If we want to replicate use dput which returns the structure of the object so that we don't have to manually create a vector (here)

dput(sentences)

Upvotes: 1

Count Orlok
Count Orlok

Reputation: 1007

sentences is just a vector of strings. They print as one-per-line because the individual sentences are too long for R to fit them both on the same line given your current console width. Try making your console wider, and you'll see that they print as multiple sentences per one line. Below is a screenshot from my RStudio:

enter image description here

To replicate that with sentences of your own, just do:

sentences2 <- c("This is my own sentence.", "This is another one of my own sentences.")
sentences2

# [1] "This is my own sentence."                
# [2] "This is another one of my own sentences."

Upvotes: 2

Related Questions