1

I am very new to R and would like to know how to make something like a 'for loop'? How to call the list in my file as a value in a 'for-loop-like' command? This is my Rscript:

lapply(file_list, function(df){

data_"df" <- data_col %>% filter(col1 == "df") %>% arrange(desc(col2))

NULL
})

However, either the error is in "df" (unexpected string constant in " data_"df"") or if I removed the "data_" returns all NULL.

In bash script, It would've look something like this:

for df in $(cat file_list)
do
    [command that loops for $df] > data_"$df"
done

In details: there are two files, file_list and data_col.
file list contains the following:

Name_A
Name_B
Name_C

and data_col

col1 col2 col3
Name_A 9 apple
Name_B 7 orange
Name_C 8 banana
Name_B 2 dragonfruit
Name_A 3 melon
Name_A 7 grapes

Expected output of data_Name_A

col1 col2 col3
Name_A 9 apple
Name_A 7 grapes
Name_A 3 melon

I will appreciate your help. Thanks!

Milky
  • 13
  • 3
  • 1
    Does this answer your question? [Split data.frame based on levels of a factor into new data.frames](https://stackoverflow.com/questions/9713294/split-data-frame-based-on-levels-of-a-factor-into-new-data-frames) – jared_mamrot Jul 05 '22 at 01:37

0 Answers0