向量包含范围内的每个值 [英] Vector contains every value in range
问题描述
我有一个数据框 testData
,它由许多唯一的 id 组成.我的目标是确定 ID 是否包含 month
、yday
和 week<范围内的all 数字/代码>
I have a dataframe testData
which is made up of many unique ids. My objective is to identify whether or not the ids contain all of the numbers in the range of month
, yday
, and week
换句话说,如果 id
具有 month
范围内的所有可能值,那么它应该收到一个 t
.如果 id
具有 yday
范围内的所有可能值,它应该收到一个 t
,如果 id
有week
范围内的所有可能值,它应该收到一个 t
.否则,它应该收到一个 f
In other words, if id
has all possible values in the range in month
, then it should receive a t
. If id
has all possible values in the range in yday
, it should receive a t
, and if id
has all possible values in the range in week
, it should receive a t
. Otherwise, it should receive an f
数据示例如下所示:
> testData
id month yday week
1 1 1 1 1
2 3 1 2 1
3 4 1 3 1
4 2 1 4 1
5 3 3 5 1
6 4 1 6 1
7 2 1 7 1
8 3 1 8 2
9 1 1 9 2
10 5 1 10 2
11 3 2 11 1
12 4 1 12 1
13 5 1 13 1
14 1 1 14 1
输出应该是这样的:
> output
id month yday week
1 1 f f t
2 2 f f f
3 3 t f t
4 4 f f f
5 5 f f t
我知道可以使用 findInterval()
检查数字是否在某个范围内,但是有人可以建议一种方法来检查向量中的数字是否包含范围内的所有整数吗?
I know that one can check if a numbers are within a certain range with findInterval()
, but could someone suggest a method to check if numbers in a vector contain all integers within a range?
> dput(testData)
structure(list(id = c(1L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 1L, 5L,
3L, 4L, 5L, 1L), month = c(1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L), yday = 1:14, week = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L)), .Names = c("id", "month",
"yday", "week"), class = "data.frame", row.names = c(NA, -14L
))
推荐答案
Easy with data.table
library(data.table)
setDT(testdata)
output<-testdata[,.(month=all(unique(testdata$month)%in%.SD$month),yday=all(unique(testdata$yday)%in%.SD$yday),Week=all(unique(testdata$week)%in%.SD$week)),by=(id)]
output
id month yday Week
1: 1 FALSE FALSE TRUE
2: 2 FALSE FALSE FALSE
3: 3 TRUE FALSE TRUE
4: 4 FALSE FALSE FALSE
5: 5 FALSE FALSE TRUE
这篇关于向量包含范围内的每个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!