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;
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
add a comment |
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
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 themoments
package for the skewness function.
– Dave2e
Mar 8 at 0:35
add a comment |
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
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
r function
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 themoments
package for the skewness function.
– Dave2e
Mar 8 at 0:35
add a comment |
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 themoments
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
add a comment |
2 Answers
2
active
oldest
votes
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
Note thate1071::skewness
takes the optiontype =
from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See alsomoments::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
add a comment |
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.
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%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
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
Note thate1071::skewness
takes the optiontype =
from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See alsomoments::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
add a comment |
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
Note thate1071::skewness
takes the optiontype =
from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See alsomoments::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
add a comment |
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
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
edited Mar 8 at 0:53
d.b
20.5k41949
20.5k41949
answered Mar 8 at 0:43
SadiazSadiaz
917
917
Note thate1071::skewness
takes the optiontype =
from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See alsomoments::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
add a comment |
Note thate1071::skewness
takes the optiontype =
from 1-3. Whilst 3 is the default, 1 is the "typical" definition. See alsomoments::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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 8 at 1:24
neilfwsneilfws
18.6k53749
18.6k53749
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%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
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
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