Rounding vector in pipe statement produces only returns 1st element in vector2019 Community Moderator ElectionTest if a vector contains a given elementWhy can't R's ifelse statements return vectors?Counting the number of elements with the values of x in a vectorError with custom aggregate function for a cast() call in R reshape2Extract every nth element of a vectorIs there an R function for finding the index of an element in a vector?How to handle pipes producing empty data further down the dplyr pipelinedplyr summarise when function return is vector-valued?combine vectors by elementsPiping a scalar to round function in r
Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?
Was it really inappropriate to write a pull request for the company I interviewed with?
Interpretation of linear regression interaction term plot
Can the Witch Sight warlock invocation see through the Mirror Image spell?
Help! My Character is too much for her story!
Precision notation for voltmeters
How to make sure I'm assertive enough in contact with subordinates?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Should I file my taxes? No income, unemployed, but paid 2k in student loan interest
Short SF story. Females use stingers to implant eggs in yearfathers
What is the purpose of a disclaimer like "this is not legal advice"?
Limpar string com Regex
What is better: yes / no radio, or simple checkbox?
How spaceships determine each other's mass in space?
Paper published similar to PhD thesis
Short story about cities being connected by a conveyor belt
Sort array by month and year
Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?
Generating a list with duplicate entries
Averaging over columns while ignoring zero entries
Are small insurances worth it?
What exactly is the meaning of "fine wine"?
Is the differential, dp, exact or not?
What is Tony Stark injecting into himself in Iron Man 3?
Rounding vector in pipe statement produces only returns 1st element in vector
2019 Community Moderator ElectionTest if a vector contains a given elementWhy can't R's ifelse statements return vectors?Counting the number of elements with the values of x in a vectorError with custom aggregate function for a cast() call in R reshape2Extract every nth element of a vectorIs there an R function for finding the index of an element in a vector?How to handle pipes producing empty data further down the dplyr pipelinedplyr summarise when function return is vector-valued?combine vectors by elementsPiping a scalar to round function in r
I have a tibble that I have aggregated into means by group and wish to extract the max mean and min mean into a 2-element vector.
I also need to determine if the difference between the max and min is <= 1, then I need to round the vector to 3 digits, if not, then I can round to whole numbers.
For some reason when I do this in a pipe statement it is only returning the 1st element rounded. What is causing this behavior?
library(tidyverse)
set.seed(123)
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(diff(.) <= 1, round(., digits = 2), round(., digits = 0))
#> [1] 728
# Expected Behavior
c(728.0433, 1131.1561) %>%
round(digits = 0)
#> [1] 728 1131
r dplyr
|
show 1 more comment
I have a tibble that I have aggregated into means by group and wish to extract the max mean and min mean into a 2-element vector.
I also need to determine if the difference between the max and min is <= 1, then I need to round the vector to 3 digits, if not, then I can round to whole numbers.
For some reason when I do this in a pipe statement it is only returning the 1st element rounded. What is causing this behavior?
library(tidyverse)
set.seed(123)
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(diff(.) <= 1, round(., digits = 2), round(., digits = 0))
#> [1] 728
# Expected Behavior
c(728.0433, 1131.1561) %>%
round(digits = 0)
#> [1] 728 1131
r dplyr
diff(.)
returns a length of 1 less than the original vector.
– akrun
2 days ago
I understand that, but.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?
– dylanjm
2 days ago
Before the last step, if you create an objectout
and thendiff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2
– akrun
2 days ago
3
ifelse
requres all arguments to have the same length. You can useif/else
– akrun
2 days ago
4
Try%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)
– akrun
2 days ago
|
show 1 more comment
I have a tibble that I have aggregated into means by group and wish to extract the max mean and min mean into a 2-element vector.
I also need to determine if the difference between the max and min is <= 1, then I need to round the vector to 3 digits, if not, then I can round to whole numbers.
For some reason when I do this in a pipe statement it is only returning the 1st element rounded. What is causing this behavior?
library(tidyverse)
set.seed(123)
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(diff(.) <= 1, round(., digits = 2), round(., digits = 0))
#> [1] 728
# Expected Behavior
c(728.0433, 1131.1561) %>%
round(digits = 0)
#> [1] 728 1131
r dplyr
I have a tibble that I have aggregated into means by group and wish to extract the max mean and min mean into a 2-element vector.
I also need to determine if the difference between the max and min is <= 1, then I need to round the vector to 3 digits, if not, then I can round to whole numbers.
For some reason when I do this in a pipe statement it is only returning the 1st element rounded. What is causing this behavior?
library(tidyverse)
set.seed(123)
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(diff(.) <= 1, round(., digits = 2), round(., digits = 0))
#> [1] 728
# Expected Behavior
c(728.0433, 1131.1561) %>%
round(digits = 0)
#> [1] 728 1131
r dplyr
r dplyr
asked 2 days ago
dylanjmdylanjm
819213
819213
diff(.)
returns a length of 1 less than the original vector.
– akrun
2 days ago
I understand that, but.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?
– dylanjm
2 days ago
Before the last step, if you create an objectout
and thendiff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2
– akrun
2 days ago
3
ifelse
requres all arguments to have the same length. You can useif/else
– akrun
2 days ago
4
Try%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)
– akrun
2 days ago
|
show 1 more comment
diff(.)
returns a length of 1 less than the original vector.
– akrun
2 days ago
I understand that, but.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?
– dylanjm
2 days ago
Before the last step, if you create an objectout
and thendiff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2
– akrun
2 days ago
3
ifelse
requres all arguments to have the same length. You can useif/else
– akrun
2 days ago
4
Try%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)
– akrun
2 days ago
diff(.)
returns a length of 1 less than the original vector.– akrun
2 days ago
diff(.)
returns a length of 1 less than the original vector.– akrun
2 days ago
I understand that, but
.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?– dylanjm
2 days ago
I understand that, but
.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?– dylanjm
2 days ago
Before the last step, if you create an object
out
and then diff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2– akrun
2 days ago
Before the last step, if you create an object
out
and then diff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2– akrun
2 days ago
3
3
ifelse
requres all arguments to have the same length. You can use if/else
– akrun
2 days ago
ifelse
requres all arguments to have the same length. You can use if/else
– akrun
2 days ago
4
4
Try
%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)– akrun
2 days ago
Try
%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)– akrun
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
You could do either:
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(rep(diff(.) <= 1, 2), round(., digits = 2), round(., digits = 0))
or
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
if(diff(.) <= 1) round(., digits = 2) else round(., digits = 0)
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%2f55027373%2frounding-vector-in-pipe-statement-produces-only-returns-1st-element-in-vector%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
You could do either:
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(rep(diff(.) <= 1, 2), round(., digits = 2), round(., digits = 0))
or
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
if(diff(.) <= 1) round(., digits = 2) else round(., digits = 0)
add a comment |
You could do either:
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(rep(diff(.) <= 1, 2), round(., digits = 2), round(., digits = 0))
or
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
if(diff(.) <= 1) round(., digits = 2) else round(., digits = 0)
add a comment |
You could do either:
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(rep(diff(.) <= 1, 2), round(., digits = 2), round(., digits = 0))
or
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
if(diff(.) <= 1) round(., digits = 2) else round(., digits = 0)
You could do either:
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
ifelse(rep(diff(.) <= 1, 2), round(., digits = 2), round(., digits = 0))
or
tibble(x = runif(30, min = 205, max = 1440),
y = rep(paste0("Group",1:5), each = 6)) %>%
group_by(y) %>%
summarize(mean = mean(x)) %>%
c(min(.$mean), max(.$mean)) %>%
if(diff(.) <= 1) round(., digits = 2) else round(., digits = 0)
answered yesterday
user2987808user2987808
594418
594418
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%2f55027373%2frounding-vector-in-pipe-statement-produces-only-returns-1st-element-in-vector%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
diff(.)
returns a length of 1 less than the original vector.– akrun
2 days ago
I understand that, but
.
should represent what is being piped in and not what is being forwarded from the conditional clause. Correct?– dylanjm
2 days ago
Before the last step, if you create an object
out
and thendiff(out) [1] 227.9097
, So, it is just length 1 instead of what you expect 2– akrun
2 days ago
3
ifelse
requres all arguments to have the same length. You can useif/else
– akrun
2 days ago
4
Try
%>% if(diff(.) <=1) round(., digits = 2) else round(., digits = 0)
(assuming length of initial vector is 2)– akrun
2 days ago