Reading in and manipulating datasets in looping fashion

I am wondering how to read multiple datasets (CSV) into R and then perform the same functions on each dataset one at a time in a looping fashion. I don't know how to upload and show these datasets, however, I have attached a screenshot of what the data looks like. It is time-series data for various stocks. The files are all very similar to each other and are all in the same folder on my computer. If you guys need more information or a more in-depth explanation of the problem I am trying to solve please let me know! Thank you all.

The actions I am performing are as follows:

ONTX30 <- ONTX %>%
mutate(as.POSIXct(ONTX$date_time, tz = "America/New_York"))
as_tsibble(index = date_time) %>%
index_by(date_time30 = ~ floor_date(., "30 minute")) %>%
summarise(open30 = first(open), close30 = last(close), min30 = min(low), max30 = max(high), volume30 = sum(volume))

Hi @B_elias2 ,

What you are trying to achieve can easily be performed with functional programming. If you have never heard of this terminology, it is simply the action of applying the same function to several datasets in a sequential (looping) fashion without explicitly using a loop.

Could you upload a few of your datasets to a cloud system (e.g. Google Drive or Dropbox) and share it here? I'll help you write a code that will perform what you are looking for.

1 Like


2nd dataset

Will these hyperlinks suffice?

Hi @B_elias2

The main issue with the two datasets is that even though they look similar, they are not similar in the relevant ways. For example, both datasets have a date_time column; however, the first one is of the datetime class, but the second one is of the double (numeric) class. That's a problem, especially because the actions you are trying to perform on both datasets make use of this column.

The second issue is that the two datasets have different columns. This does not really affect the actions you specified above, but it may affect future actions. The first dataset has the "Hours" and "Dates" columns and the second dataset has the "correct_date_time" and "date" columns.

I can easily fix this; however, the code I will send you if I do will not be very effective. Could you please send me two datasets, which are similar so I can show you how to achieve your goal?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.