EDS
EDS

Reputation: 2195

Creating uneven sequences in R

Given df=data.frame(x = seq(1:100), y = rnorm(100, mean=3, sd=0.5)) I would like to create a new vector whose ith element is determined by the row in question. If it's one of the first 3 elements after subsetting the data into 5 element subsets, I'd like to put an "a" otherwise a "b".

Output would look like so:

1   2.6    a
2   3.5    a 
3   2.6    a
4   2.7    b
5   2.1    b
6   1.8    a
7   3.7    a
8   2.9    a
9   2.7    b
10  3.4    b

The only thought I have is that this questions boils down to how one would create uneven sequences, hence the title. Something like if the ith row is a member of each sequence created by ((5*j)-4):((5*j)-2), then call it a, otherwise b. But how could I create a vector of these values? Something like the below would of course not work because each element in rows is itself a sequence and not all the numbers in the sequence.

>rows=vector()
>for (j in 1:(nrow(df)/5)) {
     rows[j]=((5*j)-4):((5*j)-2)
     }
>classify=vector()
>for (i in 1:(nrow(df))) {
  if (is.element(df[i,1], rows)) {
    classify[i]="a"
  } else {
    classify[i]="b"
  }
}
>df=cbind(df, classify)

Upvotes: 1

Views: 401

Answers (1)

tmfmnk
tmfmnk

Reputation: 40171

You can try:

rep_len(c(rep("a", 3), rep("b", 2)), nrow(df))

      x        y z
1     1 3.467233 a
2     2 2.599982 a
3     3 3.941228 a
4     4 2.833142 b
5     5 4.070231 b
6     6 3.835760 a
7     7 3.688950 a
8     8 2.882646 a
9     9 3.071788 b
10   10 3.358480 b

Upvotes: 1

Related Questions