Reputation: 97
I would like some help with understanding and fixing an error message that I encountered when I executed the command View(combined_data)
. The error message that appeared was:
r error 4 (Error in vec_data(x) * TICS_PER_SECOND : non-numeric argument to binary operator )
Prior to encountering this error message, I executed the following code to combine CSV files into a single dataframe using ldply
.
library(plyr) # for the ldply function
library(here) # for the here function
library(readr) # for the read_csv function
mydir = here("folder", "subfolder")
myfiles = list.files(path=mydir, pattern="*.csv", full.names=TRUE)
combined_data = ldply(myfiles, read_csv, skip=5) # skipping 5 rows in each file
As far as I could tell, this code worked correctly (the combined_data had correct number of rows and columns). When I ran the the command str(combined_data)
, R reported that the error was caused by the column $ In Bed Time (class "hms" "difftime").
My vain attempt to correct this was to convert some of the variables (numeric was imported as character, dates were imported as character) to their correct classes (converting character back to numeric and dates respectively). This did not have the desired effect and resulted in further issues.
combined_data[8:18] <- as.numeric(unlist(combined_data[8:18]))
combined_data[c(2,4,6)] <- format(as.Date(unlist(combined_data[c(2,4,6)]), format="%m/%d/%Y"), "%m/%d/%Y")
combined_data[c(3,5,7)] <- format(as.POSIXct(unlist(combined_data[c(3,5,7)]), format="%I:%M:%S %p"), "%I:%M:%S %p")
Variables 2,4,6 are dates and formatted as 8/20/2020
Variables 3,5,7 are times and formatted as 12:35:00 AM.
After this, I could execute the command View(combined_data)
but all my time variables (originally class "hms" "difftime") were changed to NA's. Also, while my dates displayed correctly, their class did not change to Date.
Upvotes: 0
Views: 747