简体   繁体   English

遍历R中的未知日期

[英]Iterating through unknown dates in R

I'm new to R (having worked in C++ and Python before) so this is probably just a factor of me not knowing some of R's nuances. 我是R的新手(以前在C ++和Python中工作过),所以这可能只是我不了解R的某些细​​微差别的一个因素。

The program I'm working on is supposed to construct matrices of data by date. 我正在研究的程序应该按日期构造数据矩阵。 Here's how I might initialize such a matrix: 这是我如何初始化这样的矩阵的方法:

dates <- seq(as.Date("1980-01-01"), as.Date("2013-12-31"), by="days")
HN3 <- matrix(nrow=length(dates), ncol = 5, dimnames = list(as.character(dates), c("Value1", "Value2", "Value3", "Value4", "Value5")))

Notice that dates includes every day between 1980 and 2013. 请注意, dates包括1980年至2013年之间的每一天。

So, from there, I have files containing certain dates and measurements of Value1 , etc for those dates, and I need to read those files' contents into HN3 . 因此,从那里开始,我有一些文件,其中包含某些日期和这些日期的Value1等度量值,我需要将这些文件的内容读取到HN3 But the problem is that most of the files don't contain measurements for every day . 但是问题是大多数文件都不包含每天的测量结果

So what I want to do is read a file into a dataframe (say, v1read ) with column 1 being dates and column 2 being the desired data. 因此,我想做的是将文件读入数据帧(例如v1read ),其中第1 v1read日期,第2 v1read所需数据。 Then I'd match the dates of v1read to that date's row in HN3 and copy all of the relevant v1read values that way. 然后,我比赛的日期v1read在该日期的行中的HN3和复制所有相关的v1read值的方式。 Here is my attempt at doing so: 这是我这样做的尝试:

for (i in 1:nrow(v1read)) {
  HN3[as.character(v1read[i,1]),Value1] <- v1read[i,4]
}

This gives me an out of index range error when the value of i is bumped up unexpectedly. i的值意外增加时,这给了我一个超出索引范围的错误。 I understand that R doesn't like to iterate through dates, but since the iterator itself is a numeric value rather than a date, I was hoping I'd found a loophole. 我知道R不喜欢遍历日期,但是由于迭代器本身是一个数字值而不是日期,所以我希望发现一个漏洞。

Any tips on how to accomplish this would be enormously appreciated. 任何有关如何完成此操作的技巧将不胜感激。

Let's use library(dplyr) . 让我们使用library(dplyr) Start with 从...开始

dates = seq(as.Date("1980-01-01"), as.Date("2013-12-31"), by="days")
HN3 = data.frame(Date=dates)

Now, load in your first file, the one that has a date and Value1. 现在,加载第一个文件,该文件具有日期和Value1。

file1 = read.file(value1.file) #I'm assuming this file has a column already named "Date" and one named #Value1
HN3 = left_join(HN3,file1,by="Date")

This will do a left join (SQL style) matching only the rows where a date exists and filling in the rest with NA. 这将执行左连接(SQL样式),仅匹配存在日期的行,并用NA填充其余行。 Now you have a data frame with two columns, Date and Value1. 现在,您具有一个包含两列的数据框,即Date和Value1。 Load in your other files, do a left_join with each and you'll be done. 加载其他文件,对每个文件进行left_join即可完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM