Pass a list of data frames in a loop for combining and changing names at the same time

I have a dfs group with different headers that I need to line up: I was hoping to change the column names so that each one has the same name.

df1

 name, score
 smith, 3
 smith, 7
 smith, 5

df2

 type, price
 food, 3
 food, 5
 food, 2.6

I want to combine this so that each of them has the same name and is united by lines. I have 32 of these data frames with different headers, so I plan to use a loop

I made a list of dfs

 groups <- c(df1, df2, df3, etc)

then tried to merge into empty df:

 new_df <- data.frame(words=character(),numbers=numeric())

 for (i in 1:length(groups))
 {
 x <- data.frame(words=character(),numbers=numeric())
 x[,1] <- groups[i]
 x[,2] <- groups[i+1]
 new_df <- rbind(new_df, x)
 }

Unfortunately, it just returns an empty df with a bunch of warnings. Can someone tell me how to fix this? I expected new_df to be;

 words, numbers
 smith, 3
 smith, 7
 smith, 5
 food, 3
 food, 5
 food, 2.6    

I am sure it is easy for people with a little more experience than me. thank

+4
source share
2

32 data.frames list? :

dfList <- mget(paste0("df", 1:32))

lapply:

dfList <- lapply(dfList, setNames, nm = c("words", "numbers"))

, , rbind :

DF <- do.call(rbind, dfList)
+5

, do.call rbind.data.frame . do.call , rbind.data.frame - rbind .

, , df1$words df2$words , .

new.df <- do.call(what = rbind.data.frame,
                  args = list(df1,df2))

EDITED TO ADD: , rbind(). , , R.

, David Arenburg, rbind() , . , setNames() , .

list_of_dfs <- list(df1,df2,df...)
list_of_dfs <- lapply(X = list_of_dfs,
                      FUN = setNames,
                      nm = c("words","numbers"))
big.df <- do.call(what = rbind.data.frame,
                  args = list_of_dfs)
+1

Source: https://habr.com/ru/post/1589022/


All Articles