Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Initialize data in row format in first data fram:
- v1<-c("A",1,1.3)
- v2<-c("A",2,1.8)
- v3<-c("A",3,2.4)
- v4<-c("B",1,0.8)
- v5<-c("B",3,1.7)
- first_DF<-data.frame(matrix(c(v1,v2,v3,v4,v5),ncol=3, nrow=5,byrow=TRUE,dimnames=list(NULL,c("Individual","DayAlive","Length"))), stringsAsFactors=FALSE)
- # Convert to column format in second data frame:
- individual_IDs<-unique(first_DF$"Individual")
- days_alive<-unique(first_DF$"DayAlive")
- # Initialize second DF by subsetting a single row for each individual from the first DF
- second_DF<-data.frame(first_DF[which(first_DF$"Individual" %in% individual_IDs & first_DF$"DayAlive" %in% 1),1], stringsAsFactors=FALSE)
- names(second_DF)<-"Individual"
- initial_DF_width<-dim(second_DF)[2]
- # Move 'Length' data into the columns as each 'day alive' column is created:
- for(i in 1:length(days_alive)){
- current_day<-days_alive[i]
- second_DF<-cbind(second_DF,matrix(ncol=1, nrow=nrow(second_DF),dimnames=list(NULL,paste("Day ",current_day," Length"))))
- for(j in 1:length(individual_IDs)){
- current_individualID<-individual_IDs[j]
- length<-first_DF[which(first_DF$"Individual" %in% current_individualID & first_DF$"DayAlive" %in% current_day),"Length"]
- second_DF[j,i+initial_DF_width]<-length
- }
- }
- library('reshape2')
- dcast(first_DF, Individual ~ DayAlive)
- # Individual 1 2 3
- # 1 A 1.3 1.8 2.4
- # 2 B 0.8 <NA> 1.7
- for(i in 1:length(days_alive)){
- current_day<-days_alive[i]
- second_DF<-cbind(second_DF,matrix(ncol=1, nrow=nrow(second_DF),dimnames=list(NULL,paste("Day ",current_day," Length"))))
- for(j in 1:length(individual_IDs)){
- current_individualID<-individual_IDs[j]
- # I changed "length" to "length2" to avoid confusion with the
- # function length(). You also don't need which() here.
- length2 <- first_DF[first_DF$Individual %in% current_individualID
- & first_DF$DayAlive %in% current_day, "Length"]
- if (length(length2) > 0) {
- second_DF[j, i + initial_DF_width] <- length2
- } else {
- second_DF[j, i + initial_DF_width] <- NA
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement