Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

how to filter for a vector within a data frame that contains some but not all of the elements within the vector

$
0
0

I have a large data set that contains a lot of information about departure times of bus stops. I have a main data set that contains information regarding Trip_ID, Bus_sign as well as stop_ID. I further have an index by which I would like to filter the df by.

df <- data.frame(c(10,10,10,10,10,10,10,10,10,10),
               c(8,10,12,15,22,26,27,40,45,50),
               c("0000001","0000002","0000003","0000004","0000005","0000006","0000007", "0000008","0000009","0000010"))

names <- c("trip_ID", "Bus_sign", "stop_ID")
colnames(df) <- names

index <- c("0000001", "0000002", "0000003", "0000011","00000013")

the data frame would look something like this

 trip_ID Bus_sign stop_ID
1       10        8 0000001
2       10       10 0000002
3       10       12 0000003
4       10       15 0000004
5       10       22 0000005
6       10       26 0000006
7       10       27 0000007
8       10       40 0000008
9       10       45 0000009
10      10       50 0000010

the index contains some of the stop_ID within df, however it also contains some that are not in df. I would like to filter for matches of index and df for df$stop_ID.

the result should look like this:

trip_ID Bus_sign stop_ID
1      10        8 0000001
2      10       10 0000002
3      10       12 0000003 

I have tried the subset function, however it wouldn't work

subset(df, stop_ID %in% index)

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>