Function that calculates, mean, variance and skewness at the same time in a dataframeApplying functions to dataframe or multiple listsCalculating mean of a subset within a dataframe in rR cut with open interval to right puts some values in the wrong intervalHow to calculate daily means, medians, from weather variables data collected hourly in R?Calculate summary statistics of different columns from separate data.framesMean(or other function) of corresponding elements of a list in Rr - Length and sum of runs of negative valuesFunction to perform similar calculations on variables with similar namesR: Change order of several dataframes in a list depending on mean of a specific columnLooping so that it extracts data and then calculates mean

In Romance of the Three Kingdoms why do people still use bamboo sticks when papers are already invented?

Is it possible to create light that imparts a greater proportion of its energy as momentum rather than heat?

Where does SFDX store details about scratch orgs?

Neighboring nodes in the network

prove that the matrix A is diagonalizable

SSH "lag" in LAN on some machines, mixed distros

What to put in ESTA if staying in US for a few days before going on to Canada

Do I have a twin with permutated remainders?

How do I write bicross product symbols in latex?

What does it mean to describe someone as a butt steak?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Doing something right before you need it - expression for this?

How much of data wrangling is a data scientist's job?

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

Why are electrically insulating heatsinks so rare? Is it just cost?

Alternative to sending password over mail?

How do conventional missiles fly?

AES: Why is it a good practice to use only the first 16bytes of a hash for encryption?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Is the Joker left-handed?

Is there a hemisphere-neutral way of specifying a season?

Can a rocket refuel on Mars from water?

How could indestructible materials be used in power generation?

Can one be a co-translator of a book, if he does not know the language that the book is translated into?



Function that calculates, mean, variance and skewness at the same time in a dataframe


Applying functions to dataframe or multiple listsCalculating mean of a subset within a dataframe in rR cut with open interval to right puts some values in the wrong intervalHow to calculate daily means, medians, from weather variables data collected hourly in R?Calculate summary statistics of different columns from separate data.framesMean(or other function) of corresponding elements of a list in Rr - Length and sum of runs of negative valuesFunction to perform similar calculations on variables with similar namesR: Change order of several dataframes in a list depending on mean of a specific columnLooping so that it extracts data and then calculates mean






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have this dataframe



um dois tres
1.2 1.9 0.1
1.2 1.1 2
2.2 1.2 0.4
3.0 1.8 2.6


and I need to calculate in the same function the mean, variance and skewness of the dataframe. I have the functions separate.
Any suggestions?



mean <- function(x)
sum(x)/length(x)


variance <- function(x)
n <- length(x)
m <- mean(x)
(1/(n-1))*sum((x-m)^2)


skewness <- function(x)
n <- length(x)
v <- var(x)
m <- mean(x)
third.moment <- (1/(n - 2))*sum((x - m)^3)
third.moment/(var(x)^(3/2))










share|improve this question
























  • What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

    – neilfws
    Mar 8 at 0:15











  • See the moments package for the skewness function.

    – Dave2e
    Mar 8 at 0:35

















2















I have this dataframe



um dois tres
1.2 1.9 0.1
1.2 1.1 2
2.2 1.2 0.4
3.0 1.8 2.6


and I need to calculate in the same function the mean, variance and skewness of the dataframe. I have the functions separate.
Any suggestions?



mean <- function(x)
sum(x)/length(x)


variance <- function(x)
n <- length(x)
m <- mean(x)
(1/(n-1))*sum((x-m)^2)


skewness <- function(x)
n <- length(x)
v <- var(x)
m <- mean(x)
third.moment <- (1/(n - 2))*sum((x - m)^3)
third.moment/(var(x)^(3/2))










share|improve this question
























  • What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

    – neilfws
    Mar 8 at 0:15











  • See the moments package for the skewness function.

    – Dave2e
    Mar 8 at 0:35













2












2








2








I have this dataframe



um dois tres
1.2 1.9 0.1
1.2 1.1 2
2.2 1.2 0.4
3.0 1.8 2.6


and I need to calculate in the same function the mean, variance and skewness of the dataframe. I have the functions separate.
Any suggestions?



mean <- function(x)
sum(x)/length(x)


variance <- function(x)
n <- length(x)
m <- mean(x)
(1/(n-1))*sum((x-m)^2)


skewness <- function(x)
n <- length(x)
v <- var(x)
m <- mean(x)
third.moment <- (1/(n - 2))*sum((x - m)^3)
third.moment/(var(x)^(3/2))










share|improve this question
















I have this dataframe



um dois tres
1.2 1.9 0.1
1.2 1.1 2
2.2 1.2 0.4
3.0 1.8 2.6


and I need to calculate in the same function the mean, variance and skewness of the dataframe. I have the functions separate.
Any suggestions?



mean <- function(x)
sum(x)/length(x)


variance <- function(x)
n <- length(x)
m <- mean(x)
(1/(n-1))*sum((x-m)^2)


skewness <- function(x)
n <- length(x)
v <- var(x)
m <- mean(x)
third.moment <- (1/(n - 2))*sum((x - m)^3)
third.moment/(var(x)^(3/2))







r function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 0:32









d.b

20.5k41949




20.5k41949










asked Mar 8 at 0:10









Frank TerraFrank Terra

1111




1111












  • What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

    – neilfws
    Mar 8 at 0:15











  • See the moments package for the skewness function.

    – Dave2e
    Mar 8 at 0:35

















  • What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

    – neilfws
    Mar 8 at 0:15











  • See the moments package for the skewness function.

    – Dave2e
    Mar 8 at 0:35
















What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

– neilfws
Mar 8 at 0:15





What does "of the dataframe" mean? Calculate values for each column, or for all numbers in all columns combined?

– neilfws
Mar 8 at 0:15













See the moments package for the skewness function.

– Dave2e
Mar 8 at 0:35





See the moments package for the skewness function.

– Dave2e
Mar 8 at 0:35












2 Answers
2






active

oldest

votes


















1














dput(data)
structure(list(um = c(1.2, 1.2, 2.2, 3), dois = c(1.9, 1.1, 1.2, 1.8), tres = c(0.1, 2, 0.4, 2.6)), class = "data.frame", row.names = c(NA,-4L))


# to use the function skewnewss install and load the package e1071
library(e1071)
sapply(data, function(x) c(means = mean(x), vars = var(x), skews = skewness(x)))


The output generates the required calculations in one go, for each of the input columns:



 um dois tres
means 1.9000000 1.5000000 1.27500000
vars 0.7600000 0.1666667 1.47583333
skews 0.2535648 0.0000000 0.05788459





share|improve this answer

























  • Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

    – neilfws
    Mar 8 at 1:19











  • I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

    – Frank Terra
    Mar 8 at 15:40


















1














You don't explain whether "of the dataframe" means "by column". Let's assume it does and use tidyr::gather() to reshape the data from wide to long, then dplyr::summarise() for the calculations. Assume the dataframe is named data1 and using moments::skewness:



library(tidyr)
library(dplyr)
library(moments)

data1 %>%
gather(Var, Val) %>%
group_by(Var) %>%
summarise(Mean = mean(Val),
Vari = var(Val),
Skew = skewness(Val))

# A tibble: 3 x 4
Var Mean Vari Skew
<chr> <dbl> <dbl> <dbl>
1 dois 1.5 0.167 0
2 tres 1.28 1.48 0.0891
3 um 1.9 0.76 0.390


If you want values for all numbers, not by column, just omit the group_by.



If you want to use your own function - no need since R supplies them in this case - but you could just substitute their names.






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%2f55054837%2ffunction-that-calculates-mean-variance-and-skewness-at-the-same-time-in-a-data%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    dput(data)
    structure(list(um = c(1.2, 1.2, 2.2, 3), dois = c(1.9, 1.1, 1.2, 1.8), tres = c(0.1, 2, 0.4, 2.6)), class = "data.frame", row.names = c(NA,-4L))


    # to use the function skewnewss install and load the package e1071
    library(e1071)
    sapply(data, function(x) c(means = mean(x), vars = var(x), skews = skewness(x)))


    The output generates the required calculations in one go, for each of the input columns:



     um dois tres
    means 1.9000000 1.5000000 1.27500000
    vars 0.7600000 0.1666667 1.47583333
    skews 0.2535648 0.0000000 0.05788459





    share|improve this answer

























    • Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

      – neilfws
      Mar 8 at 1:19











    • I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

      – Frank Terra
      Mar 8 at 15:40















    1














    dput(data)
    structure(list(um = c(1.2, 1.2, 2.2, 3), dois = c(1.9, 1.1, 1.2, 1.8), tres = c(0.1, 2, 0.4, 2.6)), class = "data.frame", row.names = c(NA,-4L))


    # to use the function skewnewss install and load the package e1071
    library(e1071)
    sapply(data, function(x) c(means = mean(x), vars = var(x), skews = skewness(x)))


    The output generates the required calculations in one go, for each of the input columns:



     um dois tres
    means 1.9000000 1.5000000 1.27500000
    vars 0.7600000 0.1666667 1.47583333
    skews 0.2535648 0.0000000 0.05788459





    share|improve this answer

























    • Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

      – neilfws
      Mar 8 at 1:19











    • I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

      – Frank Terra
      Mar 8 at 15:40













    1












    1








    1







    dput(data)
    structure(list(um = c(1.2, 1.2, 2.2, 3), dois = c(1.9, 1.1, 1.2, 1.8), tres = c(0.1, 2, 0.4, 2.6)), class = "data.frame", row.names = c(NA,-4L))


    # to use the function skewnewss install and load the package e1071
    library(e1071)
    sapply(data, function(x) c(means = mean(x), vars = var(x), skews = skewness(x)))


    The output generates the required calculations in one go, for each of the input columns:



     um dois tres
    means 1.9000000 1.5000000 1.27500000
    vars 0.7600000 0.1666667 1.47583333
    skews 0.2535648 0.0000000 0.05788459





    share|improve this answer















    dput(data)
    structure(list(um = c(1.2, 1.2, 2.2, 3), dois = c(1.9, 1.1, 1.2, 1.8), tres = c(0.1, 2, 0.4, 2.6)), class = "data.frame", row.names = c(NA,-4L))


    # to use the function skewnewss install and load the package e1071
    library(e1071)
    sapply(data, function(x) c(means = mean(x), vars = var(x), skews = skewness(x)))


    The output generates the required calculations in one go, for each of the input columns:



     um dois tres
    means 1.9000000 1.5000000 1.27500000
    vars 0.7600000 0.1666667 1.47583333
    skews 0.2535648 0.0000000 0.05788459






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 0:53









    d.b

    20.5k41949




    20.5k41949










    answered Mar 8 at 0:43









    SadiazSadiaz

    917




    917












    • Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

      – neilfws
      Mar 8 at 1:19











    • I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

      – Frank Terra
      Mar 8 at 15:40

















    • Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

      – neilfws
      Mar 8 at 1:19











    • I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

      – Frank Terra
      Mar 8 at 15:40
















    Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

    – neilfws
    Mar 8 at 1:19





    Note that e1071::skewness takes the option type = from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See also moments::skewness, which returns values as for type = 1.

    – neilfws
    Mar 8 at 1:19













    I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

    – Frank Terra
    Mar 8 at 15:40





    I have to create one function that calculates mean, variance and skewness for each column. I have an error when I execute sappy: "Error in var(x) : is.atomic(x) is not TRUE"

    – Frank Terra
    Mar 8 at 15:40













    1














    You don't explain whether "of the dataframe" means "by column". Let's assume it does and use tidyr::gather() to reshape the data from wide to long, then dplyr::summarise() for the calculations. Assume the dataframe is named data1 and using moments::skewness:



    library(tidyr)
    library(dplyr)
    library(moments)

    data1 %>%
    gather(Var, Val) %>%
    group_by(Var) %>%
    summarise(Mean = mean(Val),
    Vari = var(Val),
    Skew = skewness(Val))

    # A tibble: 3 x 4
    Var Mean Vari Skew
    <chr> <dbl> <dbl> <dbl>
    1 dois 1.5 0.167 0
    2 tres 1.28 1.48 0.0891
    3 um 1.9 0.76 0.390


    If you want values for all numbers, not by column, just omit the group_by.



    If you want to use your own function - no need since R supplies them in this case - but you could just substitute their names.






    share|improve this answer



























      1














      You don't explain whether "of the dataframe" means "by column". Let's assume it does and use tidyr::gather() to reshape the data from wide to long, then dplyr::summarise() for the calculations. Assume the dataframe is named data1 and using moments::skewness:



      library(tidyr)
      library(dplyr)
      library(moments)

      data1 %>%
      gather(Var, Val) %>%
      group_by(Var) %>%
      summarise(Mean = mean(Val),
      Vari = var(Val),
      Skew = skewness(Val))

      # A tibble: 3 x 4
      Var Mean Vari Skew
      <chr> <dbl> <dbl> <dbl>
      1 dois 1.5 0.167 0
      2 tres 1.28 1.48 0.0891
      3 um 1.9 0.76 0.390


      If you want values for all numbers, not by column, just omit the group_by.



      If you want to use your own function - no need since R supplies them in this case - but you could just substitute their names.






      share|improve this answer

























        1












        1








        1







        You don't explain whether "of the dataframe" means "by column". Let's assume it does and use tidyr::gather() to reshape the data from wide to long, then dplyr::summarise() for the calculations. Assume the dataframe is named data1 and using moments::skewness:



        library(tidyr)
        library(dplyr)
        library(moments)

        data1 %>%
        gather(Var, Val) %>%
        group_by(Var) %>%
        summarise(Mean = mean(Val),
        Vari = var(Val),
        Skew = skewness(Val))

        # A tibble: 3 x 4
        Var Mean Vari Skew
        <chr> <dbl> <dbl> <dbl>
        1 dois 1.5 0.167 0
        2 tres 1.28 1.48 0.0891
        3 um 1.9 0.76 0.390


        If you want values for all numbers, not by column, just omit the group_by.



        If you want to use your own function - no need since R supplies them in this case - but you could just substitute their names.






        share|improve this answer













        You don't explain whether "of the dataframe" means "by column". Let's assume it does and use tidyr::gather() to reshape the data from wide to long, then dplyr::summarise() for the calculations. Assume the dataframe is named data1 and using moments::skewness:



        library(tidyr)
        library(dplyr)
        library(moments)

        data1 %>%
        gather(Var, Val) %>%
        group_by(Var) %>%
        summarise(Mean = mean(Val),
        Vari = var(Val),
        Skew = skewness(Val))

        # A tibble: 3 x 4
        Var Mean Vari Skew
        <chr> <dbl> <dbl> <dbl>
        1 dois 1.5 0.167 0
        2 tres 1.28 1.48 0.0891
        3 um 1.9 0.76 0.390


        If you want values for all numbers, not by column, just omit the group_by.



        If you want to use your own function - no need since R supplies them in this case - but you could just substitute their names.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 1:24









        neilfwsneilfws

        18.6k53749




        18.6k53749



























            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%2f55054837%2ffunction-that-calculates-mean-variance-and-skewness-at-the-same-time-in-a-data%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

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved