Ali Mroueh
Ali Mroueh

Reputation: 33

Sub-setting a data frame into multiple other dataframes using a loop

I have the following data frame named stationDF. https://i.sstatic.net/Ucph0.png

I also have vectors from_nodes and to_nodes, from_nodes <- c(1, 156, 153, 3), to_nodes <- c(156, 153, 3, 185). As you can see in the data frame, these from and to vectors correspond to the "from" and "to" columns in my stationDF. I am trying to subset this stationDF based on these vectors. I have tried :

x1 <- stationDF[stationDF$from == from_nodes[1] & stationDF$to == to_nodes[1] |
                stationDF$from == to_nodes[1] & stationDF$to == from_nodes[1],]

This is sub setting my data frame by all the stations that are either going from 1 to 156 or 156 to 1. The following is the output of this: https://i.sstatic.net/TLqv4.png.

I want to do this for the rest of the variables in the from and to vectors but not hard coded. For example,

for (i in 1:length(from){
    x <- stationDF[stationDF$from == from_nodes[i] & stationDF$to == to_nodes[i] |
                   stationDF$from == to_nodes[i] & stationDF$to == from_nodes[i],]
}

This obviously wont work like this because it will overwrite the previous iteration but that is the thought process. I would like to end up with four different subsets of the stationDF or even just one large one if four can not be done. Anything helps, thank you.

Upvotes: 1

Views: 44

Answers (1)

Muon
Muon

Reputation: 1346

A simple modification to your code that you could make without too much trouble is the following:

x_ls <- list()
for (i in 1:length(from_nodes){
  x_ls[[i]] <- stationDF[stationDF$from == from_nodes[i] & stationDF$to == to_nodes[i] |
                         stationDF$from == to_nodes[i] & stationDF$to == from_nodes[i],]
}
x <- do.call('rbind', x_ls)

This will take each data frame output by the loop and save it into a list. Then at the end you simply bind all the dataframes in the list together using the rbind function called with do.call.

"do.call" is just a function that allows you to unpack values as arguments into another function. In this case it would be synonymous with rbind(x_ls[[1]], x_ls[[2]], x_ls[[3]], x_ls[[4]]).

Upvotes: 2

Related Questions