0

I am a beginner in R and now I got a question regarding the dataframe which I am working with.

I have some columns with several answers from a survey. Below are two columns:

OperatingSystem    LanguageWorkedWith
Linux              C;C++;C#
Windows            Java; JavaScript
Mac                C
Linux              C#

And need to break it down into separated rows acconding to the OperatingSystem (also in 2 columns: OperatingSystem and LanguageWorkedWith):

Linux   C
Linux   C++

and so on... Sorry if the question is duplicated, I just couldn't find a solution for that.

thelatemail
  • 91,185
  • 12
  • 128
  • 188

1 Answers1

0

separate_rows in the tidyr package can do that.

library(dplyr)
library(tidyr)

DF %>% separate_rows(LanguageWorkedWith, sep = ";")

giving:

  OperatingSystem LanguageWorkedWith
1           Linux                  C
2           Linux                C++
3           Linux                 C#
4         Windows               Java
5         Windows         JavaScript
6             Mac                  C
7           Linux                 C#

Note

The input used in reproducible form is:

Lines <- "OperatingSystem - LanguageWorkedWith
Linux - C;C++;C#
Windows - Java; JavaScript
Mac - C
Linux - C#"
DF <- read.table(text = Lines, header = TRUE, sep = "-", strip.white = TRUE, comment = "")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341