I wish to have some advice on this problem in R. I have a data frame "my_fruits_data" with many columns including the index columns as below in name_cols. I want to filter those index columns one by one with a for loop and store the filtered records in respective data frames with their names listed in df_fruits for post-processing. Apparently, it doesn't work as df_fruits elements are strings rather than actual data frame names. I've searched and got a few hints but none of them actually helped.
# column names
name_cols <- c("Index_apple",
"Index_pear",
"Index_orange",
"Index_watermelon",
"Index_strawberry"
)
# dataframe names for filtered result
df_fruits <- c("df_apple",
"df_pear",
"df_orange",
"df_watermelon",
"df_strawberry")
for (i in name_cols)
{
df_fruits[i] <- my_fruits_data %>%
filter (.data[[name_cols[i]]] ==1)
......
}
Thanks chase77