-1

I have a data frame like this :

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… 

I would like to split the 3rd column and make the data frame look like this :

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… 

I have tried so far this one: mention_polits_2017=mention_polits_2017[,list(mention_polits_2017=unlist(strsplit(mention_polits_2017,","))),by=mention_polits_2017$Screen.name]

But it is showing me an error, "Error in [.data.frame(mention_polits_2017, , list(mention_polits_2017 = unlist(strsplit(mention_polits_2017, : unused argument (by = mention_polits_2017$Screen.name)"

Thank you.

Eyayaw
  • 1,033
  • 5
  • 10
  • What have you tried till now? – phoxis May 28 '18 at 11:02
  • mention_polits_2017=mention_polits_2017[,list(mention_polits_2017=unlist(strsplit(mention_polits_2017,","))),by=mention_polits_2017$Screen.name] – Eyayaw May 28 '18 at 11:04
  • 1
    @Can you please update the question with what you did and possibly the output as well? This will help everyone. – phoxis May 28 '18 at 11:06
  • I have looked into where this question is answered before. I have tried almost all the alternatives. But I have some problem with my data frame itself. The string I want to split has a weird format, it is I guess the separator problem. Here is for example what is shows for the 3rd row element: strsplit(mention_polits_2017[3,3], " ") [[1]] [1] "" "@ManuelaSchwesig" "" "@sigmargabriel" "" "@nahles" – Eyayaw May 28 '18 at 15:00

1 Answers1

0

You can try

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

Data

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))
Roman
  • 17,008
  • 3
  • 36
  • 49