Produce a list of new dataframes in a for loop, then assign values

Hi all,

is there a simple way to produce a list of new dataframes and assigning it values?

I have a few small dataframes, each with the same variables within (say Aus_df, Canada_df, US_df). I want to create a transposed version of each dataframe, and call them "t_original dataframe name, which is populated by a transposed version of the original data.

for (i in c("Aus_df", "Canada_df", "US_df"){

  name <- paste("t", i, sep = "_")
  assign(name,  transpose(as.data.frame(i)))

}

This gets the names right, but there's something wrong with the assign function, the new dataframe only has its name within - so t_Aus_df only has Aus_df within it instead of the tranposed Aus_df data.

Thank you for your time!

So that means at the first iteration of the loop, i will contain "Aus_df". Yes, just the string "Aus_df", not the corresponding data frame.

The easiest workaround is to store actual your data frames in a vector. Of course, a classic "atomic" vector can't do that, you need a list:

for(i in list(Aus_df, Canada_df, US_df)){
   transpose(i)
}

But in that case you can't use paste(i) anymore, since i is no longer a string. So, what you need is to have:

  1. a list of dataframes
  2. a list of names that i loops on, that you can use both to extract the data frames and as variable name.
df_before_transpose <- list(Aus_df, Canada_df, US_df)
names(df_before_transpose) <- c("Aus_df", "Canada_df", "US_df")

for(i in names(df_before_transpose)){
  name <- paste("t", i, sep = "_")
  assign(name,  transpose(df_before_transpose[[i]]))
}

But now you can start to see how interesting this list structure is, that way you don't need to keep separate variables for each country, with complex naming schemes.

t_df <- list()
for(i in names(df_before_transpose)){
  t_df[[i]] <- transpose(df_before_transpose[[i]])
}

Or even better, using lapply() to avoid thinking about the looping process yourself:

t_df <- lapply(df_before_transpose, transpose)

Finally, are you sure that you want to use transpose()? The function I can think of is the one from purrr and it might not do want you think it does (it "inverts" a list inside-out). If you're looking for matrix transposition, the function is t(). But you should note that it converts everything to a matrix, which can have its own downsides (e.g. all columns have the same type).

class(t_df$Aus_df)
#> [1] "matrix" "array" 
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.