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

Loop in R matching values across dataframes?

$
0
0

I have two dataframes, one (df1) that looks like this:

CityYear  City  Year  Value
NY2000    NY    2000  1.2
NY2005    NY    2005  1.0
NY2010    NY    2010  1.4
NY2015    NY    2015  NA
LA2000    LA    2000  0.9
LA2005    LA    2005  1.5
LA2010    LA    2010  1.3
LA2015    LA    2015  NA
SF2000    SF    2000  0.8
SF2005    SF    2005  1.7
SF2010    SF    2010  1.2
SF2015    SF    2015  1.3
...

and so on for a bunch of other cities. The second dataframe (df2) looks something like this:

City1  City2  Connections
NY     NY     0
NY     LA     32
NY     SF     28
NY     DC     12
...
SF     NY     28
SF     LA     12
...

Basically pairing every city (some not even in my first dataset) with each other and giving their connections. Note that every city and pair is repeated twice (as City1 and City2).

In df1, I would like to compute a new variable Flow which takes the value from the formula below (I think it's much clearer than explaining it with words, but I can do it if necessary).

enter image description here

I'm new to R and I have zero idea where to start. I know (or believe) I need a for loop, but I have no idea where to go from there. Any help would be much appreciated.

EDIT: to clarify the formula in words, for every city-year observation I would like to take the Value of every other city in that year, multiply it by the Connections among the two cities, and sum every value together. For example, for New York in 2000 I would like to multiply its connections with LA by LA's value in 2000, and then do the same for DC and so on for every city in 2000, and sum the results to have a single Flow value for NY2000.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



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