'Hi, I hope someone could help me with this basic question. This is about grouping a vector into pairs and inpairs with R for a rnorm vector
Basically I created the following vector using x <- rnorm(100,0,1).
I would like to group the pairs into vector p and impairs into vector i, could someone help me, thanks?
Solution 1:[1]
Let's create sample() data with randomly sampling 10 integers (i.e. whole numbers, that can be written without a fractional component) between 0 and 100 and store it in vector x.
set.seed(1) # make it retroducible
x <- sample(1:100, 10)
> x
[1] 68 39 1 34 87 43 14 82 59 51
Now let's test if the elements of vector x are devisible by 2 (stored to vector even) or indevisible by 2 (stored to vector odd).
odd <- x[which(x %% 2 != 0)]
even <- x[which(x %% 2 == 0)]
> odd
[1] 39 1 87 43 59 51
> even
[1] 68 34 14 82
Next, let's create a vector y containing 10 normally distributed normal numbers (with mean 0 and standard deviation 0, as in your example).
set.seed(1) # make it retroducible
y <- rnorm(10, 0, 1)
> y
[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884
Again, let's test whether the elements of y are devisible by 2.
indevisible_by_two <- y[which(y %% 2 != 0)]
devisible_by_two <- y[which(y %% 2 == 0)]
> indevisible_by_two
[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884
> devisible_by_two
numeric(0)
However, this only tells us that the elements of vector y are not devisible by 2 and does not mean that they are even or odd. Since rnorm() doesn't lead to integers, the attributes of odd and even do not apply.
Therefore, your initial question to split x <- rnorm(100,0,1) into odd and even numbers is mathematically impossible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
