I have two variables: X and state which are given below
set.seed(3)
state <- rbinom(15,4,0.6)
X <- c(1:15)
X
state
and the output is
> state
[1] 3 2 3 3 2 2 4 3 2 2 2 2 2 2 1
> X
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
I want to select the Xs corresponding to the same state. Any idea how to do this in R?
Using
splityou get a list of 4 statesgenerally we use ,
aveto perform some operations while grouping.For example here I get the mean of X by state: