Reputation: 596
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)
Upvotes: 1
Views: 43
Reputation: 886938
A base R
option
read.table(text = dati1, header = FALSE, strip.white = TRUE, sep = "-")
V1 V2
1 a Novara
2 b Torino
3 c Milano
4 f Bari
Upvotes: 1
Reputation: 78917
One more:
library(dplyr)
library(stringr)
data.frame(dati1) %>%
mutate(dati1= str_split_fixed(dati1, '\\-', 2), .keep="unused")
dati1.1 dati1.2
1 a Novara
2 b Torino
3 c Milano
4 f Bari
Upvotes: 1
Reputation: 51894
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))
Upvotes: 2