I´m working on project with n data frames. Before i can work with them some processing is nescessary. One of these operations is to add a column for "year", "month" and "day" to each data frame. The also got and keep a column "date" in the format "yyyy - mm - dd".
dateiNamen<-dir(path="input_folder",pattern = "merged", all.files = T)
Auswertung<-function(Dateiname){
setwd("input_folder")
data<-read.delim(Dateiname, stringsAsFactors = FALSE)
data$date<-as.Date(data$date)
data<-subset(data, data$date>"1994-12-31")
data<-data[,colMeans(is.na(data))<0.15]
Ausgabedatei <- paste0("output_folder",Dateiname, "_bearbeitet",".csv")
write.table(data, file = Ausgabedatei, row.names = F, col.names = T, sep=",")
setwd("Project_folder")
}
fun<-lapply(dateiNamen, Auswertung)
That´s the first step and it works as it should, after this the "date" column (XX$date) of any of these data frames got the following structure:
head(XX$date)
[1] "1995-01-01" "1995-01-02" "1995-01-03" "1995-01-04" "1995-01-05" "1995-01-06"
str(XX$date)
Date[1:9131], format: "1995-01-01" "1995-01-02" "1995-01-03" "1995-01-04" "1995-01-05" "1995-01-06" "1995-01-07" "1995-01-08" "1995-01-09" "1995-01-10" ...
class(XX$date)
[1] "Date"
dput(head(XX$date))
structure(c(9131, 9132, 9133, 9134, 9135, 9136), class = "Date")
Now, i apply my second function on the data frames to create the colums for year, month and day in each data frame:
dateiNamen2<-dir(path="output_folder",pattern = "merged", all.files = T)
Spalten<-function(Dateiname){
setwd("output_folder")
data<-read.csv(Dateiname,header = TRUE, ";")
data<-data%>%
dplyr::mutate( year = lubridate::year(date),
month = lubridate::month(date),
day = lubridate::day(date))
Ausgabedatei <- paste0("output_folder_2",Dateiname, "_bearbeitet",".csv")
write.table(data, file = Ausgabedatei, row.names = F, col.names = T, sep = ",")
setwd("Project_folder")
}
fun2<-lapply(dateiNamen2, Spalten)
When i apply this function i get the following error:
**Error in as.POSIXlt.default(x, tz=tz(x)):
dont know how to convert "x" in class ""POSIXlt""**
I cant figure it out what is causing this problem, cause if i´m running the same commands on a single file it works perfectly. Maybe one of you sees something i missed.
And yeah the functions could be "prettier", but they are the first functions i ever wrote. i´ll keep trying! :)
Can you try this function :
read.csv2since it has defaultsep = ";"andheaderis alwaysTRUEforread.csvfunctions.read.csv2so better we convert it to "Date" class first."output_folder_2"is the name of the folder it should have a"/"to separate filename and folder name.list.filesinstead ofdirto create file names withfull.names = TRUEso that it returns the full path of the file and we don't have to usesetwd.AuswertungandSpaltenfunctions in one function and read and write the files once instead of doing it separately in each function.