Reputation: 307
Suppose I have
raw =
[1] "r" "csv" "python"
[4] "r" "r" "c++"
[7] "r" "matrix" "eigen"
[10] "r" "data.frame" "r"
[13] "subset" "r" "for-loop"
[16] "r" "debugging" "matrix"
[19] "r" "histogram" "distribution"
[22] "lattice" "r" "dplyr"
[25] "r" "gecode" "r"
I want to wrap this string list based on the character r, i.e.
[1] "r; csv; python"
[2] "r"
[3] "r; c++"
[4] "r; matrix; eigen"
I used strsplit() to split by r but it doesn't work that well... any suggestions
Upvotes: 2
Views: 432
Reputation: 4024
Or the dplyr method:
data_frame(raw = raw) %>%
mutate(ID = cumsum(raw == "r") ) %>%
group_by(ID) %>%
summarize(result = raw %>% paste(collapse = "; ") )
Upvotes: 0
Reputation: 99361
You could split then paste. Splitting on cumsum(raw == "r")
will give us the splitting index where each split begins with r
. Then we can iterate over the list, pasting the values together. Since we know the result will be a single character element for each iteration, we can use vapply()
.
vapply(unname(split(raw, cumsum(raw == "r"))), paste, "", collapse = "; ")
# [1] "r; csv; python"
# [2] "r"
# [3] "r; c++"
# [4] "r; matrix; eigen"
# [5] "r; data.frame"
# [6] "r; subset"
# [7] "r; for-loop"
# [8] "r; debugging; matrix"
# [9] "r; histogram; distribution; lattice"
#[10] "r; dplyr"
#[11] "r; gecode"
#[12] "r"
Data:
raw <- c("r", "csv", "python", "r", "r", "c++", "r", "matrix", "eigen",
"r", "data.frame", "r", "subset", "r", "for-loop", "r", "debugging",
"matrix", "r", "histogram", "distribution", "lattice", "r", "dplyr",
"r", "gecode", "r")
Upvotes: 3