Reputation: 623
I have a vector having multiple values. If an element and the next do not have a period, I want to append the next element to the previous with a period. But the appended elements should no longer be present in the vector
a = c("135","00","85","6","0.00","6","0.00","0.00","85","61","0.00")
I want the result to be
"135.00","85.6","0.00","6","0.00","0.00","85.61","0.00"
Upvotes: 1
Views: 184
Reputation: 13372
Maybe not the most elegant, but here a solution with a while
construct:
a <- c("135","00","85","6","0.00","6","0.00","0.00","85","61","0.00")
result <- character()
while(length(a) > 0) {
## pop first item from vector
item <- a[1]
a <- a[-1]
## if item contains dots, add to results
if (grepl("\\.", item)) {
result <- c(result, item)
} else {
## Otherise check if next item contains dot
if (! grepl("\\.", a[1])) {
## if not, combine current and next item
result <- c(result, paste(item, a[1], sep='.'))
a <- a[-1]
}
else {
## otherwise return current item
result <- c(result, item)
}
}
}
Upvotes: 1