【问题标题】:R: combining frequency lists with different lengths by labels?R:通过标签组合不同长度的频率列表?
【发布时间】:2012-02-08 14:13:53
【问题描述】:

我是 R 的新手,但我真的很喜欢它并希望不断改进。现在,经过一段时间的搜索,我需要向您寻求帮助。

这是给定的情况:

1) 我有句子(sentence.1 和 sentence.2 - 所有单词都已经小写)并创建它们单词的排序频率列表:

sentence.1 <- "bob buys this car, although his old car is still fine." # saves the sentence into sentence.1
sentence.2 <- "a car can cost you very much per month."

sentence.1.list <- strsplit(sentence.1, "\\W+", perl=T) #(I have these following commands thanks to Stefan Gries) we split the sentence at non-word characters
sentence.2.list <- strsplit(sentence.2, "\\W+", perl=T)

sentence.1.vector <- unlist(sentence.1.list) # then we create a vector of the list
sentence.2.vector <- unlist(sentence.2.list) # vectorizes the list

sentence.1.freq <- table(sentence.1.vector) # and finally create the frequency lists for 
sentence.2.freq <- table(sentence.2.vector)

这些是结果:

sentence.1.freq:
although      bob     buys      car     fine      his       is      old    still     this 
       1        1        1        2        1        1        1        1        1        1

sentence.2.freq:
a   can   car  cost month  much   per  very   you 
1     1     1     1     1     1     1     1     1 

现在,请问如何将这两个频率列表结合起来,我将拥有以下内容:

 a  although  bob  buys  can  car  cost fine his  is  month much old per still this very you
NA         1    1     1   NA    2    NA    1   1   1     NA   NA   1  NA     1    1   NA  NA
 1        NA   NA    NA    1    1     1   NA  NA  NA      1    1  NA   1    NA   NA    1   1

因此,这个“表格”应该是“灵活的”,以便在输入一个新句子的情况下,例如“and”,表格将在“a”和“although”之间添加带有“and”标签的列。

我想只是将新句子添加到新行中,然后将所有尚未列在列表中的单词按列(这里,“and”将在“you”的右侧)并再次对列表进行排序.但是,我没有管理这个,因为根据现有标签对新句子单词频率的排序已经不起作用(当再次出现“car”时,应该将新句子的 car 频率写入新句子的行和“汽车”的列,但是当第一次出现例如“你”时,它的频率应该写入新句子的行和标记为“你”的新列)。

【问题讨论】:

    标签: r list frequency words


    【解决方案1】:

    这不是完全您所描述的,但是您的目标对我来说按行而不是按列组织更有意义(并且 R 处理以这种方式组织的数据多一点无论如何都很容易)。

    #Convert tables to data frames
    a1 <- as.data.frame(sentence.1.freq)
    a2 <- as.data.frame(sentence.2.freq)
    
    #There are other options here, see note below
    colnames(a1) <- colnames(a2) <- c('word','freq')
    #Then merge
    merge(a1,a2,by = "word",all = TRUE)
           word freq.x freq.y
    1  although      1     NA
    2       bob      1     NA
    3      buys      1     NA
    4       car      2      1
    5      fine      1     NA
    6       his      1     NA
    7        is      1     NA
    8       old      1     NA
    9     still      1     NA
    10     this      1     NA
    11        a     NA      1
    12      can     NA      1
    13     cost     NA      1
    14    month     NA      1
    15     much     NA      1
    16      per     NA      1
    17     very     NA      1
    18      you     NA      1
    

    然后您可以继续使用merge 添加更多句子。为简单起见,我转换了列名,但还有其他选项。使用by.xby.y 参数而不是merge 中的by 可以指示如果每个数据帧中的名称不同,则特定列合​​并。此外,merge 中的 suffix 参数将控制如何为计数列赋予唯一名称。默认是附加.x.y,但您可以更改它。

    【讨论】:

    • 乔兰,你拯救了我的一天!这就是我所需要的,它对我来说看起来很完美。谢谢!!!
    猜你喜欢
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多