0

我有这些日期:

library(lubridate)
set.seed(50)
myDates <- ymd("2013-07-12") + days(sample(1:100, 20))
df <- data.frame(date=as.Date(myDates), value=sample(1:100, 20))
df[sample(1:20, 5, replace=F), "value"] <- NA

         date value
1  2013-09-21    NA
2  2013-08-25    11
3  2013-08-01    NA
4  2013-09-25    96
5  2013-08-31    55
6  2013-07-17    27
7  2013-09-16    99
8  2013-09-11    66
9  2013-07-16    89
10 2013-07-22    37
11 2013-08-17    NA
12 2013-08-06    56
13 2013-09-07    NA
14 2013-07-19    39
15 2013-08-05    NA
16 2013-09-08    17
17 2013-10-20    54
18 2013-08-12    23
19 2013-10-07    71
20 2013-07-26    98

我想创建一个函数,将上述日期范围和任何其他日期范围分成 4 个部分。这 4 个部分应该是日期范围的第 1、第 2、第 3 和第 4 个四分位数。因此,该函数需要找到最早日期和最晚日期,然后将 的每个元素分配value给一个四分位数。上面代码中的日期范围是这样的:

range(df$date[!is.na(df$date)])
[1] "2013-07-16" "2013-10-20"

然后我需要该函数来查找每个四分位数中 NA 值的数量。这可以做到吗?

4

2 回答 2

1

我相信以下顺序应该可以帮助您至少解决部分问题(抱歉笨拙):

df <- df[order(df[, 1] ), ]  # sort by date
df$order <- seq(1:nrow(df))  # assignment of order
quartSize <- nrow(df)/4  # size of quartiles
breakPts <- seq(1, nrow(df), quartSize)  # break points
quant <- rep(0, nrow(df))
for (i in 1:nrow(df))
  quant[i] <- ifelse(df[i, 3] < breakPts[2], 1,
                     ifelse(df[i, 3] < breakPts[3], 2,
                            ifelse(df[i, 3] < breakPts[4], 3, 4)
                     )
  )
df <- cbind(df, quant)

如果您然后运行table(df$quant, is.na(df[, 2]))[, 2]​​,您将在每个四分位数上获得 NA 的计数。

最早的日期是df[1, ];最新的,df[nrow(df), ].

于 2013-07-12T13:36:50.347 回答
1

这是一个建议:

# Create data
library(lubridate)
set.seed(50)
myDates <- ymd("2013-07-12") + days(sample(1:100, 20))
df <- data.frame(date=as.Date(myDates), value=sample(1:100, 20))
df[sample(1:20, 5, replace=F), "value"] <- NA

#          date value
# 1  2013-09-21    NA
# 2  2013-08-25    NA
# 3  2013-08-01    70
# 4  2013-09-25    82
# 5  2013-08-31    30
# 6  2013-07-17    NA
# 7  2013-09-16    55
# 8  2013-09-11    NA
# 9  2013-07-16    96
# 10 2013-07-22    34
# 11 2013-08-17    33
# 12 2013-08-06    37
# 13 2013-09-07    39
# 14 2013-07-19    54
# 15 2013-08-05    99
# 16 2013-09-08    NA
# 17 2013-10-20    11
# 18 2013-08-12    59
# 19 2013-10-07    31
# 20 2013-07-26    38

# Proposed solution
myQtle   <- quantile(as.POSIXlt(df$date), probs = 0.25 * 1:4)
myCumVal <- sapply(myQtle,
                   function(qtle, theDates, theValues){
                       sum(is.na(theValues[theDates <= qtle]))},
                   theDates  = as.POSIXlt(df$date),
                   theValues = df$value)

data.frame(qtle  = as.Date(myQtle),
           nb.na = c(myCumVal[1], diff(myCumVal)))

#            qtle nb.na
# 25%  2013-07-30     1
# 50%  2013-08-21     0
# 75%  2013-09-12     3
# 100% 2013-10-20     1
于 2013-07-12T15:03:24.470 回答