R – cut a vector

rstatistics

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?

Best Solution

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...