在R中是否有办法检查个人观察时间是否随时间保持等级? 多年来我测量了许多植物,并想检查大型植物是否保持较大而小型植物是否保持较小(即大型植物是否阻止其他植物生长)。这些植物的大小排名为1-5(从小到大)。 我测量了大约1000株植物。
非常感谢任何答案或评论。 /斯蒂娜
答案 0 :(得分:1)
也许你做这样的事情?
# create random data
plantId <- sample(1:50,1000,replace=TRUE)
rank <- sample(1:5,1000,replace=TRUE)
time <- as.POSIXct(sample(1000000:10000000,1000,replace=FALSE)+10000000*rank,origin="1970-01-01")
myData <- data.frame(plantId , rank, time )
# function to calculate the time a plant has a given rank
getRankTime <- function(id,testRank,data=myData){
plantData <- myData[myData$plantId==id,];
if(nrow(plantData) < 2){ # only one observed value of this plant
return(NA)
}else if(all(plantData$rank != testRank)){ # plant was never of the rank under consideration
return(NA)
}else{ # calculate the (censered) time the plant stay(ed) in rank 'testRank'
startObsTimeInRank <- min(plantData$time[plantData$rank == testRank])
if(any(plantData$rank > testRank)){
endObsTimeInRank <- min(plantData$time[plantData$rank > testRank])
}else{
#eighter take the last time
endObsTimeInRank <- max(plantData$time[plantData$rank == testRank])
# alternatively use the current time
# endObsTimeInRank <- Sys.time()
}
return(as.numeric(endObsTimeInRank - startObsTimeInRank))
}
}
# calculate the average time plants stay in a rank
allPlantIds <- unique(myData$plantId)
stayInRankTime <- list()
for(runRank in 1:5){
stayInRankTime[[runRank]] <- sapply(allPlantIds, function(runPlatId) getRankTime(runPlatId,runRank) )
}
# average time plants stay in acertain rank'
avgRankTime <- lapply(stayInRankTime,function(x)mean(x, na.rm =TRUE))
avgRankTime