user4824195
user4824195

Reputation: 119

group and label rows in data frame by numeric in R

I need to group and label every x observations(rows) in a dataset in R.
I need to know if the last group of rows in the dataset has less than x observations

For example: If I use a dataset with 10 observations and 2 variables and I want to group by every 3 rows. I want to add a new column so that the dataset looks like this:

    speed     dist       newcol
        4        2            1
        4       10            1
        7        4            1
        7       22            2
        8       16            2
        9       10            2
       10       18            3
       10       26            3
       10       34            3
       11       17            4

Upvotes: 2

Views: 1116

Answers (2)

vagabond
vagabond

Reputation: 3594

df$group <- rep(1:(nrow(df)/3), each = 3)

This works if the number of rows is an exact multiple of 3. Every three rows will get tagged in serial numbers.

A quick dirty way to tackle the problem of not knowing how incomplete the final group is to simply check the remained when nrow is modulus divided by group size: nrow(df) %% 3 #change the divisor to your group size

Upvotes: 2

Mamoun Benghezal
Mamoun Benghezal

Reputation: 5314

assuming your data is df you can do

df$newcol = rep(1:ceiling(nrow(df)/3), each = 3)[1:nrow(df)]

Upvotes: 1

Related Questions