It depends on how you have your data laid out. This is clumsy but I think it will work. There should be an easier way than a loop but I don't see it. People with more experience in R will likely have much better solutions.
In any case it sure beats Excel or any other spreadsheet :) ====================================================== # Create mock data set (data.frame) with four cows. i <- 1:4 cows <- paste("cow",i, sep="") food <- sample(c("hay", "grain"), 4, replace=TRUE) cattle <- data.frame(cows, food) # Vecctor of longevity data age <- sample(3:9, 4, replace = TRUE) #Create empty list mylist <- NULL for(i in 1:length(cattle[,1])) { count <-(seq(1,age[i])) dead <- c(rep(0,length(count)-1),1) newcow <- data.frame(merge(data.frame(cattle[i,]), count),dead) mylist[[i]] <- newcow } # Turn mylist into a data.frame mydata <- do.call(rbind, mylist) # Get rid of unneeded count varable. mydata <- mydata[,-3] mydata ====================================================== --- Felix Zajitschek - UNSW <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a dataset consisting of 5 columns and over > 5000 rows. Each row > gives information about an individual animal, > including longevity, i.e. > at what age an animal died. > For the model I use I need to create n rows for each > animal, n being its > longevity, and a new column 'survival' with a binary > 0/1 outcome. When > an animal died e.g. at age 5, there have to be 5 > rows of identical data, > except 4 with 0 (=alive) for 'survival', and 1 row > with '1' for > 'survival'. > > I thought of creating matrices for each individual, > adding first one > column 'survival' containing zeros to the original > dataset, then > creating matrices with data = 'the vector containing > all elements of an > individual/row' ([1,], nrow = [a,b], exctracting the > element for > longevity, and then with byrow = TRUE letting the > data be filled in by > row. At the end I would have to set the last element > in 'survival' to > '1', and then combine all matrices into one single > one. > > So far I've used Excel to create these datesets > manually, but with more > than 1000 individuals this gets really tedious. I > haven't used R before > for this sort of a bit more advanced data > manipulation, and I would > really appreciate any input/primer about how people > would go about doing > this. > > Thanks, > Felix > > > ______________________________________________________________ > ::Felix Zajitschek > Evolution & Ecology Research Centre > School of Biological, Earth and Environmental > Sciences > University of New South Wales - Sydney NSW 2052 - > Australia > Tel +61 (0)2 9385 8068 > Fax +61 (0)2 9385 1558 > eMail <mailto:[EMAIL PROTECTED]> > [EMAIL PROTECTED] > > <http://www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.htm > l> > www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.html > > > [[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. > [[elided trailing spam]] ______________________________________________ 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.