[R] count combined occurrences of categories
David Winsemius
dwinsemius at comcast.net
Fri Jan 11 18:20:10 CET 2013
On Jan 11, 2013, at 2:54 AM, Biau David wrote:
> 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))
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
--
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list