子集仅包含日期的矩阵

时间:2022-06-24 20:07:41

I got a matrix m which looks like this:

我有一个矩阵m,看起来像这样:

df <- c("2011-01-02", "2011-01-05", "2011-01-08", "2011-01-11", "2011-01-14", 
"2011-01-17", "2011-01-20", "2011-01-23", "2011-01-26", "2011-01-29", 
"2011-02-01", "2011-02-04", "2011-02-07", "2011-02-10", "2011-02-13") 

m <- matrix(nrow = 1, ncol = 15, byrow = T, data = df)

The entries within the matrix are Date class objects.

矩阵中的条目是Date类对象。

I have an interval with:

我有一个间隔:

start <- as.Date("2011-01-26")
end <- as.Date("2011-02-10")

Now I want to select all the dates in matrix m that fall into this interval (e.g. cols 9 to 14) and output them into a new matrix. I was not able to perform this selection, any input is much appreciated! I use the lubridatepackage in R.

现在我想选择矩阵m中落入此区间的所有日期(例如cols 9到14)并将它们输出到新矩阵中。我无法执行此选择,任何输入都非常感谢!我在R中使用了lubridatepackage。

The formatting of the matrix m reflects a part of the 1st row of my original Dates matrix, which itself has dim = (96, 850).

矩阵m的格式反映了我原始日期矩阵的第一行的一部分,其本身具有dim =(96,850)。

EDIT1: edited for clarity, the "start" & "end" were Date types but not labeled as such.

编辑1:为清晰起见编辑,“开始”和“结束”是日期类型,但没有标记为这样。

2 个解决方案

#1


3  

Best to treat your dates as Date class

最好将您的日期视为日期类

# convert to Dates
m <- as.Date(m)
start <- as.Date(start)
end <- as.Date(end)

Use dplyr::between to filter your data

使用dplyr :: between过滤数据

require(dplyr)
ans <- m[dplyr::between(m, start, end)]  # m >= start && m <= end
# [1] "2011-01-26" "2011-01-29" "2011-02-01" "2011-02-04" "2011-02-07"
# [6] "2011-02-10"

This outputs a vector.

这输出一个向量。

For a matrix output, try something like this

对于矩阵输出,尝试这样的事情

n <- m
n[!dplyr::between(n, start, end)] <- NA
newm <- matrix(nrow=1, ncol=15, byrow=T, data=n)

#2


1  

Here is a base R method using seq to generate the dates in between start and end and %in%

这是一个基本R方法,使用seq生成开始和结束之间的日期和%in%

m[m %in% as.character(seq(as.Date(start), as.Date(end), by="day"))]
[1] "2011-01-26" "2011-01-29" "2011-02-01" "2011-02-04" "2011-02-07" "2011-02-10"

One thing to note is that Date is a class in R, as is matrix. It is quite difficult, (probably not impossible) to store dates in a matrix as Date objects. Instead, the above are stored as characters.

需要注意的一点是,Date是R中的一个类,矩阵也是如此。将日期作为Date对象存储在矩阵中是非常困难的(可能并非不可能)。相反,以上内容存储为字符。

This solution converts start and end to Dates using as.Date and then uses seq to produce a vector of daily dates from start to end. These are then converted to characters and used to compare the element in m using %in%. Finally, [ is used to extract the desired elements.

此解决方案使用as.Date将开始和结束转换为日期,然后使用seq生成从开始到结束的每日日期向量。然后将它们转换为字符并用于使用%in%来比较m中的元素。最后,[用于提取所需的元素。

#1


3  

Best to treat your dates as Date class

最好将您的日期视为日期类

# convert to Dates
m <- as.Date(m)
start <- as.Date(start)
end <- as.Date(end)

Use dplyr::between to filter your data

使用dplyr :: between过滤数据

require(dplyr)
ans <- m[dplyr::between(m, start, end)]  # m >= start && m <= end
# [1] "2011-01-26" "2011-01-29" "2011-02-01" "2011-02-04" "2011-02-07"
# [6] "2011-02-10"

This outputs a vector.

这输出一个向量。

For a matrix output, try something like this

对于矩阵输出,尝试这样的事情

n <- m
n[!dplyr::between(n, start, end)] <- NA
newm <- matrix(nrow=1, ncol=15, byrow=T, data=n)

#2


1  

Here is a base R method using seq to generate the dates in between start and end and %in%

这是一个基本R方法,使用seq生成开始和结束之间的日期和%in%

m[m %in% as.character(seq(as.Date(start), as.Date(end), by="day"))]
[1] "2011-01-26" "2011-01-29" "2011-02-01" "2011-02-04" "2011-02-07" "2011-02-10"

One thing to note is that Date is a class in R, as is matrix. It is quite difficult, (probably not impossible) to store dates in a matrix as Date objects. Instead, the above are stored as characters.

需要注意的一点是,Date是R中的一个类,矩阵也是如此。将日期作为Date对象存储在矩阵中是非常困难的(可能并非不可能)。相反,以上内容存储为字符。

This solution converts start and end to Dates using as.Date and then uses seq to produce a vector of daily dates from start to end. These are then converted to characters and used to compare the element in m using %in%. Finally, [ is used to extract the desired elements.

此解决方案使用as.Date将开始和结束转换为日期,然后使用seq生成从开始到结束的每日日期向量。然后将它们转换为字符并用于使用%in%来比较m中的元素。最后,[用于提取所需的元素。