Re: [R] paste function by INDICES

From: Phil Spector <spector_at_stat.berkeley.edu>
Date: Wed, 27 Apr 2011 08:45:35 -0700 (PDT)

Massimiliano -

    Here's one way, assuming you wanted b to be the same length as a:

  df = transform(df,b=ave(as.character(df$a),df$ID,

                 FUN=function(a)paste(a,collapse='')))

If you just want one observation for each value of ID, you could use

      aggregate(df$a,list(ID=df$ID),paste,collapse='')

On Wed, 27 Apr 2011, Massimiliano Tripoli wrote:

> Dear all,
> I have the following R dataframe:
>
> set.seed(11)
> (df <- data.frame(ID=rep(1:10,1:10),a=factor(sample(1:4,55,rep=T)) ))
> where ID is an identification code.
> I need to create a new variable "b" in which I would paste the full group
> of "a" variable according to ID variable.
> For example for ID=2 "b" should be 13, for ID=3 "b" should be 114, and
> for ID=4 should be 1241.
> How can I do that ?
> Thank you for any advices,
> Regards,
> Massimiliano Tripoli
>
> [[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.
>



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. Received on Wed 27 Apr 2011 - 15:49:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Apr 2011 - 16:10:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive