如何与“参考日期”进行比较,然后在R中填写缺失的数据? [英] How to compare with a "reference date", then fill missing data in R?

查看:53
本文介绍了如何与“参考日期”进行比较,然后在R中填写缺失的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此主题的另外两个问题:

解决方案

  library(tidyverse)

#示例数据
dt_A = data.frame(Date = c( 2016年1月4日10:53 , 02/04/2016 10:54, 03/04/2016 10:55),
A_Value = c(5,6,7))

dt_B = data.frame(日期= c( 2016年1月4日10:53, 2016年3月4日10:55),
B_Value = c(1,3))

#使用数据A的日期完成数据B的日期
dt_B%>%已完成(Date = dt_A $ Date)


##小技巧:3 x 2
#日期B_Value
#< chr> < dbl>
#1 01/04/2016 10:53 1
#2 02/04/2016 10:54 NA
#3 03/04/2016 10:55 3


Two more questions about this topic: A B

First, let me show the example data (Data A & B):

(1) Data A:

Date_Collected  A_Value
01/04/2016 10:53    0.137
01/20/2016 13:13    0.204
01/25/2016 11:09    0.199
02/01/2016 12:55    0.441
02/01/2016 12:56    0.215
02/01/2016 13:11    0.397
02/03/2016 09:19    0.377
02/10/2016 08:11    1.45
02/15/2016 13:04    2.63

(2) Data B:

Date_Collected  B_Value
01/04/2016 10:53    0.108
01/20/2016 13:13    0.404
02/01/2016 13:11    0.594
02/15/2016 13:04    1.99

Second, I will tell what I want to do with R. You can see that "Data A" has 9 records, while "Data B" has only 4 records. As these values are so precious, I will not delete "Data A" to fit the rows of "Data B". Instead, I will fill the "missing" data in "Data B". The things need to do can be separated into two parts:

(Part Ⅰ) ① To add blank rows for "Data B" in the corresponding location, according to "Data A"; ② In these blank rows (blue in Fig.1), copy the corresponding date. The result at the end of Part Ⅰ is like Fig.1.

(Part Ⅱ) To interpolate the missing data in "B_Value". This part has been solved. You can see the solution in here of Stack Overflow.

Could someone give me advice about it (especially Part Ⅰ)? Thanks.

解决方案

library(tidyverse)

# example data
dt_A = data.frame(Date = c("01/04/2016 10:53", "02/04/2016 10:54", "03/04/2016 10:55"),
                  A_Value = c(5,6,7))

dt_B = data.frame(Date = c("01/04/2016 10:53", "03/04/2016 10:55"),
                  B_Value = c(1,3))

# complete dates of data B using dates of data A
dt_B %>% complete(Date = dt_A$Date)


# # A tibble: 3 x 2
#   Date             B_Value
#   <chr>              <dbl>
# 1 01/04/2016 10:53       1
# 2 02/04/2016 10:54      NA
# 3 03/04/2016 10:55       3

这篇关于如何与“参考日期”进行比较,然后在R中填写缺失的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆