[R] Replacing certain rows with values from a different column

James Milks jrm||k@ @end|ng |rom y@hoo@com
Tue Aug 10 12:58:18 CEST 2021

I have two columns in a larger data set that list countries in one column and, in some cases, individual provinces within a country or oversea territories in another. I have country population in a second data set that I’m planning to use to calculate per capita rates in the first data set. My issue: I need to match my two data sets. Here are some examples:

First data set:

Province <- c("Australian Capital Territory", "New South Wales", "Northern Territory", "Queensland", "South Australia", "Tasmania", "Victoria", "Western Australia", "", "", "", "Faroe Islands", "Greenland")

Country <- c("Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Austria", "Azerbaijan", "Denmark", "Denmark", "Denmark")

firstdf <- data.frame(Province, Country)

Second data set:

Country <- c("Australia", "Austria", "Azerbaijan", "Denmark", "Faroe Islands", "Greenland")

seconddf <- data.frame(Country)

In this example, I need to aggregate sum Australia while keeping Faroe Islands and Greenland separate from Denmark. What I’d like to do is create a column that looks like this:

firstdf$nation <- c("Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Australia", "Austria", "Azerbaijan", "Denmark", “Faroe Islands", “Greenland”)

Is there a way to do this or am I stuck doing this by hand?

Thanks for any help on this vexing issue.

Jim Milks
	[[alternative HTML version deleted]]

More information about the R-help mailing list