How to combine for loops with 2+ variables?How do I iterate over a range of numbers defined by variables in Bash?A 'for' loop to iterate over an enum in JavaHow to sort a dataframe by multiple column(s)Loop through an array in JavaScriptHow to make a great R reproducible exampleR appending data to rows in a loopdata.table vs dplyr: can one do something well the other can't or does poorly?R - combine data frames of different lengths after loopAdd data to variable columns of variable data frames in R for loopR-How create a nested list using a for loop

Does a reference have a storage location?

how to set the columns in pandas

Old story where computer expert digitally animates The Lord of the Rings

No point shuffling, just pick your cards

Show that there are infinitely more problems than we will ever be able to compute

Pandas merge and fillna

What is the difference between a historical drama and a period drama?

Is it possible to spoof an IP address to an exact number?

PhD: When to quit and move on?

Versicle and response symbols

How can solar sailed ships be protected from space debris?

Was Wolfgang Unzicker the last Amateur GM?

How frequently do Russian people still refer to others by their patronymic (отчество)?

SQL Server error 242 with ANSI datetime

Is it possible that Curiosity measured its own methane or failed doing the spectrometry?

Should I cheat if the majority does it?

Why did moving the mouse cursor cause Windows 95 to run more quickly?

In National Velvet why didn't they use a stunt double for Elizabeth Taylor?

Sleepy tired vs physically tired

What is meaning of 4 letter acronyms in Roman names like Titus Flavius T. f. T. n. Sabinus?

Can I have a forest in the rain shadow of a mountain range?

Could you sell yourself into slavery in the USA?

Did Stalin kill all Soviet officers involved in the Winter War?

Are there advantages in writing by hand over typing out a story?



How to combine for loops with 2+ variables?


How do I iterate over a range of numbers defined by variables in Bash?A 'for' loop to iterate over an enum in JavaHow to sort a dataframe by multiple column(s)Loop through an array in JavaScriptHow to make a great R reproducible exampleR appending data to rows in a loopdata.table vs dplyr: can one do something well the other can't or does poorly?R - combine data frames of different lengths after loopAdd data to variable columns of variable data frames in R for loopR-How create a nested list using a for loop






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have multiple for loops that pull dates from one data frame and match them to specific rows/columns in another data frame. I'm looking for help turning this into a single function or loop.



I've tried nested for loops without any luck.



The loops I have look like this and do what I need them to do:



for(x in id_vector) 
datafileslist[[x]]$m3start <- rep(dates_df[[x,3]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m3end <- rep(dates_df[[x,4]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m6start <- rep(dates_df[[x,5]], nrow(datafileslist[[x]]))



...and so on. While this works, there is a lot of repetition (I have 16 for loops).



I've tried this instead:



for(x in seq_along(id_vector)) 
for(z in 3:18)
for(y in 20:35)
datafileslist[[x]][[y]] <- rep(dates_df[[x,z]], nrow(datafileslist[[x]]))





But I need matched pairs of z and y to run once in tandem (3, 20; 4, 21; ... 18, 35) through each id, which the above code does not do. Any ideas on how I can do this?



The dates_df looks like this:



id m3start m3end
s01 2016-09-19 2016-12-17
s02 2016-11-03 2017-01-31


There are 60 ids and 16 combos of start/end dates (m3, m6, m9, etc.).



The data files list is a list of data frames, one data frame per id, with 50,000+ observations of around 20 variables for each id. I'm trying to add columns with start and end dates for each date range from the dates_df data frame to the data frame for each id.



The output should look like something like this:



id group m3start m3end m6start
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08


When I run the nested for loops, I get the same dates across m3start, m3end, m6start, m6end, etc.



id group m3start m3end m6start
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12









share|improve this question



















  • 3





    provide some sample input data and expected output. Its unclear what exactly you want to achieve.

    – Rushabh
    Mar 25 at 18:26












  • Have a look at mapply to run through two lists in parallel.

    – Andrew Gustar
    Mar 25 at 18:54

















0















I have multiple for loops that pull dates from one data frame and match them to specific rows/columns in another data frame. I'm looking for help turning this into a single function or loop.



I've tried nested for loops without any luck.



The loops I have look like this and do what I need them to do:



for(x in id_vector) 
datafileslist[[x]]$m3start <- rep(dates_df[[x,3]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m3end <- rep(dates_df[[x,4]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m6start <- rep(dates_df[[x,5]], nrow(datafileslist[[x]]))



...and so on. While this works, there is a lot of repetition (I have 16 for loops).



I've tried this instead:



for(x in seq_along(id_vector)) 
for(z in 3:18)
for(y in 20:35)
datafileslist[[x]][[y]] <- rep(dates_df[[x,z]], nrow(datafileslist[[x]]))





But I need matched pairs of z and y to run once in tandem (3, 20; 4, 21; ... 18, 35) through each id, which the above code does not do. Any ideas on how I can do this?



The dates_df looks like this:



id m3start m3end
s01 2016-09-19 2016-12-17
s02 2016-11-03 2017-01-31


There are 60 ids and 16 combos of start/end dates (m3, m6, m9, etc.).



The data files list is a list of data frames, one data frame per id, with 50,000+ observations of around 20 variables for each id. I'm trying to add columns with start and end dates for each date range from the dates_df data frame to the data frame for each id.



The output should look like something like this:



id group m3start m3end m6start
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08


When I run the nested for loops, I get the same dates across m3start, m3end, m6start, m6end, etc.



id group m3start m3end m6start
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12









share|improve this question



















  • 3





    provide some sample input data and expected output. Its unclear what exactly you want to achieve.

    – Rushabh
    Mar 25 at 18:26












  • Have a look at mapply to run through two lists in parallel.

    – Andrew Gustar
    Mar 25 at 18:54













0












0








0








I have multiple for loops that pull dates from one data frame and match them to specific rows/columns in another data frame. I'm looking for help turning this into a single function or loop.



I've tried nested for loops without any luck.



The loops I have look like this and do what I need them to do:



for(x in id_vector) 
datafileslist[[x]]$m3start <- rep(dates_df[[x,3]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m3end <- rep(dates_df[[x,4]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m6start <- rep(dates_df[[x,5]], nrow(datafileslist[[x]]))



...and so on. While this works, there is a lot of repetition (I have 16 for loops).



I've tried this instead:



for(x in seq_along(id_vector)) 
for(z in 3:18)
for(y in 20:35)
datafileslist[[x]][[y]] <- rep(dates_df[[x,z]], nrow(datafileslist[[x]]))





But I need matched pairs of z and y to run once in tandem (3, 20; 4, 21; ... 18, 35) through each id, which the above code does not do. Any ideas on how I can do this?



The dates_df looks like this:



id m3start m3end
s01 2016-09-19 2016-12-17
s02 2016-11-03 2017-01-31


There are 60 ids and 16 combos of start/end dates (m3, m6, m9, etc.).



The data files list is a list of data frames, one data frame per id, with 50,000+ observations of around 20 variables for each id. I'm trying to add columns with start and end dates for each date range from the dates_df data frame to the data frame for each id.



The output should look like something like this:



id group m3start m3end m6start
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08


When I run the nested for loops, I get the same dates across m3start, m3end, m6start, m6end, etc.



id group m3start m3end m6start
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12









share|improve this question
















I have multiple for loops that pull dates from one data frame and match them to specific rows/columns in another data frame. I'm looking for help turning this into a single function or loop.



I've tried nested for loops without any luck.



The loops I have look like this and do what I need them to do:



for(x in id_vector) 
datafileslist[[x]]$m3start <- rep(dates_df[[x,3]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m3end <- rep(dates_df[[x,4]], nrow(datafileslist[[x]]))


for(x in id_vector)
datafileslist[[x]]$m6start <- rep(dates_df[[x,5]], nrow(datafileslist[[x]]))



...and so on. While this works, there is a lot of repetition (I have 16 for loops).



I've tried this instead:



for(x in seq_along(id_vector)) 
for(z in 3:18)
for(y in 20:35)
datafileslist[[x]][[y]] <- rep(dates_df[[x,z]], nrow(datafileslist[[x]]))





But I need matched pairs of z and y to run once in tandem (3, 20; 4, 21; ... 18, 35) through each id, which the above code does not do. Any ideas on how I can do this?



The dates_df looks like this:



id m3start m3end
s01 2016-09-19 2016-12-17
s02 2016-11-03 2017-01-31


There are 60 ids and 16 combos of start/end dates (m3, m6, m9, etc.).



The data files list is a list of data frames, one data frame per id, with 50,000+ observations of around 20 variables for each id. I'm trying to add columns with start and end dates for each date range from the dates_df data frame to the data frame for each id.



The output should look like something like this:



id group m3start m3end m6start
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08
s01 int 2016-09-19 2016-12-17 2018-09-08


When I run the nested for loops, I get the same dates across m3start, m3end, m6start, m6end, etc.



id group m3start m3end m6start
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12
s01 int 2020-01-12 2020-01-12 2020-01-12






r for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 18:53







Sarah

















asked Mar 25 at 18:21









SarahSarah

62 bronze badges




62 bronze badges







  • 3





    provide some sample input data and expected output. Its unclear what exactly you want to achieve.

    – Rushabh
    Mar 25 at 18:26












  • Have a look at mapply to run through two lists in parallel.

    – Andrew Gustar
    Mar 25 at 18:54












  • 3





    provide some sample input data and expected output. Its unclear what exactly you want to achieve.

    – Rushabh
    Mar 25 at 18:26












  • Have a look at mapply to run through two lists in parallel.

    – Andrew Gustar
    Mar 25 at 18:54







3




3





provide some sample input data and expected output. Its unclear what exactly you want to achieve.

– Rushabh
Mar 25 at 18:26






provide some sample input data and expected output. Its unclear what exactly you want to achieve.

– Rushabh
Mar 25 at 18:26














Have a look at mapply to run through two lists in parallel.

– Andrew Gustar
Mar 25 at 18:54





Have a look at mapply to run through two lists in parallel.

– Andrew Gustar
Mar 25 at 18:54












1 Answer
1






active

oldest

votes


















1














Consider merge inside an lapply on list of data frames:



new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))





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%2f55344248%2fhow-to-combine-for-loops-with-2-variables%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









    1














    Consider merge inside an lapply on list of data frames:



    new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))





    share|improve this answer



























      1














      Consider merge inside an lapply on list of data frames:



      new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))





      share|improve this answer

























        1












        1








        1







        Consider merge inside an lapply on list of data frames:



        new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))





        share|improve this answer













        Consider merge inside an lapply on list of data frames:



        new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 19:23









        ParfaitParfait

        58k10 gold badges53 silver badges73 bronze badges




        58k10 gold badges53 silver badges73 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55344248%2fhow-to-combine-for-loops-with-2-variables%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript