New to R, so probably a noob question. Consider the following code, in particular the for loop:
library(lubridate)
#Read in all site files in the directory
sitefiles <- list.files(pattern = "\\.csv$") #Get a list of all csv's in dir
sites <- list() #Create an empty list.
sites <- lapply(sitefiles, read.csv)
names(sites) <- gsub("\\.csv$", "", sitefiles) #Rename the list
for (site in names(sites)){
site$time <- dmy_hms(site$timestamp)
#Error: $ operator is invalid for atomic vectors
}
OK, let's try this instead:
for (site in sites){
site$time <- mdy_hms(site$timestamp)
}
It appears to do nothing to the data frames in the list sites
. In particular the command colnames(sites[[1]])
is the same before and after running the for loop - no column has been added.
But, there was a change. Rstudio tells me there is a new variable, a data frame called site
, which DOES have the column time added. What the heck???
What is going on here? How do I execute this command successfully?