Hi there, I think I'm struggling with a fairly simple problem but can't seem to solve it. I have multiple observations for one unique identifier. Ultimately I want to end up with one line per identifier with multiple observations in rows. I'm really stuck any help would be really appreciated. CAO0337134 05/09/95 CAO0337134 27/09/05 CAO0347741 10/10/04 CAO0347741 12/10/04 CAO0367128 11/07/05 CAO0367128 12/07/05 CAO0367128 14/07/05 CAO0367128 19/09/97 CAO0367128 20/09/97 CAO0374110 14/11/89 CAO0374110 17/11/89 CAO0374779 01/10/99 CAO0374779 28/09/99 CAO0374779 29/09/99 Thanks Natalie -- View this message in context: http://n4.nabble.com/Rearranging-a-data-frame-for-multiple-observations-tp1562780p1562780.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> x <- read.table(textConnection("CAO0337134 05/09/95+ CAO0337134 27/09/05 + CAO0347741 10/10/04 + CAO0347741 12/10/04 + CAO0367128 11/07/05 + CAO0367128 12/07/05 + CAO0367128 14/07/05 + CAO0367128 19/09/97 + CAO0367128 20/09/97 + CAO0374110 14/11/89 + CAO0374110 17/11/89 + CAO0374779 01/10/99 + CAO0374779 28/09/99 + CAO0374779 29/09/99"), as.is=TRUE)> closeAllConnections() > x.l <- lapply(split(x, x$V1), function(a) c(a$V2)) > # find longest length > max.len <- max(sapply(x.l, length)) > # pad and rbind > do.call(rbind, lapply(x.l, function(a){+ c(a, rep(NA, max.len - length(a))) + })) [,1] [,2] [,3] [,4] [,5] CAO0337134 "05/09/95" "27/09/05" NA NA NA CAO0347741 "10/10/04" "12/10/04" NA NA NA CAO0367128 "11/07/05" "12/07/05" "14/07/05" "19/09/97" "20/09/97" CAO0374110 "14/11/89" "17/11/89" NA NA NA CAO0374779 "01/10/99" "28/09/99" "29/09/99" NA NA> >On Sat, Feb 20, 2010 at 7:26 AM, Newbie19_02 <nvanzuydam@gmail.com> wrote:> > Hi there, > > I think I'm struggling with a fairly simple problem but can't seem to solve > it. I have multiple observations for one unique identifier. Ultimately I > want to end up with one line per identifier with multiple observations in > rows. I'm really stuck any help would be really appreciated. > > > CAO0337134 05/09/95 > CAO0337134 27/09/05 > CAO0347741 10/10/04 > CAO0347741 12/10/04 > CAO0367128 11/07/05 > CAO0367128 12/07/05 > CAO0367128 14/07/05 > CAO0367128 19/09/97 > CAO0367128 20/09/97 > CAO0374110 14/11/89 > CAO0374110 17/11/89 > CAO0374779 01/10/99 > CAO0374779 28/09/99 > CAO0374779 29/09/99 > > Thanks > Natalie > -- > View this message in context: > http://n4.nabble.com/Rearranging-a-data-frame-for-multiple-observations-tp1562780p1562780.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]]
Henrique Dallazuanna
2010-Feb-20 15:37 UTC
[R] Rearranging a data frame for multiple observations
Try this: do.call(rbind, lapply(unstack(x, V2 ~ V1), '[', 1:max(with(x, tapply(V2, V1, length))))) On Sat, Feb 20, 2010 at 9:26 AM, Newbie19_02 <nvanzuydam at gmail.com> wrote:> > Hi there, > > I think I'm struggling with a fairly simple problem but can't seem to solve > it. ?I have multiple observations for one unique identifier. ?Ultimately I > want to end up with one line per identifier with multiple observations in > rows. I'm really stuck any help would be really appreciated. > > > CAO0337134 ? ? ?05/09/95 > CAO0337134 ? ? ?27/09/05 > CAO0347741 ? ? ?10/10/04 > CAO0347741 ? ? ?12/10/04 > CAO0367128 ? ? ?11/07/05 > CAO0367128 ? ? ?12/07/05 > CAO0367128 ? ? ?14/07/05 > CAO0367128 ? ? ?19/09/97 > CAO0367128 ? ? ?20/09/97 > CAO0374110 ? ? ?14/11/89 > CAO0374110 ? ? ?17/11/89 > CAO0374779 ? ? ?01/10/99 > CAO0374779 ? ? ?28/09/99 > CAO0374779 ? ? ?29/09/99 > > Thanks > Natalie > -- > View this message in context: http://n4.nabble.com/Rearranging-a-data-frame-for-multiple-observations-tp1562780p1562780.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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Dear Henrique, THanks this works in the way that I need it to. THanks for your help. Natalie -- View this message in context: http://n4.nabble.com/Rearranging-a-data-frame-for-multiple-observations-tp1562780p1562949.html Sent from the R help mailing list archive at Nabble.com.