Home > Enterprise >  Merge dataset information same cell
Merge dataset information same cell

Time:05-09

I need to do a dataset merge but the information is in the same cell, how can I do it?

dati1<- c("a - Novara", "b - Torino", "c - Milano", "f - Bari")

dati2<- c("a", "b", "c", "f")

dat3 <- dati1<- c("Novara", "Torino", "Milano", "Bari")

result

tot <- data.frame(dati2, dat3)

CodePudding user response:

data.frame(do.call(rbind, strsplit(dati1, split = " - ")))

  X1     X2
1  a Novara
2  b Torino
3  c Milano
4  f   Bari

or with tidyr::separate:

separate(data.frame(dati1), col = dati1, into = str_c("col", 1:2))

CodePudding user response:

as.data.frame(t(do.call(cbind, strsplit(dati1,split=' - '))))

Output:

  V1     V2
1  a Novara
2  b Torino
3  c Milano
4  f   Bari
  • Related