How to create a for loop to filter within a specific row and to save the output in a new document using R?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I have different data sets that all have several thousand rows and that all have the same structure with 5 columns. A subset of my data looks like this:



INr         FNr         TM          I1              I2
1 1 B2 1598,45 0,14
2 1 B2 930,40 0,11
3 1 B2 107,86 0,04
4 1 B2 881,09 0,11
7 1 B3 2201,98 0,15
8 1 B3 161,30 0,04
9 1 B3 1208,14 0,17
4 2 B3 831,75 0,12
5 2 B3 1027,41 0,14
7 2 B3 2052,16 0,15
8 2 B3 159,63 0,05
9 2 B4 1111,49 0,16
10 2 B4 1312,15 0,12
1 3 B4 863,79 0,10
2 3 B4 104,06 0,04
3 3 B4 816,02 0,11
4 3 B4 1053,02 0,14
5 3 B5 132,32 0,03
6 3 B5 2059,03 0,14
7 3 B5 153,49 0,04
8 3 B5 1118,69 0,15
9 3 B5 1632,66 0,18
10 3 B5 1302,15 0,12


I now have to filter this data frame for all the different values of TM whicha are always a combination of a letter and a number from 1 to 12 (e.g. A1, B2, B3, C4, D6, F8,...). However, not all the letters and not all the numbers are always present as also shown in the table above, where A1 to A12 and B1 are missing.



To filter and save the data, I have already coded a short script that works perfectly fine, but in this way, I have to adapt the parameter for the sorting and the resulting output file every time:



library(tidyverse)
df %>%
filter(TM == "B2") %>%
write.csv(file = "C:/Users/Desktop/B2.csv", row.names = FALSE)


I am sure that there would be a possibility to solve this problem with a for loop but as I am not yet that experienced with the use of loops, I would like to ask you, if you could help me with this issue.



Thank you very much!










share|improve this question


















  • 4





    Possible duplicate of Split dataframe into multiple output files

    – Niek
    Jan 3 at 13:04


















2















I have different data sets that all have several thousand rows and that all have the same structure with 5 columns. A subset of my data looks like this:



INr         FNr         TM          I1              I2
1 1 B2 1598,45 0,14
2 1 B2 930,40 0,11
3 1 B2 107,86 0,04
4 1 B2 881,09 0,11
7 1 B3 2201,98 0,15
8 1 B3 161,30 0,04
9 1 B3 1208,14 0,17
4 2 B3 831,75 0,12
5 2 B3 1027,41 0,14
7 2 B3 2052,16 0,15
8 2 B3 159,63 0,05
9 2 B4 1111,49 0,16
10 2 B4 1312,15 0,12
1 3 B4 863,79 0,10
2 3 B4 104,06 0,04
3 3 B4 816,02 0,11
4 3 B4 1053,02 0,14
5 3 B5 132,32 0,03
6 3 B5 2059,03 0,14
7 3 B5 153,49 0,04
8 3 B5 1118,69 0,15
9 3 B5 1632,66 0,18
10 3 B5 1302,15 0,12


I now have to filter this data frame for all the different values of TM whicha are always a combination of a letter and a number from 1 to 12 (e.g. A1, B2, B3, C4, D6, F8,...). However, not all the letters and not all the numbers are always present as also shown in the table above, where A1 to A12 and B1 are missing.



To filter and save the data, I have already coded a short script that works perfectly fine, but in this way, I have to adapt the parameter for the sorting and the resulting output file every time:



library(tidyverse)
df %>%
filter(TM == "B2") %>%
write.csv(file = "C:/Users/Desktop/B2.csv", row.names = FALSE)


I am sure that there would be a possibility to solve this problem with a for loop but as I am not yet that experienced with the use of loops, I would like to ask you, if you could help me with this issue.



Thank you very much!










share|improve this question


















  • 4





    Possible duplicate of Split dataframe into multiple output files

    – Niek
    Jan 3 at 13:04














2












2








2








I have different data sets that all have several thousand rows and that all have the same structure with 5 columns. A subset of my data looks like this:



INr         FNr         TM          I1              I2
1 1 B2 1598,45 0,14
2 1 B2 930,40 0,11
3 1 B2 107,86 0,04
4 1 B2 881,09 0,11
7 1 B3 2201,98 0,15
8 1 B3 161,30 0,04
9 1 B3 1208,14 0,17
4 2 B3 831,75 0,12
5 2 B3 1027,41 0,14
7 2 B3 2052,16 0,15
8 2 B3 159,63 0,05
9 2 B4 1111,49 0,16
10 2 B4 1312,15 0,12
1 3 B4 863,79 0,10
2 3 B4 104,06 0,04
3 3 B4 816,02 0,11
4 3 B4 1053,02 0,14
5 3 B5 132,32 0,03
6 3 B5 2059,03 0,14
7 3 B5 153,49 0,04
8 3 B5 1118,69 0,15
9 3 B5 1632,66 0,18
10 3 B5 1302,15 0,12


I now have to filter this data frame for all the different values of TM whicha are always a combination of a letter and a number from 1 to 12 (e.g. A1, B2, B3, C4, D6, F8,...). However, not all the letters and not all the numbers are always present as also shown in the table above, where A1 to A12 and B1 are missing.



To filter and save the data, I have already coded a short script that works perfectly fine, but in this way, I have to adapt the parameter for the sorting and the resulting output file every time:



library(tidyverse)
df %>%
filter(TM == "B2") %>%
write.csv(file = "C:/Users/Desktop/B2.csv", row.names = FALSE)


I am sure that there would be a possibility to solve this problem with a for loop but as I am not yet that experienced with the use of loops, I would like to ask you, if you could help me with this issue.



Thank you very much!










share|improve this question














I have different data sets that all have several thousand rows and that all have the same structure with 5 columns. A subset of my data looks like this:



INr         FNr         TM          I1              I2
1 1 B2 1598,45 0,14
2 1 B2 930,40 0,11
3 1 B2 107,86 0,04
4 1 B2 881,09 0,11
7 1 B3 2201,98 0,15
8 1 B3 161,30 0,04
9 1 B3 1208,14 0,17
4 2 B3 831,75 0,12
5 2 B3 1027,41 0,14
7 2 B3 2052,16 0,15
8 2 B3 159,63 0,05
9 2 B4 1111,49 0,16
10 2 B4 1312,15 0,12
1 3 B4 863,79 0,10
2 3 B4 104,06 0,04
3 3 B4 816,02 0,11
4 3 B4 1053,02 0,14
5 3 B5 132,32 0,03
6 3 B5 2059,03 0,14
7 3 B5 153,49 0,04
8 3 B5 1118,69 0,15
9 3 B5 1632,66 0,18
10 3 B5 1302,15 0,12


I now have to filter this data frame for all the different values of TM whicha are always a combination of a letter and a number from 1 to 12 (e.g. A1, B2, B3, C4, D6, F8,...). However, not all the letters and not all the numbers are always present as also shown in the table above, where A1 to A12 and B1 are missing.



To filter and save the data, I have already coded a short script that works perfectly fine, but in this way, I have to adapt the parameter for the sorting and the resulting output file every time:



library(tidyverse)
df %>%
filter(TM == "B2") %>%
write.csv(file = "C:/Users/Desktop/B2.csv", row.names = FALSE)


I am sure that there would be a possibility to solve this problem with a for loop but as I am not yet that experienced with the use of loops, I would like to ask you, if you could help me with this issue.



Thank you very much!







r for-loop filter save export-to-csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 12:58









HotshotHotshot

7111




7111








  • 4





    Possible duplicate of Split dataframe into multiple output files

    – Niek
    Jan 3 at 13:04














  • 4





    Possible duplicate of Split dataframe into multiple output files

    – Niek
    Jan 3 at 13:04








4




4





Possible duplicate of Split dataframe into multiple output files

– Niek
Jan 3 at 13:04





Possible duplicate of Split dataframe into multiple output files

– Niek
Jan 3 at 13:04












1 Answer
1






active

oldest

votes


















2














You should split your data.frame in a list, each element of the list then is printed:



# here you split by the variable you want:
listed <- split(data, data$TM)
listed
# here the partial result
$B2
INr FNr TM I1 I2
1 1 1 B2 1598,45 0,14
2 2 1 B2 930,40 0,11
3 3 1 B2 107,86 0,04
4 4 1 B2 881,09 0,11

$B3
INr FNr TM I1 I2
5 7 1 B3 2201,98 0,15
6 8 1 B3 161,30 0,04
7 9 1 B3 1208,14 0,17
8 4 2 B3 831,75 0,12
9 5 2 B3 1027,41 0,14
10 7 2 B3 2052,16 0,15
11 8 2 B3 159,63 0,05
...


Then R is a vectorized language, so you do not need in this case a loop, but you can use one of the apply functions, to apply to each of the list a function, in this case, write.table:



sapply(names(listed),  # here the object to sapply the function 
# here the function: you have to create the path with paste0
# adding the x to have the correct names
function (x) write.table(listed[[x]], file=paste0("C:\Users\Desktop\",x, ".csv")))




With data:



data <- read.table(text = "INr         FNr         TM          I1              I2
1 1 B2 1598,45 0,14
2 1 B2 930,40 0,11
3 1 B2 107,86 0,04
4 1 B2 881,09 0,11
7 1 B3 2201,98 0,15
8 1 B3 161,30 0,04
9 1 B3 1208,14 0,17
4 2 B3 831,75 0,12
5 2 B3 1027,41 0,14
7 2 B3 2052,16 0,15
8 2 B3 159,63 0,05
9 2 B4 1111,49 0,16
10 2 B4 1312,15 0,12
1 3 B4 863,79 0,10
2 3 B4 104,06 0,04
3 3 B4 816,02 0,11
4 3 B4 1053,02 0,14
5 3 B5 132,32 0,03
6 3 B5 2059,03 0,14
7 3 B5 153,49 0,04
8 3 B5 1118,69 0,15
9 3 B5 1632,66 0,18
10 3 B5 1302,15 0,12", header = T)





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022787%2fhow-to-create-a-for-loop-to-filter-within-a-specific-row-and-to-save-the-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You should split your data.frame in a list, each element of the list then is printed:



    # here you split by the variable you want:
    listed <- split(data, data$TM)
    listed
    # here the partial result
    $B2
    INr FNr TM I1 I2
    1 1 1 B2 1598,45 0,14
    2 2 1 B2 930,40 0,11
    3 3 1 B2 107,86 0,04
    4 4 1 B2 881,09 0,11

    $B3
    INr FNr TM I1 I2
    5 7 1 B3 2201,98 0,15
    6 8 1 B3 161,30 0,04
    7 9 1 B3 1208,14 0,17
    8 4 2 B3 831,75 0,12
    9 5 2 B3 1027,41 0,14
    10 7 2 B3 2052,16 0,15
    11 8 2 B3 159,63 0,05
    ...


    Then R is a vectorized language, so you do not need in this case a loop, but you can use one of the apply functions, to apply to each of the list a function, in this case, write.table:



    sapply(names(listed),  # here the object to sapply the function 
    # here the function: you have to create the path with paste0
    # adding the x to have the correct names
    function (x) write.table(listed[[x]], file=paste0("C:\Users\Desktop\",x, ".csv")))




    With data:



    data <- read.table(text = "INr         FNr         TM          I1              I2
    1 1 B2 1598,45 0,14
    2 1 B2 930,40 0,11
    3 1 B2 107,86 0,04
    4 1 B2 881,09 0,11
    7 1 B3 2201,98 0,15
    8 1 B3 161,30 0,04
    9 1 B3 1208,14 0,17
    4 2 B3 831,75 0,12
    5 2 B3 1027,41 0,14
    7 2 B3 2052,16 0,15
    8 2 B3 159,63 0,05
    9 2 B4 1111,49 0,16
    10 2 B4 1312,15 0,12
    1 3 B4 863,79 0,10
    2 3 B4 104,06 0,04
    3 3 B4 816,02 0,11
    4 3 B4 1053,02 0,14
    5 3 B5 132,32 0,03
    6 3 B5 2059,03 0,14
    7 3 B5 153,49 0,04
    8 3 B5 1118,69 0,15
    9 3 B5 1632,66 0,18
    10 3 B5 1302,15 0,12", header = T)





    share|improve this answer






























      2














      You should split your data.frame in a list, each element of the list then is printed:



      # here you split by the variable you want:
      listed <- split(data, data$TM)
      listed
      # here the partial result
      $B2
      INr FNr TM I1 I2
      1 1 1 B2 1598,45 0,14
      2 2 1 B2 930,40 0,11
      3 3 1 B2 107,86 0,04
      4 4 1 B2 881,09 0,11

      $B3
      INr FNr TM I1 I2
      5 7 1 B3 2201,98 0,15
      6 8 1 B3 161,30 0,04
      7 9 1 B3 1208,14 0,17
      8 4 2 B3 831,75 0,12
      9 5 2 B3 1027,41 0,14
      10 7 2 B3 2052,16 0,15
      11 8 2 B3 159,63 0,05
      ...


      Then R is a vectorized language, so you do not need in this case a loop, but you can use one of the apply functions, to apply to each of the list a function, in this case, write.table:



      sapply(names(listed),  # here the object to sapply the function 
      # here the function: you have to create the path with paste0
      # adding the x to have the correct names
      function (x) write.table(listed[[x]], file=paste0("C:\Users\Desktop\",x, ".csv")))




      With data:



      data <- read.table(text = "INr         FNr         TM          I1              I2
      1 1 B2 1598,45 0,14
      2 1 B2 930,40 0,11
      3 1 B2 107,86 0,04
      4 1 B2 881,09 0,11
      7 1 B3 2201,98 0,15
      8 1 B3 161,30 0,04
      9 1 B3 1208,14 0,17
      4 2 B3 831,75 0,12
      5 2 B3 1027,41 0,14
      7 2 B3 2052,16 0,15
      8 2 B3 159,63 0,05
      9 2 B4 1111,49 0,16
      10 2 B4 1312,15 0,12
      1 3 B4 863,79 0,10
      2 3 B4 104,06 0,04
      3 3 B4 816,02 0,11
      4 3 B4 1053,02 0,14
      5 3 B5 132,32 0,03
      6 3 B5 2059,03 0,14
      7 3 B5 153,49 0,04
      8 3 B5 1118,69 0,15
      9 3 B5 1632,66 0,18
      10 3 B5 1302,15 0,12", header = T)





      share|improve this answer




























        2












        2








        2







        You should split your data.frame in a list, each element of the list then is printed:



        # here you split by the variable you want:
        listed <- split(data, data$TM)
        listed
        # here the partial result
        $B2
        INr FNr TM I1 I2
        1 1 1 B2 1598,45 0,14
        2 2 1 B2 930,40 0,11
        3 3 1 B2 107,86 0,04
        4 4 1 B2 881,09 0,11

        $B3
        INr FNr TM I1 I2
        5 7 1 B3 2201,98 0,15
        6 8 1 B3 161,30 0,04
        7 9 1 B3 1208,14 0,17
        8 4 2 B3 831,75 0,12
        9 5 2 B3 1027,41 0,14
        10 7 2 B3 2052,16 0,15
        11 8 2 B3 159,63 0,05
        ...


        Then R is a vectorized language, so you do not need in this case a loop, but you can use one of the apply functions, to apply to each of the list a function, in this case, write.table:



        sapply(names(listed),  # here the object to sapply the function 
        # here the function: you have to create the path with paste0
        # adding the x to have the correct names
        function (x) write.table(listed[[x]], file=paste0("C:\Users\Desktop\",x, ".csv")))




        With data:



        data <- read.table(text = "INr         FNr         TM          I1              I2
        1 1 B2 1598,45 0,14
        2 1 B2 930,40 0,11
        3 1 B2 107,86 0,04
        4 1 B2 881,09 0,11
        7 1 B3 2201,98 0,15
        8 1 B3 161,30 0,04
        9 1 B3 1208,14 0,17
        4 2 B3 831,75 0,12
        5 2 B3 1027,41 0,14
        7 2 B3 2052,16 0,15
        8 2 B3 159,63 0,05
        9 2 B4 1111,49 0,16
        10 2 B4 1312,15 0,12
        1 3 B4 863,79 0,10
        2 3 B4 104,06 0,04
        3 3 B4 816,02 0,11
        4 3 B4 1053,02 0,14
        5 3 B5 132,32 0,03
        6 3 B5 2059,03 0,14
        7 3 B5 153,49 0,04
        8 3 B5 1118,69 0,15
        9 3 B5 1632,66 0,18
        10 3 B5 1302,15 0,12", header = T)





        share|improve this answer















        You should split your data.frame in a list, each element of the list then is printed:



        # here you split by the variable you want:
        listed <- split(data, data$TM)
        listed
        # here the partial result
        $B2
        INr FNr TM I1 I2
        1 1 1 B2 1598,45 0,14
        2 2 1 B2 930,40 0,11
        3 3 1 B2 107,86 0,04
        4 4 1 B2 881,09 0,11

        $B3
        INr FNr TM I1 I2
        5 7 1 B3 2201,98 0,15
        6 8 1 B3 161,30 0,04
        7 9 1 B3 1208,14 0,17
        8 4 2 B3 831,75 0,12
        9 5 2 B3 1027,41 0,14
        10 7 2 B3 2052,16 0,15
        11 8 2 B3 159,63 0,05
        ...


        Then R is a vectorized language, so you do not need in this case a loop, but you can use one of the apply functions, to apply to each of the list a function, in this case, write.table:



        sapply(names(listed),  # here the object to sapply the function 
        # here the function: you have to create the path with paste0
        # adding the x to have the correct names
        function (x) write.table(listed[[x]], file=paste0("C:\Users\Desktop\",x, ".csv")))




        With data:



        data <- read.table(text = "INr         FNr         TM          I1              I2
        1 1 B2 1598,45 0,14
        2 1 B2 930,40 0,11
        3 1 B2 107,86 0,04
        4 1 B2 881,09 0,11
        7 1 B3 2201,98 0,15
        8 1 B3 161,30 0,04
        9 1 B3 1208,14 0,17
        4 2 B3 831,75 0,12
        5 2 B3 1027,41 0,14
        7 2 B3 2052,16 0,15
        8 2 B3 159,63 0,05
        9 2 B4 1111,49 0,16
        10 2 B4 1312,15 0,12
        1 3 B4 863,79 0,10
        2 3 B4 104,06 0,04
        3 3 B4 816,02 0,11
        4 3 B4 1053,02 0,14
        5 3 B5 132,32 0,03
        6 3 B5 2059,03 0,14
        7 3 B5 153,49 0,04
        8 3 B5 1118,69 0,15
        9 3 B5 1632,66 0,18
        10 3 B5 1302,15 0,12", header = T)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 13:30

























        answered Jan 3 at 13:24









        s_ts_t

        3,69121133




        3,69121133
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022787%2fhow-to-create-a-for-loop-to-filter-within-a-specific-row-and-to-save-the-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$