2017-03-26 10 views
0

Ich habe zwei große Zeichenvektoren wie folgt:Wie zwei große Zeichen Vektoren in einem Datenrahmen zu kombinieren?

a<- c("Product1_name Product1_desc  Product1_color","Product2_name Product2_desc  Product2_color","Product3_name Product3_desc  Product3_color") 

b<- c("16 MAR 2017","15 MAR 2017","20 MAR 2017") 

Wie kann ich a und b in einem Datenrahmen (4 Spalten) wie unten kombinieren:

enter image description here

+1

'data.frame (b, do.call (rbind, strsplit (a, '\\ s +')))' – Sotos

Antwort

1

ein Potential s olution das stringr und data.table Paket mit dem folgenden

library(data.table) 
library(stringr) 

a <- c("Product1_name Product1_desc  Product1_color","Product2_name Product2_desc  Product2_color","Product3_name Product3_desc  Product3_color") 
b <- c("16 MAR 2017","15 MAR 2017","20 MAR 2017") 

# remove multiple consecutive spaces from a 
a <- str_replace_all(a, "()+", " ") 

# create data table 
dt <- data.table(
    date = b, 
    product_tmp = a 
) 

# split temporary product column in three columns 
dt[, c("product_name", "product_desc", "product_color") := tstrsplit(product_tmp, " ")] 

# remove temporary product column 
dt[, product_tmp := NULL] 

# show data table 
dt 
+0

Danke, wie erwartet gearbeitet. – Curious

1

Wir können read.table zu spaltete sich die 'a' in drei Spalten und cbind mit 'b'

cbind(b, read.table(text=a, header=FALSE)) 
Verwandte Themen