I need your help. I have a data frame looks like below
id home_1 home_2 home_3
1 1 -0.07288651 -1.0946734 0.06310788
2 2 0.27480575 -0.5939264 -0.10267407
3 3 -1.29267610 -1.0765848 -0.96190129
4 4 -0.53468273 0.5315489 -1.36055340
...
I want to create 3 data frames; df1, df2 and df3
- df1 would have a table with sorted column 'home_1'
- df2 would have a table with sorted column 'home_2'
- df3 would have a table with sorted column 'home_3'
Please find the codes below
dummy <- data.frame(id = 1:10,home_1 = rnorm(10),home_2 = rnorm(10),home_3 = rnorm(10))
f <- function(df,param1, param2) {
c <- paste0(param1, "_", param2);
print(paste0("Let's sort column ", c))
df %>% arrange(c) %>% print() #sort dataframe by column 'home_1/2/3'
}
for (i in 1:3) {
print(paste0("Index : ",i))
table <- paste0("df",i)
table <- f(dummy,"home",i) # create dataframe with name df1/2/3
}
Problem 1 Then I run my code, the function cannot detect the respective column. The errors inside my function
Error in grouped_df_impl(data, unname(vars), drop) :
Column `c` is unknown
the local variable c does exist, but the group_by function cannot detect c.
Does anybody know how to make Column 'c' to be detected by group_by function?
Problem 2 Same problem with my for loop. I want to create a data frame name that is dynamic.
However, this following function table <- f(dummy,"home",i), created a data frame with name 'table' instead of 'df1'.
Can anybody give me a hint on how to resolve these issues? Thank you in advance.
df1<- df[order(df$home_1),] df2<- df[order(df$home_2),] df3<- df[order(df$home_3),]As I suppose, solution such like this won't satisfy you, will it? - Adammlapply()you have list of df's. However you can acces each row and column of df in list by index. - Adamm