Vector includes this file
c("Car", "Bus", "Subway", "Truck", "Speed", "Car")
I would like to take "Speed" and unite it with "Car"
My outcome should be
c("Car", "Bus", "Subway", "Truck", "Speed Car")
CodePudding user response:
Create a new vector with length one less than the original vector and paste
v2 <- v1[seq_len(length(v1)-1)]
v2[length(v2)] <- paste(v1[5:6], collapse = " ")
-output
> v2
[1] "Car" "Bus" "Subway" "Truck" "Speed Car"
Or in a single line
> c(v1[1:4], paste(v1[5:6], collapse = " "))
[1] "Car" "Bus" "Subway" "Truck" "Speed Car"
data
v1 <- c("Car", "Bus", "Subway", "Truck", "Speed", "Car")