At the start of my code I read a csv and split it up into different DataFrames grouped by hardcoded IDs and add those DataFrames to a List. I afterwards I iterate through the list in order to perform the same calculations on every DataFrame and my output is a plot and a numerical value for each DataFrame. (See Below)
#Read from CSV
SourceCSV= read.csv("files/dt1Summary.csv",header=TRUE, sep = ';', row.names = NULL)
#Reading Source Data into seperate Frames by ID
ID110=SourceCSV[SourceCSV$ID==110,]
ID1103=SourceCSV[SourceCSV$ID==1103,]
ID2983=SourceCSV[SourceCSV$ID==2983,]
ID28=SourceCSV[SourceCSV$ID==28,]
#Add them all to a List
IDList = list(ID110, ID1103, ID2983, ID28)
#Do stuff by iterating through IDList
...
I now want to make the code more adaptive to different input. I tried to create dynamic names for the DataFrames ("ID" + dynamic value) and add an unknown amount of IDs to them. When I run the code I am succeeding in splitting up the SourceCSV into its unique IDs and the DataFrames have the exact same content as above. However, I don't know how to add them to a list. The code below is how I tried to assign the DataFrames and add them to the list in the same step, but my list still has the values I initially assigned (4 Dataframes with one value inside: num 1).
#Read from CSV
SourceCSV= read.csv("files/dt1Summary.csv",header=TRUE, sep = ';', row.names = NULL)
#Reading Source Data into seperate Frames by ID
IDList <- rep(list(data.frame(1)),length(unique(SourceCSV$ID)))
for(i in 1:unique(SourceCSV$ID))
{
IDList[i] <- assign(paste("ID", unique(SourceCSV$ID)[i], sep = ""),data.frame(SourceCSV[SourceCSV$ID==unique(SourceCSV$ID)[i],]))
}
#Do stuff by iterating through IDList
...
I have 3 Questions and Appreciate every Input to apply more best practices since I read that assing(paste(...),...)
should almost always not be done.
Question 1
How can I directly insert the DataFrame into IDList
upon creation? (As tried inside the for Loop)
Question 2
Is this the right way to do it or would there be a way to work with IDs seperately inside the SourceCSV-DataFrame in order to avoid Looping through a list 500 times?
Question 3
What other things look bad in my code, or my question on Stack Overflow that I could improve upon?
I Appreciate every input and you can use this as the dt1Summary.csv testfile:
Name;EndTime;exception reason_1;exception reason_2;Date;ID;StartTime;Status
Jon Doe;;;;01.01.2020;9999;;;
Jon Doe;01.01.2020 16:42:38;;;01.01.2020;420;01.01.2020 16:42:36;Completed
Jon Doe;01.01.2020 16:56:47;;;01.01.2020;28;01.01.2020 16:56:45;Completed
Jon Doe;01.01.2020 17:05:12;;;01.01.2020;1;01.01.2020 17:05:10;Completed
Jon Doe;01.01.2020 18:13:01;;;01.01.2020;9999;01.01.2020 18:12:57;Completed
Jon Doe;01.01.2020 18:25:22;;;01.01.2020;420;01.01.2020 18:15:03;Completed
Jon Doe;01.01.2020 18:30:16;;;;01.01.2020;1;01.01.2020 18:30:14;Completed
Jon Doe;01.01.2020 18:52:36;just because I can;;01.01.2020;420;01.01.2020 18:51:03;Failed