Hi Jim, Thank you very much for your suggestions. I used it but it gave me three sites. But actually I do have only two sites "Id_X" and "Id_y" . In fact "A" is repeated two times for "Id_X". If it is repeated, I would like to take the first one among many repeated values.
dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("id_X", "id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L, 3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16", "9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L, 2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE = structure(c( 1L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("ID", "EventDate", "timeGroup", "SITE"), class = "data.frame", row.names = c(NA, -5L)) library(prettyR) stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE")) ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2 SITE_3 1 id_X B1 9/8/16 9/9/16 9/15/17 A A B 2 id_Y B1 9/7/16 9/15/16 <NA> A B <NA> > Basically I am looking for like following table ID timeGroup EventDate_1 EventDate_2 EventDate_3 SITE_1 SITE_2 1 id_X B1 9/8/16 9/9/16 9/15/17 A B 2 id_Y B1 9/7/16 9/15/16 <NA> A B Thanks On Tue, May 1, 2018 at 3:32 PM, Jim Lemon <drjimle...@gmail.com> wrote: > Hi Marna, > Try this: > > library(prettyR) > stretch_df(dat,idvar="ID",to.stretch=c("EventDate","SITE")) > > Jim > > > On Wed, May 2, 2018 at 8:24 AM, Marna Wagley <marna.wag...@gmail.com> > wrote: > > Hi R user, > > I was trying to convert a long matrix to wide? I have an example and > would > > like to get a table (FinalData1): > > > > > > FinalData1 > > B1 B2 > > id_X "A" "B" > > id_Y "A" "B" > > > > but I got the following table using the following code. > > > > FinalData1 > > > > B1 B2 > > > > id_X "A" "A" > > > > id_Y "A" "B" > > > > > > the code and the example data I used are given below. Is there any > > suggestions to fix the problem? > > > > > > dat<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L), .Label = > c("id_X", > > > > > > "id_Y"), class = "factor"), EventDate = structure(c(4L, 5L, 2L, > > > > 3L, 1L), .Label = c("9/15/16", "9/15/17", "9/7/16", "9/8/16", > > > > "9/9/16"), class = "factor"), timeGroup = structure(c(1L, 1L, > > > > 2L, 1L, 2L), .Label = c("B1", "B2"), class = "factor"), SITE = > structure(c( > > 1L, > > > > 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = > c("ID", > > > > "EventDate", "timeGroup", "SITE"), class = "data.frame", row.names = > c(NA, > > > > -5L)) > > > > > > tmp <- split(dat, dat$ID) > > > > tmp1 <- do.call(rbind, lapply(tmp, function(dat){ > > > > tb <- table(dat$timeGroup) > > > > idx <- which(tb>0) > > > > tb1 <- replace(tb, idx, as.character(dat$SITE)) > > > > })) > > > > > > tmp1 > > > > FinalData<-print(tmp1, quote=FALSE) > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 -- To UNSUBSCRIBE and more, see 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.