Hi All, I have a file with two columns, the first column has the names of the patients and the second column has the age. I am looking into creating an output file that looks like 1-10 10-20 etc Eric Chris Bob mat Andrew Suzan Where each column has the name of the patients in a given age category that is displayed in the header. For example in the output, the first column has the name of the patients with age between 1 to 10. The problem that I am having is that I can not use cbind since the length of the vectors is different. Is there a way to create such a file? Thanks for your help -- View this message in context: http://www.nabble.com/problem-with-cbind-tp23747075p23747075.html Sent from the R help mailing list archive at Nabble.com.
Here is one way of doing it by splitting the data and then padding everything to the same length:> x <- data.frame(pat=LETTERS, age=sample(60, 26)) > x.cut <- split(x, cut(x$age, breaks=c(1,seq(10,60,10)))) > # determine maximum number in a group and then pad the rest out to thatsize> x.max <- max(sapply(x.cut, nrow)) > x.pad <- lapply(x.cut, function(.grp){+ c(as.character(.grp$pat), rep("", x.max - nrow(.grp))) + })> # now you can do the cbind > print(do.call(cbind, x.pad), quote=FALSE)(1,10] (10,20] (20,30] (30,40] (40,50] (50,60] [1,] H E C B A K [2,] L J D F G P [3,] T V I O N R [4,] U W M X S [5,] Y Q [6,] Z> > >On Wed, May 27, 2009 at 1:27 PM, kayj <kjaja27@yahoo.com> wrote:> > Hi All, > > I have a file with two columns, the first column has the names of the > patients and the second column has the age. I am looking into creating an > output file that looks like > > 1-10 10-20 etc > Eric Chris > Bob mat > Andrew > Suzan > > > Where each column has the name of the patients in a given age category that > is displayed in the header. For example in the output, the first column has > the name of the patients with age between 1 to 10. > > The problem that I am having is that I can not use cbind since the length > of > the vectors is different. Is there a way to create such a file? > > Thanks for your help > > > > -- > View this message in context: > http://www.nabble.com/problem-with-cbind-tp23747075p23747075.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Try this where Age.Group is a factor whose levels represent the columns of out and Seq is a sequence number labeling the first Name in each Age.Group 1, the second 2 and so on.> DF <- data.frame(Name = LETTERS, Age = 1:26) > DF$Age.Group <- cut(DF$Age, seq(0, 30, 10)) > DF$Seq <- with(DF, ave(seq_along(Name), Age.Group, FUN = seq_along)) > out <- tapply(DF$Name, DF[c("Seq", "Age.Group")], paste) > out[is.na(out)] <- "" > outAge.Group Seq (0,10] (10,20] (20,30] 1 "A" "K" "U" 2 "B" "L" "V" 3 "C" "M" "W" 4 "D" "N" "X" 5 "E" "O" "Y" 6 "F" "P" "Z" 7 "G" "Q" "" 8 "H" "R" "" 9 "I" "S" "" 10 "J" "T" "" On Wed, May 27, 2009 at 1:27 PM, kayj <kjaja27 at yahoo.com> wrote:> > Hi All, > > I have a file with two columns, the first column has the names of the > patients and the second column has the age. I am looking into creating an > output file that looks like > > 1-10 ? ?10-20 ? ?etc > Eric ? ?Chris > Bob ? ? mat > ? ? ? ? ? ?Andrew > ? ? ? ? ? ?Suzan > > > Where each column has the name of the patients in a given age category that > is displayed in the header. For example in the output, the first column has > the name of the patients with age between 1 to 10. > > The problem that I am having is that I can not use cbind since the length of > the vectors is different. Is there a way to create such a file? > > Thanks for your help > > > > -- > View this message in context: http://www.nabble.com/problem-with-cbind-tp23747075p23747075.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >