【发布时间】: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 是新行或新值,请引用它。