Skip to content

count combined occurrences of categories

8 messages · Pascal Oettli, PIKAL Petr, arun +2 more

#
Hello,

Maybe you can try something like that:
count.au1 <- tapply(tutu$au1, tutu$nam, table)
count.au2 <- tapply(tutu$au2, tutu$nam, table)
count.au3 <- tapply(tutu$au3, tutu$nam, table)

HTH,
Pascal

Le 11/01/2013 19:54, Biau David a ?crit :
#
Hi

structure of your data frame is not suitable for this task. 

library(reshape)
tutu.m<-melt(tutu, "nam")
table(tutu.m$nam, tutu.m$value)
    
     art deb joy mar seb lio nem tat
  da   2   3   1   4   1   1   0   0
  fr   2   2   2   3   0   1   1   1
  ya   1   2   1   0   0   1   1   0

Regards
Petr
#
HI,
You could try this:
library(reshape2)


?dcast(melt(tutu,"nam"),nam~value,length)
#? nam art deb joy mar seb lio nem tat
#1? da?? 2?? 3?? 1?? 4?? 1?? 1?? 0?? 0
#2? fr?? 2?? 2?? 2?? 3?? 0?? 1?? 1?? 1
#3? ya?? 1?? 2?? 1?? 0?? 0?? 1?? 1?? 0
A.K.



----- Original Message -----
From: Biau David <djmbiau at yahoo.fr>
To: r help list <r-help at r-project.org>
Cc: 
Sent: Friday, January 11, 2013 5:54 AM
Subject: [R] count combined occurrences of categories

Dear all,
?
i would like to count the number of times where I have combined occurrences of the categories of 2 variables.
?
For instance, in the dataframe below, i would like to know how many times each author (au1, au2, au3 represent the first, second, third author) is associated with each of the category of the variable 'nam'. The position of the author does not matter.
?
nam <- c('da', 'ya', 'da', 'da', 'fr', 'fr', 'fr', 'da', 'ya', 'fr')
au1 <- c('deb', 'art', 'deb', 'seb', 'deb', 'deb', 'mar', 'mar', 'joy', 'joy')
au2 <- c('art', 'deb', 'mar', 'deb', 'joy', 'mar', 'art', 'lio', 'nem', 'mar')
au3 <- c('mar', 'lio', 'joy', 'mar', 'art', 'lio', 'nem', 'art', 'deb', 'tat')
tutu <- data.frame(cbind(nam, au1, au2, au3))
?
thanks,

David
??? [[alternative HTML version deleted]]


______________________________________________
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.
#
On Jan 11, 2013, at 2:54 AM, Biau David wrote:

            
You should first abandon the practice of using `cbind` inside  
`data.frame`. Obscure errors will plague your R experience until you  
do so.

Bas solution:

 > tutus <- data.frame(nam=tutu$nam, au=with(tutu, c(au1,au2,au3)))
 > tutab <- with(tutus, table(nam, au)  )
 > tutab
     au
nam  1 2 3 4 5 6 7
   da 2 3 1 2 4 0 0
   fr 2 2 2 2 2 1 1
   ya 1 2 1 1 0 1 0
#
HI David,

I get different results with dcast()

library(reshape2)
? dcast(melt(tutu,"nam"),nam~value,length)
#? nam art deb joy mar seb lio nem tat
#1? da?? 2?? 3?? 1?? 4?? 1?? 1?? 0?? 0
#2? fr?? 2?? 2?? 2?? 3?? 0?? 1?? 1?? 1
#3? ya?? 1?? 2?? 1?? 0?? 0?? 1?? 1?? 0

?tutus <- data.frame(nam=tutu$nam, au=with(tutu, c(au1,au2,au3)))
?with(tutus,table(nam,au))
#??? au
#nam? 1 2 3 4 5 6 7
?# da 2 3 1 2 4 0 0?? #some numbers don't match the previous result
? #fr 2 2 2 2 2 1 1
? #ya 1 2 1 1 0 1 0
#If I convert to as.character(), it matched with the dcast() results

tutunew<-data.frame(nam=tutu$nam,au=with(tutu,c(as.character(au1),as.character(au2),as.character(au3))))
with(tutunew,table(nam,au))
#??? au
#nam? art deb joy lio mar nem seb tat
?# da?? 2?? 3?? 1?? 1?? 4?? 0?? 1?? 0
? #fr?? 2?? 2?? 2?? 1?? 3?? 1?? 0?? 1
? #ya?? 1?? 2?? 1?? 1?? 0?? 1?? 0?? 0
A.K.





----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: Biau David <djmbiau at yahoo.fr>
Cc: r help list <r-help at r-project.org>
Sent: Friday, January 11, 2013 12:20 PM
Subject: Re: [R] count combined occurrences of categories
On Jan 11, 2013, at 2:54 AM, Biau David wrote:

            
You should first abandon the practice of using `cbind` inside `data.frame`. Obscure errors will plague your R experience until you do so.

Bas solution:
? ? au
nam? 1 2 3 4 5 6 7
? da 2 3 1 2 4 0 0
? fr 2 2 2 2 2 1 1
? ya 1 2 1 1 0 1 0

--
David Winsemius, MD
Alameda, CA, USA

______________________________________________
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.
#
On Jan 11, 2013, at 9:47 AM, arun wrote:

            
Probably due to the fact I used c() on factors:

tutu <- data.frame(nam, au1, au2, au3, stringsAsFactors=FALSE)
 > tutus <- data.frame(nam=tutu$nam, au=with(tutu, c(au1,au2,au3)))
 > tutab <- with(tutus, table(nam, au)  )
 > tutab
     au
nam  art deb joy lio mar nem seb tat
   da   2   3   1   1   4   0   1   0
   fr   2   2   2   1   3   1   0   1
   ya   1   2   1   1   0   1   0   0

--  
David.
David Winsemius, MD
Alameda, CA, USA
1 day later