我有12列,其中包括有关物种产卵的数据。在每一列中,1表示该物种在该月内产卵,0表示没有。我想创建另一个称为“产卵期”的列,该列给出了几个月的范围。我还有另一列,其中包括调查日期,并且我想要另一列查看产卵期列和调查日期,并确定调查是否在产卵期发生。
预期结果是:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Spawning_Period Survey_Date Survey_Sampling
1 1 1 1 1 1 1 1 1 1 1 1 Jan-Dec 17/01/2019 1
1 1 0 0 0 0 0 0 1 1 1 1 Jan-Feb, Sep-Dec 13/06/2019 0
我一直认为for循环可能是答案,因为有500次观察。我看过类似的问题,但似乎找不到我想要的东西。
答案 0 :(得分:0)
希望这是一个足够的解决方案,您可以随后添加日期和采样:
A = matrix(c(rep(1,14),rep(0,6),rep(1,4),rep(0,12)),nrow=3,ncol=12,byrow=T)
A = data.frame(A)
colnames(A) = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
# Change the colnames to numerics( we can handle them much easier)
colnames(A) = c( 1:12)
A$Spawning_Period=rep(0,nrow(A))
#a loop is inefficient but shows you what actually happens here:
for(i in 1:nrow(A)){
Month.Vector=c()
#Shows a vector of months pre row, where species spawn
Month.Vector=colnames(A)[A[i,]==1]
Spawning.Period=c(1,diff(as.numeric(Month.Vector)))
#new Code to determine when there is no spawning period
if(length(Month.Vector)==0){
A[i,"Spawning_Period"]=NA } else{
#Determines the Break point
Break.Points=which(Spawning.Period!=1)
if(length(Break.Points)==0){
A[i,"Spawning_Period"]="1-12"}else{
Collect = list()
for(j in 1:length(Break.Points)){
if(j==1){
Collect [[1]] = paste(Month.Vector[1:(Break.Points[j]-1)][1],Month.Vector[1:(Break.Points[j]-1)][length(Month.Vector[1:(Break.Points[j]-1)])],sep="-")
Collect[[length(Break.Points)+1]] = paste(Month.Vector[(Break.Points[j]):length(Month.Vector)][1],Month.Vector[(Break.Points[j]):length(Month.Vector)][length(Month.Vector[(Break.Points[j]):length(Month.Vector)])],sep="-")
}
if(j!=1){
Collect[[j]] = paste(Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)][1],Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)][length(Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)])],sep="-")
}
}
A[i,"Spawning_Period"]= paste(unlist(Collect), collapse =", ")
}
}
}
您将在此处将月份表示为日期,但是如果您愿意,可以将其转换为相应的值。希望这会有所帮助。
答案 1 :(得分:0)
这是使用@RBeginner的完整解决方案,以防万一有人想看到它。我注意到没有中断时,它仍会在应为4,5时发布1-12,因此我添加了该值,并为1-12中的0添加了NA。
spawning_period_ind <- spawning_period[,c(1:12)]
colnames(spawning_period_ind) = c( 1:12)
spawning_period_ind$Spawning_Period=c(0,0)
for(i in 1:nrow(spawning_period_ind)){
Month.Vector=c()
#Shows a vector of months pre row, where species spawn
Month.Vector=colnames(spawning_period_ind)[spawning_period_ind[i,]==1]
Spawning.Period=c(1,diff(as.numeric(Month.Vector)))
#new Code to determine when there is no spawning period
if(all(is.na(Month.Vector)==TRUE)){
spawning_period_ind[i,"Spawning_Period"]=NA }
else if(all(is.na(Month.Vector)==FALSE & length(Spawning.Period)==12)) {
spawning_period_ind[i,"Spawning_Period"]="1-12"
}
else
{
#Determines the Break point
Break.Points=which(Spawning.Period!=1)
if(length(Break.Points)==0 & length(Spawning.Period)!=12)
{
spawning_period_ind[i,"Spawning_Period"]= paste(Month.Vector, collapse = ",", sep = "")
}
else
{
Collect = list()
for(j in 1:length(Break.Points)){
if(j==1){
Collect [[1]] = paste(Month.Vector[1:(Break.Points[j]-1)][1],Month.Vector[1:(Break.Points[j]-1)][length(Month.Vector[1:(Break.Points[j]-1)])],sep="-")
Collect[[length(Break.Points)+1]] = paste(Month.Vector[(Break.Points[j]):length(Month.Vector)][1],Month.Vector[(Break.Points[j]):length(Month.Vector)][length(Month.Vector[(Break.Points[j]):length(Month.Vector)])],sep="-")
}
if(j!=1){
Collect[[j]] = paste(Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)][1],Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)][length(Month.Vector[(Break.Points[j-1]):(Break.Points[j]-1)])],sep="-")
}
}
spawning_period_ind[i,"Spawning_Period"]= paste(unlist(Collect), collapse =", ")
}
}
}