How do we append a single value to multiple positions in a vector?
x=c(1,2,3)
append(x, "a", c(1,3))
[1] "1" "a" "2" "3"
Warning messages:
1: In if (!after) c(values, x) else if (after >= lengx) c(x, values) else c(x[1L:after], :
條件的長度 > 1,因此只能用其第一元素
2: In if (after >= lengx) c(x, values) else c(x[1L:after], values, :
條件的長度 > 1,因此只能用其第一元素
3: In 1L:after : numerical expression has 2 elements: only the first used
4: In (after + 1L):lengx :
numerical expression has 2 elements: only the first used
With the above code, only the first position is registered, with a warning message.
lapply(c(1,3), function(y) append(x, 'a', y))
yields this result:
[[1]]
[1] "1" "a" "2" "3"
[[2]]
[1] "1" "2" "3" "a"
Expected output:
1 a 2 3 a