Home > Mobile >  change every nth element of a vector R
change every nth element of a vector R

Time:07-21

I have:

ls = 1:14

I change value of every element except the first, last, and every 4th element from counting from the end

I would like:

ls = c(1, 2, "", "", "", 6, "", "", "", 10, "", "", "", 14)

Any suggestions how to achieve this?

CodePudding user response:

Something like this would work:

ls[-c(1, seq(14, 1, -4))] = ""
  •  Tags:  
  • r
  • Related