Skip to content
Prev 139976 / 398502 Next

create matrix

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
<felix.zajitschek at unsw.edu.au> wrote:

            
______________________________________________________________
<http://www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.htm
www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.html
[[elided trailing spam]]