user3640617
user3640617

Reputation: 1576

R - add column based on intervals in separate data frame

I have the following data frames:

DF <- data.frame(Time=c(1:20))
StartEnd <- data.frame(Start=c(2,6,14,19), End=c(4,10,17,20))

I want to add a column "Activity" to DF if the values in the Time column lie inbetween one of the intervals specified in the StartEnd dataframe.

I came up with the following:

mapply(FUN = function(Start,End) ifelse(DF$Time >= Start & DF$Time <= End, 1, 0), 
  Start=StartEnd$Start, End=StartEnd$End)

This doesn't give me the output I want (it gives me a matrix with four columns), but I would like to get a vector that I can add to DF.

I guess the solution is easy but I'm not seeing it :) Thank you in advance.

EDIT: I'm sure I can use a loop but I'm wondering if there are more elegant solutions.

Upvotes: 0

Views: 630

Answers (2)

jimjamslam
jimjamslam

Reputation: 2067

If you're using the tidyverse, I think a good way to go would be with with purrr::map2:

# generate a sequence (n, n + 1, etc.) for each StartEnd row
# (map functions return a list; purrr::flatten_int or unlist can
# squash this down to a vector!)
activity_times = map2(StartEnd$Start, StartEnd$End, seq) %>% flatten_int

# then get a new DF column that is TRUE if Time is in activity_times
DF %>% mutate(active = Time %in% active_times)

Upvotes: 1

smanski
smanski

Reputation: 541

You can achieve this with

DF$Activity <- sapply(DF$Time, function(x) {
  ifelse(sum(ifelse(x >= StartEnd$Start & x <= StartEnd$End, 1, 0)), 1, 0)
})

I hope this helps!

Upvotes: 1

Related Questions