How to Split a Vector into Two Parts in R?

  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Cut Vector
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
FallenApple
Messages
564
Reaction score
61
How do a cut a vector in R? Say I want to split a vector to two parts. One for everything less than 150 and other for everything greater than 150. I tried the cut function but it seems confusing.
 
Physics news on Phys.org
FallenApple said:
How do a cut a vector in R? Say I want to split a vector to two parts. One for everything less than 150 and other for everything greater than 150. I tried the cut function but it seems confusing.
Here are some examples: http://www.endmemo.com/program/R/cut.php
From the link above:
Divide the data into ranges -5 ~ 5:
> c <- cut(x,breaks=-5:5)
> c
[1] (-1,0] (0,1] (-1,0] (0,1] (0,1] (-1,0] (1,2] (0,1] (1,2]
< the rest is omitted>

10 Levels: (-5,-4] (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] ... (4,5]
Here x is an array of 100 random normal values. (The R code is shown in the link.)
The cut function breaks the array up into intervals from -5 to 5, with a step size of 1.
The first few values in the array are
[1] -0.154103462 0.271704132 -0.234160855 0.764474679 0.438237645
The first value above lies in the interval (-1, 0]. The second value above lies in the interval (0, 1], and so on.

As for the question you asked, try something like cut(vec, breaks=0:150:300)

Here vec is your vector, and the breaks are at 0, 150, and 300.
 
  • Like
Likes   Reactions: FallenApple