Mutate multiple columns with condition in a map functionHow to sort a dataframe by multiple column(s)Grouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameHow can I view the source code for a function?dplyr mutate with conditional valuesCan dplyr package be used for conditional mutating?Check if values of datetime column in df2 is within datateime values of df1 in Rifelse statement within a data.table query in RMutate multiple columns in R based on conditionsR Mutate multiple columns with ifelse()-condition
Was there a Viking Exchange as well as a Columbian one?
Pre-plastic human skin alternative
Initiative: Do I lose my attack/action if my target moves or dies before my turn in combat?
Why do games have consumables?
555 timer FM transmitter
Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?
How could Tony Stark make this in Endgame?
Critique of timeline aesthetic
How to write a column outside the braces in a matrix?
Function pointer with named arguments?
How exactly does Hawking radiation decrease the mass of black holes?
How to limit Drive Letters Windows assigns to new removable USB drives
Aligning equation numbers vertically
Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?
How to display Aura JS Errors Lightning Out
Could the terminal length of components like resistors be reduced?
Minor Revision with suggestion of an alternative proof by reviewer
Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?
How does Captain America channel this power?
How to prevent z-fighting in OpenSCAD?
Classification of surfaces
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
What is causing the white spot to appear in some of my pictures
Can I grease a crank spindle/bracket without disassembling the crank set?
Mutate multiple columns with condition in a map function
How to sort a dataframe by multiple column(s)Grouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameHow can I view the source code for a function?dplyr mutate with conditional valuesCan dplyr package be used for conditional mutating?Check if values of datetime column in df2 is within datateime values of df1 in Rifelse statement within a data.table query in RMutate multiple columns in R based on conditionsR Mutate multiple columns with ifelse()-condition
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to create several columns with a ifelse()-condition for multiple dataframes. Here is my example code:
df1 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df2 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df3 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
And now I want to add new columns with ascending conditions (from 1 to 5). The first column should only contain values from the "return"-column, which is higher than 1, the second column should only contain values, which is higher than 1.5, the third column should only contain values, which is higher than 2, and so on...
For example, I can do it with a for-loop. But that works only for one dataframe:
for(i in seq(1, 5, 0.5))
varname =paste0("return>",i)
df1[[varname]] <- with(df1, ifelse(return > i, return, NA))
> head(df1)
# A tibble: 6 x 12
date return `return > 0.5 s~ `return > 1 sd` `return > 1.5 s~ `return > 2 sd` `return > 2.5 s~
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-03-09 4.94 4.94 4.94 4.94 4.94 4.94
2 2019-03-10 0.936 0.936 NA NA NA NA
3 2019-03-11 0.770 0.770 NA NA NA NA
4 2019-03-12 1.03 1.03 1.03 NA NA NA
5 2019-03-13 3.34 3.34 3.34 3.34 3.34 3.34
6 2019-03-14 0.983 0.983 NA NA NA NA
# ... with 5 more variables: `return > 3 sd` <dbl>, `return > 3.5 sd` <dbl>, `return > 4 sd` <dbl>,
# `return > 4.5 sd` <dbl>, `return > 5 sd` <dbl>
Here is another code to get the desired output for one dataframe:
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
My question is, how can I put this code in a map-function (or in another function) to run this code for all 3 dataframes?
r
add a comment |
I want to create several columns with a ifelse()-condition for multiple dataframes. Here is my example code:
df1 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df2 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df3 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
And now I want to add new columns with ascending conditions (from 1 to 5). The first column should only contain values from the "return"-column, which is higher than 1, the second column should only contain values, which is higher than 1.5, the third column should only contain values, which is higher than 2, and so on...
For example, I can do it with a for-loop. But that works only for one dataframe:
for(i in seq(1, 5, 0.5))
varname =paste0("return>",i)
df1[[varname]] <- with(df1, ifelse(return > i, return, NA))
> head(df1)
# A tibble: 6 x 12
date return `return > 0.5 s~ `return > 1 sd` `return > 1.5 s~ `return > 2 sd` `return > 2.5 s~
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-03-09 4.94 4.94 4.94 4.94 4.94 4.94
2 2019-03-10 0.936 0.936 NA NA NA NA
3 2019-03-11 0.770 0.770 NA NA NA NA
4 2019-03-12 1.03 1.03 1.03 NA NA NA
5 2019-03-13 3.34 3.34 3.34 3.34 3.34 3.34
6 2019-03-14 0.983 0.983 NA NA NA NA
# ... with 5 more variables: `return > 3 sd` <dbl>, `return > 3.5 sd` <dbl>, `return > 4 sd` <dbl>,
# `return > 4.5 sd` <dbl>, `return > 5 sd` <dbl>
Here is another code to get the desired output for one dataframe:
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
My question is, how can I put this code in a map-function (or in another function) to run this code for all 3 dataframes?
r
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18
add a comment |
I want to create several columns with a ifelse()-condition for multiple dataframes. Here is my example code:
df1 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df2 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df3 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
And now I want to add new columns with ascending conditions (from 1 to 5). The first column should only contain values from the "return"-column, which is higher than 1, the second column should only contain values, which is higher than 1.5, the third column should only contain values, which is higher than 2, and so on...
For example, I can do it with a for-loop. But that works only for one dataframe:
for(i in seq(1, 5, 0.5))
varname =paste0("return>",i)
df1[[varname]] <- with(df1, ifelse(return > i, return, NA))
> head(df1)
# A tibble: 6 x 12
date return `return > 0.5 s~ `return > 1 sd` `return > 1.5 s~ `return > 2 sd` `return > 2.5 s~
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-03-09 4.94 4.94 4.94 4.94 4.94 4.94
2 2019-03-10 0.936 0.936 NA NA NA NA
3 2019-03-11 0.770 0.770 NA NA NA NA
4 2019-03-12 1.03 1.03 1.03 NA NA NA
5 2019-03-13 3.34 3.34 3.34 3.34 3.34 3.34
6 2019-03-14 0.983 0.983 NA NA NA NA
# ... with 5 more variables: `return > 3 sd` <dbl>, `return > 3.5 sd` <dbl>, `return > 4 sd` <dbl>,
# `return > 4.5 sd` <dbl>, `return > 5 sd` <dbl>
Here is another code to get the desired output for one dataframe:
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
My question is, how can I put this code in a map-function (or in another function) to run this code for all 3 dataframes?
r
I want to create several columns with a ifelse()-condition for multiple dataframes. Here is my example code:
df1 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df2 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
df3 <- tibble(
date = lubridate::today() +0:9,
return= runif(n = 10, min = 0, max = 5))
And now I want to add new columns with ascending conditions (from 1 to 5). The first column should only contain values from the "return"-column, which is higher than 1, the second column should only contain values, which is higher than 1.5, the third column should only contain values, which is higher than 2, and so on...
For example, I can do it with a for-loop. But that works only for one dataframe:
for(i in seq(1, 5, 0.5))
varname =paste0("return>",i)
df1[[varname]] <- with(df1, ifelse(return > i, return, NA))
> head(df1)
# A tibble: 6 x 12
date return `return > 0.5 s~ `return > 1 sd` `return > 1.5 s~ `return > 2 sd` `return > 2.5 s~
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-03-09 4.94 4.94 4.94 4.94 4.94 4.94
2 2019-03-10 0.936 0.936 NA NA NA NA
3 2019-03-11 0.770 0.770 NA NA NA NA
4 2019-03-12 1.03 1.03 1.03 NA NA NA
5 2019-03-13 3.34 3.34 3.34 3.34 3.34 3.34
6 2019-03-14 0.983 0.983 NA NA NA NA
# ... with 5 more variables: `return > 3 sd` <dbl>, `return > 3.5 sd` <dbl>, `return > 4 sd` <dbl>,
# `return > 4.5 sd` <dbl>, `return > 5 sd` <dbl>
Here is another code to get the desired output for one dataframe:
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
My question is, how can I put this code in a map-function (or in another function) to run this code for all 3 dataframes?
r
r
edited Mar 9 at 11:04
TheRealBilaal
5421517
5421517
asked Mar 9 at 9:09
TobKelTobKel
17710
17710
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18
add a comment |
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18
add a comment |
1 Answer
1
active
oldest
votes
Put all the dataframes in a list and create a function to return new columns
df_list <- list(df1, df2, df3)
return_new_cols <- function(df1)
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
df1
Now apply return_new_cols
function to each dataframe in the list
lapply(df_list, return_new_cols)
You can achieve the same with purrr::map
instead of lapply
purrr::map(df_list, return_new_cols)
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%2f55075730%2fmutate-multiple-columns-with-condition-in-a-map-function%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
Put all the dataframes in a list and create a function to return new columns
df_list <- list(df1, df2, df3)
return_new_cols <- function(df1)
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
df1
Now apply return_new_cols
function to each dataframe in the list
lapply(df_list, return_new_cols)
You can achieve the same with purrr::map
instead of lapply
purrr::map(df_list, return_new_cols)
add a comment |
Put all the dataframes in a list and create a function to return new columns
df_list <- list(df1, df2, df3)
return_new_cols <- function(df1)
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
df1
Now apply return_new_cols
function to each dataframe in the list
lapply(df_list, return_new_cols)
You can achieve the same with purrr::map
instead of lapply
purrr::map(df_list, return_new_cols)
add a comment |
Put all the dataframes in a list and create a function to return new columns
df_list <- list(df1, df2, df3)
return_new_cols <- function(df1)
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
df1
Now apply return_new_cols
function to each dataframe in the list
lapply(df_list, return_new_cols)
You can achieve the same with purrr::map
instead of lapply
purrr::map(df_list, return_new_cols)
Put all the dataframes in a list and create a function to return new columns
df_list <- list(df1, df2, df3)
return_new_cols <- function(df1)
n <- seq(0.5, 5, 0.5)
df1[paste0("return > ", n, " sd")] <- lapply(n, function(x)
replace(df1$return, df1$return <= x, NA))
df1
Now apply return_new_cols
function to each dataframe in the list
lapply(df_list, return_new_cols)
You can achieve the same with purrr::map
instead of lapply
purrr::map(df_list, return_new_cols)
answered Mar 9 at 9:31
Ronak ShahRonak Shah
50k104370
50k104370
add a comment |
add a comment |
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%2f55075730%2fmutate-multiple-columns-with-condition-in-a-map-function%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
you could loop through each dataframe and return a list of dataframes.
– Sada93
Mar 9 at 9:18