2

I would like to take two columns and add them two other columns. For example, I have the data below:

EU.Member.States    X.    Other.countries..continued.   X..1
Austria             122   Cameroon                      203
Belgium             150   Canada                        156
Denmark             179   Canary Islands                132
Finland             156   Cape Verde                    147
France              130   Cayman Islands                213

How can I take the rows under "Other.countries..continued." and "X..1" and add them directly under "EU.Member.States" and "X." respectively?

I have tried using unite of (tidyr) with no success.

mundos
  • 459
  • 6
  • 14

1 Answers1

1

Your question is almost identical to this one. Using the piping from dplyr package I can suggest a solution by first duplicating your column names, and then applying classic rbind. I used only the first 2 lines of your example:

df %>% setNames(names(df)[c(1,2,1,2)]) %>% {rbind(.[,1:2], .[,3:4])}
#### EU.Member.States  X.
#### 1          Austria 122
#### 2          Belgium 150
#### 3         Cameroon 203
#### 4           Canada 156

Note: the brackets are here to tell the piping not to take the . as an implicit first argument.

agenis
  • 8,069
  • 5
  • 53
  • 102