检测数据框中丢失的(不存在的)行并用 NA 替换它们
Detect missing (non existing) rows within a dataframe and replace them with NA
我有一个包含约 12000 个观察值的数据框,其中有两列 "Code" 和 "Date"。每个代码应该有 4 个观察值,因此有 4 个日期,但是我在 "Date" 列中有缺失值(不是 NA,而是不存在的行)。
这是我的数据框示例:
Station Date
7002 17/12/1966
7002 05/05/1968
7002 30/10/1968
7002 16/08/1970
7003 02/12/1966
7003 05/05/1968
7003 31/10/1968
8004 04/07/1968
8004 15/11/1968
8006 13/10/1966
8006 23/09/1967
8006 01/09/1968
[....]
我需要做的是为每个代码检测缺少的行。
我正在使用 "water years",从 10 月 1 日开始到下一个 9 月 30 日结束,例如1998 年 1 月 10 日 - 1999 年 9 月 30 日。 这就是难点,这使得我的问题与其他类似的问题不同。
所考虑的时间段范围从 1966 年 1 月 10 日到 1970 年 9 月 30 日(4 个水年),"Date" 栏中的观测值已经固定为水年(即每个水一个观测值年)。
我的输出应该是这样的:
例如
Station Date
7002 17/12/1966
7002 05/05/1968
7002 30/10/1968
7002 16/08/1970
7003 02/12/1966
7003 05/05/1968
7003 31/10/1968
7003 NA
8004 NA
8004 04/07/1968
8004 15/11/1968
8004 NA
8006 13/10/1966
8006 23/09/1967
8006 01/09/1968
8006 NA
[...]
library(lubridate)
library(dplyr)
设置示例数据:
dat = read.table(text="Station Date Day
7002 17/12/1966 77
7002 05/05/1968 582
7002 30/10/1968 760
7002 16/08/1970 1415
7003 02/12/1966 62
7003 05/05/1968 582
7003 31/10/1968 761
8004 04/07/1968 4294
8004 15/11/1968 4428
8006 13/10/1966 5856
8006 23/09/1967 6567
8006 01/09/1968 6910", header=TRUE, stringsAsFactors=FALSE)
dat$Date = as.Date(dat$Date, format=c("%d/%m/%Y"))
添加水年: 我假设水年是以水年开始的年份来命名的。比如water year 01/10/1967 - 30/09/1968就是water year 1967.
dat$water.year = ifelse(month(dat$Date) %in% 1:9, year(dat$Date) - 1, year(dat$Date))
添加缺失年份的行: 我通过合并一个包含 Station
和 water.year
的所有组合的新数据框来做到这一点。
full_join(expand.grid(Station=unique(dat$Station), water.year=1966:1969),
dat,
by=c("Station","water.year")) %>% arrange(Station, water.year)
Station water.year Date Day
1 7002 1966 1966-12-17 77
2 7002 1967 1968-05-05 582
3 7002 1968 1968-10-30 760
4 7002 1969 1970-08-16 1415
5 7003 1966 1966-12-02 62
6 7003 1967 1968-05-05 582
7 7003 1968 1968-10-31 761
8 7003 1969 <NA> NA
9 8004 1966 <NA> NA
10 8004 1967 1968-07-04 4294
11 8004 1968 1968-11-15 4428
12 8004 1969 <NA> NA
13 8006 1966 1966-10-13 5856
14 8006 1966 1967-09-23 6567
15 8006 1967 1968-09-01 6910
16 8006 1968 <NA> NA
17 8006 1969 <NA> NA
我有一个包含约 12000 个观察值的数据框,其中有两列 "Code" 和 "Date"。每个代码应该有 4 个观察值,因此有 4 个日期,但是我在 "Date" 列中有缺失值(不是 NA,而是不存在的行)。
这是我的数据框示例:
Station Date
7002 17/12/1966
7002 05/05/1968
7002 30/10/1968
7002 16/08/1970
7003 02/12/1966
7003 05/05/1968
7003 31/10/1968
8004 04/07/1968
8004 15/11/1968
8006 13/10/1966
8006 23/09/1967
8006 01/09/1968
[....]
我需要做的是为每个代码检测缺少的行。
我正在使用 "water years",从 10 月 1 日开始到下一个 9 月 30 日结束,例如1998 年 1 月 10 日 - 1999 年 9 月 30 日。 这就是难点,这使得我的问题与其他类似的问题不同。
所考虑的时间段范围从 1966 年 1 月 10 日到 1970 年 9 月 30 日(4 个水年),"Date" 栏中的观测值已经固定为水年(即每个水一个观测值年)。
我的输出应该是这样的: 例如
Station Date
7002 17/12/1966
7002 05/05/1968
7002 30/10/1968
7002 16/08/1970
7003 02/12/1966
7003 05/05/1968
7003 31/10/1968
7003 NA
8004 NA
8004 04/07/1968
8004 15/11/1968
8004 NA
8006 13/10/1966
8006 23/09/1967
8006 01/09/1968
8006 NA
[...]
library(lubridate)
library(dplyr)
设置示例数据:
dat = read.table(text="Station Date Day
7002 17/12/1966 77
7002 05/05/1968 582
7002 30/10/1968 760
7002 16/08/1970 1415
7003 02/12/1966 62
7003 05/05/1968 582
7003 31/10/1968 761
8004 04/07/1968 4294
8004 15/11/1968 4428
8006 13/10/1966 5856
8006 23/09/1967 6567
8006 01/09/1968 6910", header=TRUE, stringsAsFactors=FALSE)
dat$Date = as.Date(dat$Date, format=c("%d/%m/%Y"))
添加水年: 我假设水年是以水年开始的年份来命名的。比如water year 01/10/1967 - 30/09/1968就是water year 1967.
dat$water.year = ifelse(month(dat$Date) %in% 1:9, year(dat$Date) - 1, year(dat$Date))
添加缺失年份的行: 我通过合并一个包含 Station
和 water.year
的所有组合的新数据框来做到这一点。
full_join(expand.grid(Station=unique(dat$Station), water.year=1966:1969),
dat,
by=c("Station","water.year")) %>% arrange(Station, water.year)
Station water.year Date Day 1 7002 1966 1966-12-17 77 2 7002 1967 1968-05-05 582 3 7002 1968 1968-10-30 760 4 7002 1969 1970-08-16 1415 5 7003 1966 1966-12-02 62 6 7003 1967 1968-05-05 582 7 7003 1968 1968-10-31 761 8 7003 1969 <NA> NA 9 8004 1966 <NA> NA 10 8004 1967 1968-07-04 4294 11 8004 1968 1968-11-15 4428 12 8004 1969 <NA> NA 13 8006 1966 1966-10-13 5856 14 8006 1966 1967-09-23 6567 15 8006 1967 1968-09-01 6910 16 8006 1968 <NA> NA 17 8006 1969 <NA> NA