简体   繁体   English

R 中的匹配日期

[英]Matching Dates in R

I have a data frame concerning the purchases of a shop owner.我有一个关于店主购买的数据框。 They don't happen on a daily basis.它们不会每天发生。 It has two columns: the first one describes the date , the second one the quantity bought in that date.它有两列:第一列描述日期,第二列描述在该日期购买的数量

I would like to transform it into daily data , completing the original dataset, so I created a sequence:我想把它转换成每日数据,完成原始数据集,所以我创建了一个序列:

a <- seq(as.Date("2013/11/19"), as.Date("2017/04/22"), "days")

The first date corresponds to the one of the first purchase, and the second one of the last one, of the original dataset.第一个日期对应于原始数据集的第一次购买和最后一次购买的第二个日期。

The classes are both " Date ".这些课程都是“日期”。

How can I merge the two dataset by "date" , even if, obviously, they have different rows length?如何通过 "date"合并两个数据集,即使它们显然具有不同的行长? I would like to have a dataframe with daily " Date " as first column , and " Quantity " on the second one , with zeros where purchases didn't happen.我想对第二每天日期”作为第一中的数据帧,并且“数量”,用那里购买没有发生。

Using base R:使用基础 R:

# create sample data frame with sales data
test <- data.frame(date = as.Date(c("2017/08/12", "2017/08/15", "2017/09/02")), quantity = c(3,2,1))
# create the date range
dates <- data.frame(date = seq(min(test$date), max(test$date), by = "day"))
# perform the left join
# (keeping all rows from "dates", and joining the sales dataset to them)
result <- merge(dates, test, by.y = "date", by.x = "date", all.x = TRUE)

In the merge function, by.y and by.x are the columns used to join the dataset, while all.x tells you, that all rows from x (in this case dates ) should be kept in the resulting data frame.在合并函数中, by.y 和 by.x 是用于连接数据集的列,而all.x告诉您,来自x所有行(在本例中为dates )应保留在结果数据框中。

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

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