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='')

                                        - Phil Spector
                                         Statistical Computing Facility
                                         Department of Statistics
                                         UC Berkeley
                                         spec...@stat.berkeley.edu



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@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@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.

Reply via email to