RobertF
RobertF

Reputation: 904

Trying to create a count variable in R

I'm attempting to create a counter variable in R which loops through the n rows of my 442 column dataframe and increases the counter by 1 on every 55th row.

I've tried the following code:

dataset$num=ceiling(row(dataset)/55)

which works fine, however R duplicates the function for every column in my dataframe rather than simply creating a single new column containing the counter variable. So I have 442 copies of the same variable titled num.1, num.2, ..., num.442.

What am I doing wrong? Thanks!

Upvotes: 0

Views: 1242

Answers (1)

Thomas
Thomas

Reputation: 44525

It sounds like you just want something like:

rep(1:1000,each=55,length.out=nrow(dataset))

The 1000 here could be anything as long as it's larger than nrow(dataset)/55.

Upvotes: 3

Related Questions