dataframe - R count column for each element, no matter redundancy -


sorry if i'm asking obvious, couldn't find similar.

suppose have data:

a<-c('blue','blue','green','red','black',      'white','blue','blue','blue','red',      'black','white','blue','green','red',      'black','white','white','black','white',      'blue','white','blue','green') 

and have in data frame, column summarizes number of times each element appears in whole vector, no matter if it's redundant. this:

data.frame(a=c('blue','blue','green','red',                'black','white','blue','blue',                'blue','red','black','white',                'blue','green','red','black',                'white','white','black','white',                'blue','white','blue','green'),            b=c(8,8,3,3,4,6,8,8,8,3,4,                6,8,3,3,4,6,6,4,6,8,6,8,3)) 

any appreciated.

in spirit of question linked mnel, here's how ave:

data.frame(a, b=ave(seq_along(a), a, fun=length))        b 1   blue 8 2   blue 8 3  green 3 4    red 3 5  black 4 6  white 6 7   blue 8 8   blue 8 9   blue 8 10   red 3 11 black 4 12 white 6 13  blue 8 14 green 3 15   red 3 16 black 4 17 white 6 18 white 6 19 black 4 20 white 6 21  blue 8 22 white 6 23  blue 8 24 green 3 

this uses bogus numeric vector, , takes length each value.

it might make more sense take vector of 1's , take sum:

data.frame(a, b=ave(rep(1, length(a)), a, fun=sum)) 

the result same.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -