margo
margo

Reputation: 23

Filtering dataset to only show the data within a range of values

How do I remove data within a dataframe which is not included within my range? My dataframe which contains the information is called df and contains a start and end point:

start  end
65     1237
1262   2134
2178   4511
.....

I am trying to filter through results in another df, n, to only show the rows in V1 which fall between the start and end points in df. n looks like:

V1    V2
1     0
2     0
3     1
4     0

I have wrote this function so far, which I am wanting to apply to n, however, the code does not do anything when typing in T_filter(n) in the console. Does anyone have any suggestions?

T_filter <- function(x) {
   T_range=c(df$start: df$end)
   T_range = sort(T_range)
   tmp = tmp[ T_range , ]
   tmp = tmp[!is.na(tmp$V1),]
  }}

Upvotes: 0

Views: 88

Answers (1)

user6836753
user6836753

Reputation:

You may try.

do.call(rbind, Map(\(x,y) n[x:y, ], df$start, df$end))

It's mapping the columns in parallel, and using those to subset the rows

Upvotes: 1

Related Questions