1

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
4
  • 1
    Once objects are saved to a list their original sources are not connected and remain as separate objects. The sheets data frames are never reassigned values so they remain empty. Commented Dec 31, 2018 at 21:26
  • You've only modified the contents of sheet_dfs but not sheet1, sheet2, or sheet3. You'll notice that names(sheet_dfs) doesn't even relate back to sheet1... Commented Dec 31, 2018 at 21:29
  • Is there a way to name these dataframes that I am creating using the "i" from the loop? Commented Dec 31, 2018 at 21:30
  • 1
    Probably want: sheets_df["sheet1"] Commented Dec 31, 2018 at 21:39

1 Answer 1

2

First, you need to create a proper named list. Your initial list assignment just creates a vector via c. Furthermore, you need to access the list elements properly via [[ or name. See below modification of your code.

# set up named list programmatically
sheet_names = paste0("sheet", 1:4)
sheet_dfs = setNames(vector(length = length(sheet_names), mode = "list"), sheet_names)
# you can also create a named list as follows, which is, however, less programmatically
# sheet_dfs = list("sheet1" = sheet1, "sheet2" = sheet2, ...)

# your temp assignments...
#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(County = c("Cobb","Clayton","Fulton"), Winner = c("Kemp","Abrams","Smith"))
  temp2 <- data.frame(County = c("Henry","Polk","Gwinnett"), Winner = c("Fuller","Parker","Newsome"))
  sheet_dfs[[i]] <- rbind(temp1,temp2)
}

# access the list via name or index
sheet_dfs[[1]]
sheet_dfs["sheet1"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.