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:
- a list of dataframes
- 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"