r - Replacing elements of a vector -
i have following string:
string <- c("100 100 test 100 string")
i replace 100's in above string elements of vector:
replacement <- c(1000,2000,3000)
the first 100 of string should replace 1000, second 100 2000 , on. resulting string should follows:
result <- c("1000 2000 test 3000 string")
is there efficient way in r?
thank you.
ravi
not elegant, should do..
string <- c("100 100 test 100 string") temp <- unlist(strsplit(string, split = "\\s+")) replacement <- c(1000, 2000, 3000) temp[temp == "100"] <- replacement result <- paste(temp, collapse = " ") result ## [1] "1000 2000 test 3000 string"
Comments
Post a Comment