50196

Vector contains every value in range

Question:

I have a dataframe testData which is made up of many unique ids. My objective is to identify whether or not the ids contain <i>all</i> of the numbers in the range of month, yday, and week

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

A sample of the data looks like this:

> 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

The output should look something like this:

> 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

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 ))

Answer1:

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

Answer2:

Here's how to do it with dplyr:

library(dplyr) testData_copy <-testData testData %>% group_by(id) %>% summarise(month=n_distinct(month)== n_distinct(testData_copy$month), yday =n_distinct(yday) == n_distinct(testData_copy$yday), week =n_distinct(week) == n_distinct(testData_copy$week) ) # A tibble: 5 × 4 id month yday week <int> <lgl> <lgl> <lgl> 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

Recommend

  • How to find week of month for calendar which starts from Monday?
  • SAS Get week number for each month
  • Alter View within stored procedure
  • LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-null
  • Shutdown for socketserver based Python 3 server hangs
  • How to use case/esac in process substitution?
  • Excel changes conditional formatting formula
  • Add date without exceeding a month
  • Aggregating based on “near” row values
  • Message Response Zombies occurring with errors Codes 0xC0C01B4C and 0xc0c016b5 no Orchestration
  • How to perform a left join in SQLALchemy?
  • Dynamically load images from project folder - Windows Phone 7
  • TypeScript Mapped Types: Get element type of array
  • Cannot debug project after upgrading to Service Fabric SDK v2.3.301
  • Use double quote then curly brace in powershell -Command
  • Create a table from a list of tuples in Python 3
  • Populate checkbox from database
  • Sort by a column in a union query in SqlAlchemy SQLite
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Is there a package like bigmemory in R that can deal with large list objects?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Initializer list vs. initialization method
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Modifying destination and filename of gulp-svg-sprite
  • Deserializing XML into class C#
  • Trying to switch camera back to front but getting exception
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • embed rChart in Markdown
  • python draw pie shapes with colour filled
  • Programmatically clearing map cache
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?