我在数据框中有一列,如下所示,每一行包含多个国家,并用 ,
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
你试一试:
data.frame(countries = unlist(strsplit(as.character(df$countries), " , ")))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句