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










0















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









share|improve this question






















  • 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 then diff(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 use if/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















0















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









share|improve this question






















  • 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 then diff(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 use if/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













0












0








0








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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





    ifelse requres all arguments to have the same length. You can use if/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












  • 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







  • 3





    ifelse requres all arguments to have the same length. You can use if/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












1 Answer
1






active

oldest

votes


















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)






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%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









    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)






    share|improve this answer



























      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)






      share|improve this answer

























        0












        0








        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)






        share|improve this answer













        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)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        user2987808user2987808

        594418




        594418





























            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%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





















































            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

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme