【问题标题】:R if conditional nested in for loopR if 条件嵌套在 for 循环中
【发布时间】:2015-12-27 01:21:57
【问题描述】:

这是用于 R 编程的,在此先感谢!

我有一个数据向量,它有一个给定的年份,后面跟着一些 dd/mm 格式的日期。我正在尝试使用 for 循环遍历整个向量,并使用带有正则表达式的 if 条件将年份值粘贴到日期值(缺少年份但出现在适当的年份之下)。

向量:

doc.year <- c(2014, Alabama, 10/07, Georgia, 11/07, 2013, Virginia, 9/21, GT, 12/12, 2012, Miami, 08/21, Cal, 08/30) #original data
year.vector <- (2014, 2013, 2012) #years are pulled from the original data
doc.date <- doc.year #create a copy of the original data
gameday.vector <- (10/07, 11/07, 9/21, 12/12, 08/21, 08/30) #mm/dd dates are pulled from the original data

我的代码:

for(i in doc.year){ #iterate through the raw data
  if(doc.year[i] %in% year.vector){ #if it is a year value
    season.year <- doc.year[i] #then assign that year to a variable
  }
  else if(doc.year[i] %in% gameday.vector){ #if it's not a year value and it is a mm/dd value
    gameday <- doc.year[i] #then assign that date a variable
    doc.date[i] <- paste(gameday,"/",season.year) #and replace the copied vector with the date in mm/dd/yyyy format
  }
  else{
  }
}

期望的输出:

doc.date <- (2014, Alabama, 10/07/2014, Georgia, 11/07/2014, 2013, Virginia, 9/21/2013, GT, 12/12/2013, 2012, Miami, 08/21/2012, Cal, 08/30/2012)

【问题讨论】:

  • 不明白你的问题,也不明白你的输出。您能否将“...”替换为真实示例并引用所有字符例如,如果 Alabama 是新行或新值,请引用它。

标签: regex r for-loop


【解决方案1】:

@Simon 的解决方案是一个很好的 R 解决方案,但如果您坚持使用循环,您的代码几乎就在那里:

doc.year <- c(2014, "Alabama", "10/07", "Georgia", "11/07", 2013, "Virginia", "9/21")
year.vector <- c(2014, 2013)
doc.date <- doc.year
gameday.vector <- c("10/07", "11/07", "9/21")

for(i in 1:length(doc.year)) {
  if(doc.year[i] %in% year.vector){
    season.year <- doc.year[i]
  }
  else if(doc.year[i] %in% gameday.vector){
    gameday <- doc.year[i]
    doc.date[i] <- paste(gameday,"/",season.year, sep="")
  }
}

【讨论】:

    【解决方案2】:

    您不想在 R 中使用 for 循环。如果您考虑使用 for 循环,最好使用 C、Perl、C++、Java 或类似的东西。也就是说,假设数据格式为

    doc.year <- c("2014", "Alabama", "10/07", "Georgia", "11/07",
                  "2013", "Virginia", "9/21")
    

    您可以在没有任何其他变量的情况下一次性获得所需的片段:

    ## figure out which entries are years
    is.year <- grepl("^\\d{4}$", doc.year)
    
    ## create a vector with years for all non-year entries
    years <- rep(doc.year[is.year], matrix(rle(is.year)$len,2)[2,] / 2)
    
    ## paste together the dates
    dates <- paste(matrix(doc.year[!is.year],2)[2,], years, sep='/')
    

    对于任何理智的 R 处理,您只需创建一个带有结果的数据框:

    > data.frame(place=matrix(doc.year[!is.year],2)[1,], date=dates)
         place       date
    1  Alabama 10/07/2014
    2  Georgia 11/07/2014
    3 Virginia  9/21/2013
    

    如果你真的想回去修补doc.year,你可以,例如:

    doc.year[grep("/", doc.year)] <- dates
    
    > doc.year
    [1] "2014"       "Alabama"    "10/07/2014" "Georgia"    "11/07/2014"
    [6] "2013"       "Virginia"   "9/21/2013" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2020-03-06
      • 2023-03-02
      • 2017-09-21
      • 2019-10-01
      相关资源
      最近更新 更多