[R] adding a frequency variable to a data frame

2014-04-11 Thread Michael Friendly
I'm sure this is pretty simple, but it's Friday afternoon, and I just 
don't see it...


In a data frame with a categorical/character factor, I want to add another
column giving, for each observation, the frequency of that factor level.

An example, where the variable of interest is family:

 data(Donner, package=vcdExtra)
 str(Donner)
'data.frame':   90 obs. of  5 variables:
 $ family  : Factor w/ 10 levels Breen,Donner,..: 9 1 1 1 1 1 1 1 1 
1 ...

 $ age : int  23 13 1 5 14 40 51 9 3 8 ...
 $ sex : Factor w/ 2 levels Female,Male: 2 2 1 2 2 1 2 2 2 2 ...
 $ survived: int  0 1 1 1 1 1 1 1 1 1 ...
 $ death   : POSIXct, format: 1846-12-29 NA ...
 table(Donner$family)

BreenDonner  Eddy  FosdWolfGraves  Keseberg McCutchen 
MurFosPik

914 4 410 4 312
Other  Reed
   23 7


Here, I want to create a new variable, family.size,   where all the 
Breens have 9,

the Donners, 14,  and so on...

--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept.  Chair, Quantitative Methods
York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

__
R-help@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.


Re: [R] adding a frequency variable to a data frame

2014-04-11 Thread Andrija Djurovic
Hi. Here is one solution:

table(Donner$family)[Donner$family]

Andrija


On Fri, Apr 11, 2014 at 10:40 PM, Michael Friendly frien...@yorku.cawrote:

 I'm sure this is pretty simple, but it's Friday afternoon, and I just
 don't see it...

 In a data frame with a categorical/character factor, I want to add another
 column giving, for each observation, the frequency of that factor level.

 An example, where the variable of interest is family:

  data(Donner, package=vcdExtra)
  str(Donner)
 'data.frame':   90 obs. of  5 variables:
  $ family  : Factor w/ 10 levels Breen,Donner,..: 9 1 1 1 1 1 1 1 1 1
 ...
  $ age : int  23 13 1 5 14 40 51 9 3 8 ...
  $ sex : Factor w/ 2 levels Female,Male: 2 2 1 2 2 1 2 2 2 2 ...
  $ survived: int  0 1 1 1 1 1 1 1 1 1 ...
  $ death   : POSIXct, format: 1846-12-29 NA ...
  table(Donner$family)

 BreenDonner  Eddy  FosdWolfGraves  Keseberg McCutchen
 MurFosPik
 914 4 410 4 312
 Other  Reed
23 7
 

 Here, I want to create a new variable, family.size,   where all the Breens
 have 9,
 the Donners, 14,  and so on...

 --
 Michael Friendly Email: friendly AT yorku DOT ca
 Professor, Psychology Dept.  Chair, Quantitative Methods
 York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
 4700 Keele StreetWeb:   http://www.datavis.ca
 Toronto, ONT  M3J 1P3 CANADA

 __
 R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] adding a frequency variable to a data frame

2014-04-11 Thread Andrija Djurovic
another:

ave(as.numeric(Donner$family), Donner$family, FUN=length)


On Fri, Apr 11, 2014 at 10:51 PM, Andrija Djurovic djandr...@gmail.comwrote:

 Hi. Here is one solution:

 table(Donner$family)[Donner$family]

 Andrija


 On Fri, Apr 11, 2014 at 10:40 PM, Michael Friendly frien...@yorku.cawrote:

 I'm sure this is pretty simple, but it's Friday afternoon, and I just
 don't see it...

 In a data frame with a categorical/character factor, I want to add another
 column giving, for each observation, the frequency of that factor level.

 An example, where the variable of interest is family:

  data(Donner, package=vcdExtra)
  str(Donner)
 'data.frame':   90 obs. of  5 variables:
  $ family  : Factor w/ 10 levels Breen,Donner,..: 9 1 1 1 1 1 1 1 1 1
 ...
  $ age : int  23 13 1 5 14 40 51 9 3 8 ...
  $ sex : Factor w/ 2 levels Female,Male: 2 2 1 2 2 1 2 2 2 2 ...
  $ survived: int  0 1 1 1 1 1 1 1 1 1 ...
  $ death   : POSIXct, format: 1846-12-29 NA ...
  table(Donner$family)

 BreenDonner  Eddy  FosdWolfGraves  Keseberg McCutchen
 MurFosPik
 914 4 410 4 312
 Other  Reed
23 7
 

 Here, I want to create a new variable, family.size,   where all the
 Breens have 9,
 the Donners, 14,  and so on...

 --
 Michael Friendly Email: friendly AT yorku DOT ca
 Professor, Psychology Dept.  Chair, Quantitative Methods
 York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
 4700 Keele StreetWeb:   http://www.datavis.ca
 Toronto, ONT  M3J 1P3 CANADA

 __
 R-help@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.




[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] adding a frequency variable to a data frame

2014-04-11 Thread Michael Friendly

Bingo! That's exactly the idiom I was looking for.
Thanks, Andrija.
-Michael


On 4/11/2014 4:57 PM, Andrija Djurovic wrote:

another:

ave(as.numeric(Donner$family), Donner$family, FUN=length)




--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept.  Chair, Quantitative Methods
York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

__
R-help@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.