[R] separate and gather functions

reichm@@j m@iii@g oii sbcgiob@i@@et reichm@@j m@iii@g oii sbcgiob@i@@et
Tue Aug 13 01:22:36 CEST 2019

R-Help Forum


I have a data set from which I have extracted two columns Column 1 is a
listing of Federal agencies and Column 2 lists functions like this


Col1                       Col2

Agency A             Function1, Function2, Function3, Function4

Agency B              Function2, Function4

Agency C              Function1, Function3, Function4


What I need is a long list like this


Col1                       Col2

Agency A             Function1

Agency A             Function2

Agency 4              Function3 etc


Is there a more elegant /efficient way other than what I did which was to
separate Col2 into separate columns and then gather the data back up


myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
"x4", "x5"), sep = ",")  

myDat4 <- gather(data=myDat3, key="type", value = "Value",
"x1","x2","x3","x4","x5", na.rm = TRUE)


while it works, looking for a more elegant solution, if there is one



	[[alternative HTML version deleted]]

More information about the R-help mailing list