R - cut a vector

10,498

Solution 1

Simply do:

# Generate some values
vals <- rnorm(503)
# Take the first 253
result <- vals[1:253]

Solution 2

In addition to the above answer, you can pass any vector in the square brackets to reduce your vector. The above case you are dropping the last 250 values. You can play around with the values inside the square brackets to get different results

    # Generate some values
    vals <- rnorm(503)

    # Take the last 250
    result <- vals[250:503]

    # Take 1,3,4,7 values
    result <- vals[c(1,3,4,7)]

    # Take alternate values
    result <- vals[seq(1,503,2)]

etc...

Share:
10,498
maximus
Author by

maximus

about:me ;)

Updated on June 04, 2022

Comments

  • maximus
    maximus almost 2 years

    I have 503 datapoints and I want to cut away the last 250 to get the 253.

    I tried cut ,but these function only gives me groups back...

    How can I do that in R?