I am new to R
, would be really grateful if anybody can help me with this issue. I am trying to convert char
list to a data frame in R
.
The char
list looks like this.
chr [1:6]
[1] "Colour: Gold|Style: Without Offers"
[2] "Colour: Gold|Style: Without Offers|Verified Purchase" .
[3] "Colour: Gold|Style: With Offers|Verified Purchase"
[4] "Colour: Gold|Style: Without Offers|Verified Purchase"
[5] "Colour: Black|Verified Purchase" .
[6] "Colour: Gold|Style: Without Offers" .
The desired output looks like this:
Colour Style PurchaseType
========================
[1] Gold Without Offers NA .
[2] Gold Without Offers Verified Purchase .
[3] Gold With Offers Verified Purchase .
[4] Gold Without Offers Verified Purchase
[5] Black NA . Verified Purchase .
[6] Gold Without Offers" NA
Please suggest a solution.
We could split the string by the delimiter and rbind
after rearrange the input fields.
lst <- lapply(strsplit(v1, "\\||\\w+\\:\\s*", perl = TRUE), function(x) {
x1 <- x[nzchar(x)]
if(length(grep("Offer", x1))==0) c(x1[1], NA, x1[2]) else x1})
d1 <- as.data.frame(do.call(rbind, lapply(lst, `length<-`,
max(lengths(lst)))), stringsAsFactors = FALSE)
names(d1) <- c("Colour", "Style", "PurchaseType")
d1
# Colour Style PurchaseType
#1 Gold Without Offers <NA>
#2 Gold Without Offers Verified Purchase
#3 Gold With Offers Verified Purchase
#4 Gold Without Offers Verified Purchase
#5 Black <NA> Verified Purchase
#6 Gold Without Offers <NA>
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.