【问题标题】:Is there a way to get a range of month values in a seperate column based on multiple columns?有没有办法根据多列在单独的列中获取一系列月份值?
【发布时间】:2019-07-10 16:38:29
【问题描述】:

我有 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 个观察值。我看过类似的问题,但似乎找不到我要找的东西。

【问题讨论】:

    标签: r


    【解决方案1】:

    希望这是一个足够的解决方案,您可以在之后添加日期和采样:

    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 月至 12 月有 0 的情况下,当没有关于产卵期的可用信息时,它会填充 1-12,因此它应该是 NA。
    • 我刚刚编辑了答案,预先设置了一个简单的 if 条件。
    • 所以我尝试了这个,但它最终没有工作,但我做了更多的调整,它完全符合我的需要,所以谢谢!
    【解决方案2】:

    这是使用@RBeginner 的完整解决方案,以防万一有人想看到它。我注意到,当没有休息时,它仍然会发布 1-12,而它应该是 4,5,所以我添加了它,对于 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 =", ")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2021-08-10
      • 2020-11-10
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2016-09-09
      相关资源
      最近更新 更多