2015-05-17 54 views
3

爲簡單起見假設我有在類似下面的一個數據幀中的列中,每行包含由,如何一行分成多行中的R

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

這是如何分割的多個國家數據看起來像

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

我們如何改變這是像

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 
+2

如果你有更多的列,你也可以'library(splitstackshape); cSplit(df,「countries」,direction =「long」)' –

+0

另一個選擇是'data.frame(countries = scan(text = gsub('','',df $ countries),what ='',sep = ''))' – akrun

回答

3

試試吧:

data.frame(countries=unlist(strsplit(as.character(df$countries)," , ")))