r - Why are these sequences reversed when generated with the colon operator? -
i've noticed when try generate list of sequences :
operator (without anonymous function), sequences reversed. take following example.
x <- c(4, 6, 3) lapply(x, ":", = 1) # [[1]] # [1] 4 3 2 1 # # [[2]] # [1] 6 5 4 3 2 1 # # [[3]] # [1] 3 2 1
but when use seq
, fine.
lapply(x, seq, = 1) # [[1]] # [1] 1 2 3 4 # # [[2]] # [1] 1 2 3 4 5 6 # # [[3]] # [1] 1 2 3
and help(":")
stated
for other arguments
from:to
equivalentseq(from, to)
, , generates sequencefrom
to
in steps of 1 or -1.
why first list of sequences reversed?
can generated forward sequences way colon operator lapply
?
or have use lapply(x, function(y) 1:y)
?
the ":" operator implemented primitive do_colon function in c. primitive function not have named arguments. takes first parameter "from" , second "to" ignorning parameter names. see
`:`(to=10, from=5) # [1] 10 9 8 7 6 5
additionally lapply
function passes it's values leading unnamed parameter in function call. cannot pass values primitive functions via lapply
second positional argument.
Comments
Post a Comment