Let me know in the comments below, in case you have any additional questions.
#For loop in r where output is list how to#
In this R tutorial you learned how to store the results created in a for-loop in a list. You can find a selection of articles here: I’m explaining the topics of this article in the video:įurthermore, you could have a look at some of the related articles on my homepage. Would you like to know more about loops and lists? Then you may watch the following video of my YouTube channel. of 3 variables:Ĭreated on by the reprex package (v0.2.My_list # Print final list # ] # 1 1 1 1 1 # ] # 2 2 2 2 2 # ] # 3 3 3 3 3Īs you can see based on the previous output of the RStudio console, we have created a list with three list elements.
#> $ DATEN3:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'ame': 2 obs. #> $ DATEN2:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'ame': 2 obs. This isnt particularly useful output, but it can be beneficial to build up your loops in this way using print statements so you know your loop is behaving as. #> $ DATEN1:Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'ame': 2 obs. You also can continue using list and purrr, i.e not row bind to a ame DATEN_list % First, we have to create an empty list: mylist <- list () Create empty list mylist Print empty list list () mylist <- list () Create empty list mylist Print empty list list () Now, we can write and. You’ll then be able to apply some operation, by dataset using group_by # sum of all column for each DATENi In this Section, I’ll illustrate how to store the results of a for-loop in a list in R. #> Warning: le package 'purrr' a été compilé avec la version R 3.5.2 Write.csv(df, file.path(tempdir(), paste0("test", i, ".csv")), row.names = FALSE) I took the example back for previous answer df = read.table(text = These will allow you to continue processing further. Here is another approach not using for loop, but using tidyverse tools.
The elements of the list can be accessed individually for further processing. Instead, use a single list variable to do so. In summary, you don't need to create separate variables to store the contents of each csv file. The apply family of functions in base R (apply(), lapply(), tapply(), etc) solve a similar problem, but purrr is more consistent and thus is easier to learn. Your DATEN variable is then a list with three elements, each of which is named as per the name of the csv file that was read in: length(DATEN)Īs with all lists, you can refer to the individual elements by element number: DATEN]
#For loop in r where output is list code#
I have also read in the csv files without using their headers, to keep the code as close to your example as possible. You would have to put the path arguments back in to make the example apply in your own case. Note that I have left out the path components you've listed, as the stated path is specific to your circumstances and not directly reproducible. Now that there are some csv files created these can be read in one step using an anonymous function within sapply, a variant of lapply which I've used to retain the csv file names as the names of the individual list elements. Write.csv(df, paste0("test", i, ".csv"), row.names = FALSE) That way you don't have to create three separate variables in your global environment when there is no need to do so.Īs we don't have access to your data I need to set up some test data for a reproducible example: df = read.table(text =
It is simpler if you don't use a for loop but instead use one of the *apply functions to generate a list with all three files within it.