Here is what I am trying to accomplish. Say I have this vector
example <- c("a", "b", "N/A", "c", "d", "N/A", "e", "f")
What I am looking to do is for each "N/A", I need to add another one after it. My desired output is
c("a", "b", "N/A", 'N/A", "c", "d", "N/A", "N/A", "e", "f")
for a vector of any length.
I have tried
position <- which(example == "N/A")
for (i in 1:length(example)) {
append(example, "N/A", after = position[i])
}
but it doesn't work. I think the "after" argument only accepts one value.
CodePudding user response:
This can be done with purrr:map2
, Map
or mapply
:
> unlist(purrr::map2(example, (example == 'N/A') 1, rep))
[1] "a" "b" "N/A" "N/A" "c" "d" "N/A" "N/A" "e" "f"
CodePudding user response:
Try:
> position <- which(example=="N/A")
> for(i in rev(position))
example <- append(example, "N/A", after=i)
> example
[1] "a" "b" "N/A" "N/A" "c" "d" "N/A" "N/A" "e" "f"