0

I want to combind data1 and region . Currently, I usefor...,the result as total_df. Is there any easier way for this ? Thanks!

data1 <- data.frame(category=c("a","b","c"),
                    amount=1:3)

region <- data.frame(country=c('US','UK'))


total_df <- data.frame()
for (country in region$country){
  data1$region <- country
  total_df <- rbind(total_df,data1)
}
anderwyang
  • 1,801
  • 4
  • 18

1 Answers1

1

Yes !

Using rep, the code gets refactored, like this:

data1 <- data.frame(category=rep(c("a","b","c"),2),
                    amount=rep(1:3,2),
                    country=c(rep("US",3),rep("UK",3)))

> data1
  category amount country
1        a      1      US
2        b      2      US
3        c      3      US
4        a      1      UK
5        b      2      UK
6        c      3      UK
AugtPelle
  • 549
  • 1
  • 10