简体   繁体   中英

Split values inside a cell separated by comma in two different variables in R

Imput:

var_1 Var_2
a,b c,d
e,f g,h

Desidred Output:

var_1 Var_2
a c
b d
e g
f h

Use separate_rows assuming the columns have equal number of elements in each row which are comma separated

library(tidyr)
separate_rows(df1, c(var_1, Var_2), sep=",")

-output

# A tibble: 4 × 2
  var_1 Var_2
  <chr> <chr>
1 a     c    
2 b     d    
3 e     g    
4 f     h    

data

df1 <- structure(list(var_1 = c("a,b", "e,f"), Var_2 = c("c,d", "g,h"
)), class = "data.frame", row.names = c(NA, -2L))

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