-3

Insert 16 as the 3rd element into the sales vector the sales vector is

Sales <- c (8,11,14,20,21,11,18,10,6,22)

how can I insert 16 as the 3rd element into the sales vector?

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Try: `Sales[3]<-16` – Duck Sep 18 '20 at 17:58
  • Maybe take a look at: https://stackoverflow.com/questions/18951248/insert-elements-in-a-vector-in-r or https://stackoverflow.com/questions/18951248/insert-elements-in-a-vector-in-r – Ben Sep 18 '20 at 18:01
  • When you say insert 16 as the third element, do you mean replacing the current third element (output has same length) or adding 16 in front of 14 and having an output length + 1? – Ben Norris Sep 18 '20 at 18:18

1 Answers1

1
sales <- c(sales[1:2], 16, sales[3:length(sales)])

or

index <- 3
number <- 16
sales <- c(sales[1:(index - 1)], number, sales[index:length(sales)])
Pascal Schmidt
  • 223
  • 2
  • 12