0

I am still relatively new to R and any tips would be appreciated

So I want to parse the time for specific dataframes and what I've tried to do was create a list and adjust the names accordingly.

mydir = "Data/Fitabase_Data_4.12.16-5.12.16"
csvfiles = list.files(path = mydir, pattern ="*.csv", full.names = TRUE)

# Create list of names without ".csv".
names <-substr(csvfiles, 36, 100)
names <- gsub("\\.csv", "", names)

# Create for loop to individually read each .csv.
for(i in names){
  filepath <- file.path("Data/Fitabase_Data_4.12.16-5.12.16", paste(i, ".csv", sep = ""))
  assign(i, read.csv(filepath))
}
hourlyfiles = list.files(path = mydir, pattern = "hourly*", full.names = TRUE)
hourlyfiles <- gsub("Data/Fitabase_Data_4.12.16-5.12.16/", "", hourlyfiles)
hourlyfiles <- gsub("\\.csv", "$ActivityHour", hourlyfiles)

Below is the first three rows of one of the tables and I am trying to parse the date and time in order to upload the dataset somewhere else.

Id ActivityHour Calories
1503960366 4/12/2016 12:00:00 AM 81
1503960366 4/12/2016 1:00:00 AM 61
1503960366 4/12/2016 2:00:00 AM 59

The code below returns an error: "All formats failed to parse. No formats found."

# Create for loop to parse each file
for(i in hourlyfiles){
  parse_date_time(i, "%m/%d/$y %I:%M:%S %p")
}
DNW78
  • 1
  • 2
  • You need a step to read the data (ex. `read.csv()`) before parsing. One example is https://stackoverflow.com/q/62598787/10276092 – M.Viking Aug 18 '22 at 20:09
  • I've read all the .csv files into RStudios. I will add that into the post for clarification. Thank you! – DNW78 Aug 18 '22 at 20:10
  • Inside the loop, something like `print(get(i))` works on the table, however your current code isn't telling the parse_data_time() function to focus on the ActivityHour variable. Consider refactoring your approach. Rather than having many hourly files objects in memory, see this answer https://stackoverflow.com/a/9084146/10276092 – M.Viking Aug 18 '22 at 21:40

0 Answers0