【问题标题】:How was the "sentences" vector made? (creating vectors of strings)“句子”矢量是如何制作的? (创建字符串向量)
【发布时间】:2020-07-04 22:12:41
【问题描述】:

我正在尝试从 rcorpora 包中复制“句子”对象的格式,但我对它是如何制作的有点困惑。

sentences 对象是一个包含 720 个句子的向量,但我对确切的结构和它是如何制作的感到困惑。

基于

is.list(sentences) returning FALSE

is.vector(sentences) returning TRUE

is.character(sentences) returning TRUE

我总结的对象句子是一个向量而不是一个列表。

每个句子都在不同的行,如图所示:

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

但是当我尝试像这样构建自己的句子版本时

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

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

我没有得到相同的结果。

如何创建句子对象/向量以使每个句子位于不同的行并具有不同的编号?

【问题讨论】:

    标签: r string


    【解决方案1】:

    sentences 只是一个字符串向量。它们打印为每行一个,因为在给定当前控制台宽度的情况下,单个句子对于 R 来说太长而无法将它们都放在同一行上。尝试使您的控制台更宽,您会看到它们每行打印多个句子。下面是我的 RStudio 的截图:

    要用你自己的句子来复制它,只需这样做:

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

    【讨论】:

      【解决方案2】:

      如果我们想复制使用dput,它会返回对象的结构,这样我们就不必手动创建vector(这里)

      dput(sentences)
      

      【讨论】:

        猜你喜欢
        • 2012-05-16
        • 1970-01-01
        • 2019-08-04
        • 1970-01-01
        • 2012-06-13
        • 2012-09-24
        • 2021-03-29
        • 2022-01-10
        • 1970-01-01
        相关资源
        最近更新 更多