简体   繁体   中英

Duplicating and splitting tab- and semicolon-delimited csv file

I have a .csv file that is tab-delimited with the following data:

Column1 Column2 Column3 Column4
  123      a      v; w     t
  456      b    x; y; z    u

I would like to split items within column3 separated by the semicolon ; into individual rows and duplicate the other information that belongs to its row.

Column1 Column2 Column3 Column4
  123      a       v       t
  123      a       w       t
  456      b       x       u
  456      b       y       u
  456      b       z       u

I am new to using R and will greatly appreciate some ideas on how to perform this. Thank you!

Does this work:

library(dplyr)
library(tidyr)
df %>% separate_rows(Column3, sep = '; ')
# A tibble: 5 x 4
  Column1 Column2 Column3 Column4
    <int> <chr>   <chr>   <chr>  
1     123 a       v       t      
2     123 a       w       t      
3     456 b       x       u      
4     456 b       y       u      
5     456 b       z       u      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM