I've written some code with help to a previously answered question. Initially I had this code:
getT <- function(df, ID, Number){
df %>%
group_by(ID, Number) %>%
mutate( Distance = finish - begin) %>%
select(-begin,-finish,-symbols) %>%
nest() %>%
mutate( data = map( data, ~ filter(.x, Distance == max(Distance)))) %>%
unnest()
}
getallT <- as.data.frame(getT(df))
getTID <- function(df, ID) {
subset(x = getallT, subset = (ID))
}
Which gave this output:
ID Number Time Distance
33 1 2.00 870
33 2 1.98 859
33 3 0.82 305
33 4 2.02 651
33 5 2.53 502
I wanted to filter it by Time
so I used this code(thanks to a post below):
getHLN <- function(df, ID) {
getallT %>% filter (ID ==id & !between(Time, 1.50, 2.10))
}
Which now gives this output:
ID Number Time Distance
1 33 3 0.82 305
2 33 4 2.02 651
3 33 5 2.53 502
But now I've come across an issue so now I'm left wondering how to either:
A. Filter out Number 4 & 5 so that I can create a separate function with a different Time
filter for it. To later create another different function to merge the two previous functions into one.
OR
B. Create a different Time
filter specifically for Number 4 & 5 within the same function.
I tried doing A. by using filter (getallT, Number >= 3) %>%
but doesn't work. I would rather go with B though if possible. So something like...
For ID numbers 1-3: filter(!between(Time,1,2))
For ID numbers 4-5: filter(!between(Time 1.5,2.3)) within the same function.
I've been trying out a few things for the past day but keep getting error messages such as Error in filter_impl(.data, quo) :
Evaluation error: operations are possible only for numeric, logical or complex types.
I've been trying out what's on here but must not be doing something write so need some insight! http://genomicsclass.github.io/book/pages/dplyr_tutorial.html
Here is an example dataset
df <- data.frame(ID=rep(33,5),
Number=1:5,
Time=c(2.00,1.98,0.82,2.02,2.53),
Distance=c(870,859,305,651,502))
Any help would be much appreciated.