如何在R中进行字符串拆分?

时间:2018-05-28 11:00:41

标签: r string strsplit

我有一个这样的数据框:

null

我想拆分第3列并使数据框看起来像这样:

Screen.name     party                             users
1  A_Gloeckner   SPD                          @MartinSchulz. 
2  A_Gloeckner   SPD                           @MartinSchulz 
3 A_Gloeckner   SPD  @ManuelaSchwesig @sigmargabriel @nahles
4  a_grotheer   SPD                           @SouthendRNLI 
5  a_grotheer   SPD                           @ribasdiego10 
6  a_grotheer   SPD                        @HBBuergerschaft 
7  a_grotheer   SPD                             @UniBremen… 

到目前为止,我已尝试过这一个: 提及_polits_2017 =提及_polits_2017 [,列表(提及_polits_2017 = unlist(strsplit(提及_polits_2017,“,”))),by =提及_polits_2017 $ Screen.name]

但它显示错误,“Screen.name party mentioned_users 1 A_Gloeckner SPD @MartinSchulz. 2 A_Gloeckner SPD @MartinSchulz 3 A_Gloeckner SPD @ManuelaSchwesig 4 A_Gloeckner SPD @sigmargabriel 5 A_Gloeckner SPD @nahles 6 a_grotheer SPD @SouthendRNLI 7 a_grotheer SPD @ribasdiego10 8 a_grotheer SPD @HBBuergerschaft 9 a_grotheer SPD @UniBremen… 中的错误(提及_polits_2017 ,,列表(提及_polits_2017 = unlist(strsplit(提及_polits_2017,:   未使用的参数(by = mention_polits_2017 $ Screen.name)“

谢谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试

library(tidyverse)
df %>% 
 separate_rows(users, sep=" ")
  Screen.name party            users
1 A_Gloeckner   SPD   @MartinSchulz.
2 A_Gloeckner   SPD    @MartinSchulz
3 A_Gloeckner   SPD @ManuelaSchwesig
4 A_Gloeckner   SPD   @sigmargabriel
5 A_Gloeckner   SPD          @nahles
6  a_grotheer   SPD    @SouthendRNLI
7  a_grotheer   SPD    @ribasdiego10
8  a_grotheer   SPD @HBBuergerschaft
9  a_grotheer   SPD       @UniBremen

数据

df <- structure(list(Screen.name = structure(c(1L, 1L, 1L, 2L, 2L, 
                                               2L, 2L), .Label = c("A_Gloeckner", "a_grotheer"), class = "factor"), 
                     party = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "SPD", class = "factor"), 
                     users = c("@MartinSchulz.", "@MartinSchulz", "@ManuelaSchwesig @sigmargabriel @nahles", 
                               "@SouthendRNLI", "@ribasdiego10", "@HBBuergerschaft", "@UniBremen"
                     )), class = "data.frame", .Names = c("Screen.name", "party", 
                                                          "users"), row.names = c(NA, -7L))