0

I've got a df with country-level data entered in 2003.

Several rows of data belong to a country named 'Federal Republic of Yugoslavia'.

These are two separate countries today and I want to duplicate these rows of data so that I can rename each set of rows to its respective modern country name.

data.frame(Country = "Yugoslavia", Chickens = 567)

Using this minimal example, how do I create this dataframe?

data.frame(Country = c("Serbia", "Montenegro"), Chickens = 567)
Ravikiran Reddy Kotapati
  • 2,485
  • 3
  • 22
  • 27
prayner
  • 393
  • 1
  • 10
  • Hi and welcome to SO! can you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your dataset ? – dc37 Nov 25 '19 at 14:17

3 Answers3

1

you can do in one tidyverse pipe:

library(tidyverse)
df2 <- df %>%
  mutate(Country = if_else(Country == "Yugoslavia", "Serbia", as.character(Country))) %>%
  bind_rows(df) %>%
  mutate(Country = if_else(Country == "Yugoslavia", "Montenegro", as.character(Country)))

You could also use mutate_if instead of the if_else statements.

     Country Chickens
1     Serbia      567
2 Montenegro      567

By default data.frame turns character columns into factors. The substitution above coerces into character.

If you want to preserve the factor class then just add:

%>% mutate(Country = as.factor(Country))

... at the end.

nycrefugee
  • 1,629
  • 1
  • 10
  • 23
0

You can do something like this:

data2<-data[data$country=="Yugoslavia"]
levels(data2$country)[levels(data2$country)=="Yugoslavia"]<-"Serbia"
levels(data$country)[levels(data$country)=="Yugoslavia"]<-"Montenegro"
rbind(data,data2)
iod
  • 7,412
  • 2
  • 17
  • 36
0

You can write a function which returns the duplicated and renamed rows like:

fun <- function(y) {
  if(y[["Country"]] == "Yugoslavia") rbind(replace(y, "Country", "Serbia")
                                         , replace(y, "Country", "Montenegro"))
  else y
}
do.call("rbind", apply(x, 1, fun))
#     Country      Chickens
#[1,] "Italy"      "  2"   
#[2,] "Serbia"     "567"   
#[3,] "Montenegro" "567"   
#[4,] "Austria"    "  3"   

Or if order does not matter:

rbind(x[x$Country != "Yugoslavia",]
, replace(x[x$Country == "Yugoslavia",], "Country", "Serbia")
, replace(x[x$Country == "Yugoslavia",], "Country", "Montenegro"))
#      Country Chickens
#1       Italy        2
#3     Austria        3
#2      Serbia      567
#21 Montenegro      567

Data:

x <- data.frame(Country = c("Italy","Yugoslavia","Austria"), Chickens = c(2,567,3))
x
#     Country Chickens
#1      Italy        2
#2 Yugoslavia      567
#3    Austria        3
GKi
  • 37,245
  • 2
  • 26
  • 48