简体   繁体   English

在表中添加观察并赋予给定观察的属性(连接)

[英]Adding observations in a table and attribute a given observation (join)

Good afternoon,下午好,

I'm analyzing the distribution of observations in a given month, for example:我正在分析给定月份的观察分布,例如:

Date Observations日期观察

2010-01 10 2010-01 10

2010-03 15 2010-03 15

2010-05 16 2010-05 16

Question: How do I insert the missing dates (2010-02 and 2010-05) in the table (using other table with all the monthly dates) and attribute a 0 as observations.问题:如何在表中插入缺失的日期(2010-02 和 2010-05)(使用包含所有月度日期的其他表)并将 0 属性作为观察值。

Thanks in advance.提前致谢。

We convert the 'Date' to Date class, then use complete expand the dataset by getting the sequence of min/max or first , last 'Date' by '1 month' while fill ing the 'Observations' with 0我们将“日期”转换为Date class,然后使用complete的数据集扩展数据集,方法是获取min/maxfirstlast “日期” by “1 个月”,同时用 0 fill “观察”

library(tidyr)
library(dplyr)
df1 %>%
     mutate(Date = as.Date(Date)) %>%
     complete(Date = seq(first(Date), last(Date), by = '1 month'), 
            fill = list(Observations = 0))

If there is another dataset with complete 'Date', then the obvious option is a left_join and then replace the NA elements in 'Observations' with 0 because by default if we don't have a match, it will be filled with NA如果有另一个具有完整“日期”的数据集,那么显而易见的选项是left_join ,然后将“观察”中的NA元素替换为 0,因为默认情况下,如果我们没有匹配项,它将用NA填充

left_join(df2, df1, by = 'Date') %>%
     mutate(Observations = replace_na(Observations, 0))

NOTE: df2 is the dataset with complete 'Date'注意: df2是具有完整“日期”的数据集

In case, if the 'df2' have other columns as well, we don't need to select those columns如果'df2'还有其他列,我们不需要select这些列

left_join(df2 %>% 
               select(Date), df1) %>%
     mutate(Observations = replace_na(Observations, 0))

In base R , we can use mergebase R中,我们可以使用merge

transform(merge(df2, df1, by = 'Date', all.x = TRUE),
      Observations = replace(Observations, is.na(Observations), 0))

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

相关问题 按组删除过去的观察到最接近给定日期的观察 - Remove past observations up to nearest observation to given date by group data.table count observations close in distance and time of the current observation 统计当前观测距离和时间接近的观测值 - data.table count observations close in distance and time of current observation 对于表中的每个观测值,根据纬度和经度 (R) 计算 x 米内表中其他观测值的数量 - For each observation in a table, count number of other observations in table within x metres based on latitude and longitude (R) 在 data.table 中添加缺失的观察值 - adding missing observations in data.table 根据给定行中的观察结果选择data.table列的子集 - Select subset of data.table columns based on observations in a given row 如何生成具有给定值的所有观察值的列表/表格。 在 R - How to generate list/table of all the observations with a given value. In R 将类似的连续观察合并为R中的一个观察 - combine similar consecutive observations into one observation in R 根据观察日期分组观察次数 - Number observations by group according to date of observation 如何在某个观察值之上计算观察值? - How to count observations above a certain observation? 观测次数占每年观测总数的比例 - Number of observation as a share of total observations per year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM