Referring to question substr in dplyr %>% mutate, and to @akrun 's answer, why do the two created columns give the same answer?
df <- data_frame(t = '1234567890ABCDEFG', a = 1:5, b = 6:10)
df %>% mutate(u = substr(t, a, a + b), v = substring(t, a, a + b))
I can't grasp the difference with the situation in the original question. Thank you!
The difference is in the vectorization
The
substr
returns only a single value while thesubstring
returns avector
oflength
equal to the number of rows in the dataset 'df'. As there is only a single value output, it gets recycled in themutate
. However, if we are using multiple values i.e.Then, the output is the same. In the OP's example, it gets the above output as the
x
insubstr
has the same length asstart
andstop
. We can replicate the first output with