'For Loop to compile data from multiple dataframes with varied number of observations
I have created a forloop that iterates over a list of dataframes and creates a dataframe output like the one below. However, each possible pair of users does not appear in each dataframe; users appearance varies from one iteration to the next. Some dataframes only have 10 pairs, while others have 200.
| user1 | user2 | value |
|---|---|---|
| Scott | Clark | .067 |
| David | Sarah | .234 |
| David | Clark | .544 |
| Thomas | Sarah | .324 |
| Clark | Thomas | .761 |
| Sarah | Clark | .921 |
I would like to compile a single dataframe to hold the results (i.e. value) of each iteration, so the first two columns are used as a matching reference, and the value column is added for each iteration, populating the cells with the values matching the two users. Each value column being identifiable by the date represented as i in each iteration (i = "2020"). However, some pairs of users may not have comparable data for a given iteration, in which case, I would like those cells to have NA. The resulting dataframe should look something like the one below.
| user1 | user2 | value_2018 | value_2019 | value_2020 | value_2021 | value_2022 |
|---|---|---|---|---|---|---|
| Scott | Clark | .067 | .067 | .098 | .067 | .098 |
| David | Sarah | .234 | NA | .777 | .222 | .756 |
| David | Clark | .544 | .567 | .754 | .121 | .334 |
| Thomas | Sarah | .324 | .123 | NA | NA | .443 |
| Scott | Clark | .761 | .234 | .437 | .878 | .543 |
| David | Sarah | .921 | .345 | .958 | NA | .299 |
Any help would be greatly appreciated. Let me know if I am failing to include some important information.
Here is the dput info:
structure(list(user1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Bilirakis_Gus", "Boozman_John", "Brady_Kevin",
"Buchanan_Vern", "Burgess_Michael", "Burr_Richard", "Carter_John",
"Chabot_Steve", "Collins_Susan", "Cornyn_John", "Fortenberry_Jeff",
"Foxx_Virginia", "Grassley_Charles", "Graves_Sam", "Jordan_Jim",
"Latta_Robert", "Lummis_Cynthia", "McCarthy_Kevin", "McCaul_Michael",
"McClintock_Tom", "McHenry_Patrick", "Murkowski_Lisa", "Portman_Robert",
"Posey_Bill", "Rodgers_Cathy", "Rogers_Mike", "Thompson_Glenn",
"Thune_John", "Toomey_Patrick", "Wicker_Roger", "Wilson_Joe"), class = "factor"),
user2 = structure(1:10, .Label = c("Boozman_John", "Brady_Kevin",
"Buchanan_Vern", "Burgess_Michael", "Burr_Richard", "Carter_John",
"Chabot_Steve", "Collins_Susan", "Cornyn_John", "Fortenberry_Jeff",
"Foxx_Virginia", "Grassley_Charles", "Graves_Sam", "Jordan_Jim",
"Latta_Robert", "Lummis_Cynthia", "McCarthy_Kevin", "McCaul_Michael",
"McClintock_Tom", "McHenry_Patrick", "Murkowski_Lisa", "Portman_Robert",
"Posey_Bill", "Rodgers_Cathy", "Rogers_Mike", "Thompson_Glenn",
"Thune_John", "Toomey_Patrick", "Wicker_Roger", "Wilson_Joe",
"Wittman_Robert"), class = "factor"), rhetsim = c(0.532304260552694,
0.565448793804611, 0.614746610378663, 0.41456827871494, 0.680331655545798,
0.402937226093047, 0.635434336603617, 0.703608799728453,
0.592589001800602, 0.561932070884369)), row.names = c(NA,
10L), class = "data.frame")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
