- #1
FallenApple
- 566
- 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.
Here are some examples: http://www.endmemo.com/program/R/cut.phpFallenApple 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 x is an array of 100 random normal values. (The R code is shown in the link.)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]
The first value above lies in the interval (-1, 0]. The second value above lies in the interval (0, 1], and so on.[1] -0.154103462 0.271704132 -0.234160855 0.764474679 0.438237645
A vector in R is a data structure that stores multiple values of the same data type in a single variable. It is represented by a set of values enclosed in c() function.
To create a vector in R, you can use the c() function and pass in the values you want to store in the vector. For example, c(1, 2, 3) will create a vector with values 1, 2, and 3.
To cut a vector in R, you can use the square bracket notation and specify the indices of the elements you want to extract. For example, v[2:5] will extract elements 2 to 5 from the vector v.
Vector indexing in R refers to extracting specific elements from a vector by specifying their indices. On the other hand, vector subsetting involves extracting elements based on a logical condition or a vector of logical values.
Yes, you can modify a vector while cutting it in R by assigning new values to the indices you are extracting. For example, v[2:5] <- 10 will replace elements 2 to 5 in the vector v with the value 10.