I am a new R programmer. I am trying to use rbind to combine temporary data frames. I need the names of each set of combined datasets to come from a list of data frames that a loop iterates through.
I can get the temporary files to combine properly, but it doesn't work if I use the data frame name from iterating through the name list. I have searched and searched and also called a programmer friend, to no avail. Thank you!!
#initialize empty data frames
sheet1 <- data.frame(County = character(), Winner = character())
sheet2 <- data.frame(County = character(), Winner = character())
sheet3 <- data.frame(County = character(), Winner = character())
sheet4 <- data.frame(County = character(), Winner = character())
#put empty data frames into a list
sheet_dfs <- c(sheet1, sheet2, sheet3, sheet4)
#pull information from different Excel sheets for each iteration (not shown here)
#and write to element of list of dataframes
for (i in 1:3) {
temp1 <- data.frame(c("Cobb","Clayton","Fulton"), c("Kemp","Abrams","Smith"))
colnames(temp1) <- c("County","Winner")
temp2 <- data.frame(c("Henry","Polk","Gwinnett"), c("Fuller","Parker","Newsome"))
colnames(temp2) <- c("County","Winner")
sheet_dfs[[i]] <- rbind(temp1,temp2)}
sheet1 #expecting a data frame with names, what am I doing wrong?
sheet2 #expecting a data frame with names, what am I doing wrong?
sheet3 #expecting a data frame with names, what am I doing wrong?
test <- rbind(temp1,temp2)
test #this is exactly the output I need for my dataframes sheet1,sheet2,sheet3.
returns:
County Winner
1 Cobb Kemp
2 Clayton Abrams
3 Fulton Smith
4 Henry Fuller
5 Polk Parker
6 Gwinnett Newsome
sheet_dfsbut notsheet1,sheet2, orsheet3. You'll notice thatnames(sheet_dfs)doesn't even relate back tosheet1...sheets_df["sheet1"]