[R] word frequency count

mail me mailme842 at googlemail.com
Sun Mar 18 20:07:27 CET 2012


Hi:
Thanks for reply. I am using the following statement

res <- with(df, table(paste(item1, item2, sep=', ')) )

to get the frequency counts of the rows, which gives the following output:
milk,bread 2
bread,butter 1
beer,diaper 3
milk,bread 2

But I need to extract from the above result two vectors or dataframes
(such as DF1 and DF2) to make the final output as below:

DF1
milk,bread
bread,butter
beer,diaper
milk,bread

DF2
2
1
3
2

Can anyone help? Thanks in advance!




On Sun, Mar 18, 2012 at 4:22 PM, S Ellison <S.Ellison at lgcgroup.com> wrote:
> You could do try
> with(df, table(item1:item2) )
> or
> with(df, table(paste(item1, item2, sep=', ')) )
>
> If the order is immaterial, so that (milk, bread) is the same as (bread, milk), there's a bit more work to do. Maybe
>
> table( apply(df, 1, function(x) paste(sort(x))) )
>
> ________________________________________
> From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of mail me [mailme842 at googlemail.com]
> Sent: 18 March 2012 13:31
> To: r-help
> Subject: Re: [R] word frequency count
>
> Hi:
>
> Suppose I create the dataframe df using the following code:
>
> df <- data.frame( item1 = c('milk',
> 'bread','beer','beer','milk','beer'), item2 =c('bread',
> 'butter','diaper','diaper','bread', 'diaper'), stringsAsFactors = F);
>
>
> df
>
>  item1  item2
> 1  milk  bread
> 2 bread butter
> 3  beer diaper
> 4  beer diaper
> 5  milk  bread
> 6  beer diaper
>
> And now i want the following output:milk,bread   2
> bread,butter 1
> beer,diaper  3
> milk,bread   2

>
> >
> and "milk,bread" is a single datum. I hope this clarifies the problem!
>
> Thanks!
>
>
>
> On 3/18/12, John Kane <jrkrideau at inbox.com> wrote:
>> ? table
>>
>> First however confirm "that milk,bread" is a single datum. str() should do
>> this
>>
>> Can you post a sample of the data here using dput()?
>>
>> John Kane
>> Kingston ON Canada
>>
>>
>>> -----Original Message-----
>>> From: mailme842 at googlemail.com
>>> Sent: Sun, 18 Mar 2012 13:12:48 +0200
>>> To: r-help at r-project.org
>>> Subject: [R] word frequency count
>>>
>>> Hi:
>>>
>>> I have a dataframe containing comma seperated group of words such as
>>>
>>> milk,bread
>>> bread,butter
>>> beer,diaper
>>> beer,diaper
>>> milk,bread
>>> beer,diaper
>>>
>>> I want to output the frequency of occurrence of comma separated words
>>> for each row and collapse duplicate rows, to make the output as shown
>>> in the following dataframe:
>>>
>>> milk,bread   2
>>> bread,butter 1
>>> beer,diaper  3
>>> milk,bread   2
>>>
>>> Thanks for help!
>>>
>>> deb
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ____________________________________________________________
>> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your
>> desktop!
>> Check it out at http://www.inbox.com/marineaquarium
>>
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:8}}



More information about the R-help mailing list