How to Split a Vector into Two Parts in R?

  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Cut Vector
AI Thread Summary
To split a vector in R into two parts based on a threshold, the cut function can be utilized effectively. The goal is to separate the vector into values less than 150 and those greater than 150. While the cut function may initially seem confusing, it can be simplified by specifying appropriate break points. For instance, using the command cut(vec, breaks=c(-Inf, 150, Inf)) will categorize the vector into two groups: one for values below 150 and another for values above. This method allows for clear segmentation of the data without the need for complex intervals. Additionally, examples provided in external resources illustrate how to use the cut function to create ranges, which can further aid in understanding its application.
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.
 
Technology 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 FallenApple
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top