I have the following dataframe with definitions on names and cities which I would like to process, I'm not sure how to explain it so I included tables below with input and output.
Input:
+---------+-----------+------------+---------------+
| Varname | Component | names | cities |
+---------+-----------+------------+---------------+
| A | B | Jack,Bruce | New york |
| B | | Cathy | Boston,Miami |
| C | | Bob | New york |
| D | C | Dick,Nancy | Austin,Dallas |
| E | A,C | | |
| F | | Mandy | Manchester |
+---------+-----------+------------+---------------+
output:
+---------+-----------+----------------------+------------------------+
| Varname | Component | names | cities |
+---------+-----------+----------------------+------------------------+
| A | | Jack,Bruce,Cathy | New york,Boston,Miami |
| B | | Cathy | Boston,Miami |
| C | | Bob | New york |
| D | | Dick,Nancy,Bob | Austin,Dallas,New york |
| E | | Jack,Bruce,Cathy,Bob | New york,Boston,Miami |
| F | | Mandy | Manchester |
+---------+-----------+----------------------+------------------------+
As you hopefully see, I would like to take the component column, and for each of those Varnames in that column, look up the names and cities (and in reality I have slight more columns) and combine them so that I have a complete table. Is this possible? I have no idea where to start. My tables are not huge, so an for(){} statement could be applied.
->edit, I may have not have given a correct example, I have replace the input with something that is more consistent with my data.
dput() of input
structure(list(Varname = structure(1:6, .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), Component = structure(c(3L, 1L, 1L, 4L, 2L, 1L), .Label = c("", "A,C", "B", "C"), class = "factor"), names = structure(c(5L, 3L, 2L, 4L, 1L, 6L), .Label = c("", "Bob", "Cathy", "Dick,Nancy", "Jack,Bruce", "Mandy"), class = "factor"), cities = structure(c(5L, 3L, 5L, 2L, 1L, 4L), .Label = c("", "Austin,Dallas", "Boston,Miami", "Manchester", "New york" ), class = "factor")), .Names = c("Varname", "Component", "names", "cities"), class = "data.frame", row.names = c(NA, -6L ))
dput
so it can be copied – Warner