0

I want to apply a for loop on a non-specified range of rows in a tibble.

I have to modify the following code that applies a for loop on a specific number of rows in a tibble:

for(times in unique(dat$wname)[1:111]){...} 

In this tibble the range from 1:111 corresponds to a specific file, in fact, the value of the column "File" repeat 111 times. However, in my data, I do not know how many times the same file repeat. For example, I can have a file that repeats for 80 rows and another for 85. How do I tell the loop to look only in the range in which the rows in the column file have the same name?

I need something to say:

for(times in unique(dat$wname)["for each row in the column File with the same name"]){...}

How can I do it?

Mayur Karmur
  • 2,119
  • 14
  • 35
cate
  • 1
  • 1
  • Please provide a reproducible example. See [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for tips on how to do so. – John Coleman Aug 22 '19 at 11:11

1 Answers1

0

You can count the number of rows using nrow, or ncol if you want to use columns,in your dat variable or lenght in unique(dat$wname) and do something like this:

rows= nrow(dat) # OR
rows=length(unique(dat$wname))

for(times in unique(dat$wname)[1:rows]){...}

But a reproducible example would make things a lot easier to understand/answer

G.Fernandes
  • 321
  • 2
  • 4
  • @G.Frenandes, I want to test the stationary of time series data , build ARIMA models, predicting, every 30 rows of the time series, how can I go about it? I have multiple time series data in a long format under the same variable and a single time series has 30 years of data, I want to replicate the 30 years time series in to 4, i.e 4*30= 120 observations. Thank you very much for your assistance! – Stackuser May 26 '20 at 20:55