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;
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
add a comment |
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
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 atmapply
to run through two lists in parallel.
– Andrew Gustar
Mar 25 at 18:54
add a comment |
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
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
r for-loop
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 atmapply
to run through two lists in parallel.
– Andrew Gustar
Mar 25 at 18:54
add a comment |
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 atmapply
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
add a comment |
1 Answer
1
active
oldest
votes
Consider merge
inside an lapply
on list of data frames:
new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Consider merge
inside an lapply
on list of data frames:
new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))
add a comment |
Consider merge
inside an lapply
on list of data frames:
new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))
add a comment |
Consider merge
inside an lapply
on list of data frames:
new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))
Consider merge
inside an lapply
on list of data frames:
new_datafileslist <- lapply(datafileslist, function(df) merge(df, dates_df, by="id"))
answered Mar 25 at 19:23
ParfaitParfait
58k10 gold badges53 silver badges73 bronze badges
58k10 gold badges53 silver badges73 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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