English 中文(简体)
R:将频率清单与不同长度的标签结合起来?
原标题:R: combining frequency lists with different lengths by labels?

我对R说了一个新东西,但真的喜欢,希望不断改善。 现在,在寻找一个时间之后,我需要请你提供帮助。

This is the given case:

(1) 我有句子(第1句和第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)

These are the results:

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

因此,这种“表”应当是“灵活”的,这样,如果加上“但”和“但”之间的“和”标签,表格就添加该栏。

我认为,在新的一行中只增加新的句子,将所有尚未列入清单一栏的字句(这里,“和”是“你”的权利)重新分类。 然而,我没有这样做,因为根据现有标签的频率对新句进行分类(例如,如果再次出现“伤.”,新句的汽车频率应写进新的句子和“伤.”一栏,但如存在的话。 第1次,其频率应写入新的句子和一个新的栏目,称为“青年”。

问题回答

页: 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

然后,你可以使用<条码>,复制,添加更多的句子。 我将专栏名称改为简明,但还有其他选择。 使用<代码>by.x和by.y的论点,而不是在<代码>上<>逐>>/代码>,可在每一数据框架中注明姓名与否的具体栏目。 此外,<代码>suffix 的论点载于, 即,将控制计票栏如何获得独特名称。 缺省是<代码>.x和.y,但可以改动。





相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签