12

I have a column in a dataframe like the one below, each row contains multiple countries separated by ,

df <- data.frame(
                  countries = c(
                                "UK , Spain , Germany , Italy , Netherlands" , 
                                "UK , Canada , AUS , China" , 
                                "Spain , AUS , Italy , Russia"
                                )
                )

This is how data looks like

                   countries
1 UK , Spain , Germany , Italy , Netherland
2                 UK , Canada , AUS , China
3              Spain , AUS , Italy , Russia

How can we transform this to be something like below?

  countries
1   UK
2   Spain
3   Germany
4   Italy
5   Netherlands
6   UK
7   Canada
8   AUS
9   China
10  Spain
11  AUS
12  Italy
13  Russia
Jaap
  • 81,064
  • 34
  • 182
  • 193
Nader Hisham
  • 5,214
  • 4
  • 19
  • 35
  • 2
    In case you have more column there, you could also do `library(splitstackshape) ; cSplit(df, "countries", direction = "long")` – David Arenburg May 17 '15 at 06:16
  • Another option is `data.frame(countries=scan(text=gsub(' ', '', df$countries), what='', sep=','))` – akrun May 17 '15 at 06:27

2 Answers2

10

Just try:

data.frame(countries = unlist(strsplit(as.character(df$countries), " , ")))
zx8754
  • 52,746
  • 12
  • 114
  • 209
nicola
  • 24,005
  • 3
  • 35
  • 56
10

You can use 'separate_rows' from "tidyr" package:

df = separate_rows(df,1,sep = ",")
ALAKANANDA
  • 101
  • 1
  • 2