Learn R Programming

psych (version 1.0-23)

skew: Calculate skew for a vector, matrix, or data.frame

Description

Find the skew for each variable in a data.frame or matrix. Unlike skew in e1071, this calculates a different skew for each variable or column of a data.frame/matrix.

Usage

skew(x, na.rm = TRUE)

Arguments

x
A data.frame or matrix
na.rm
how to treat missing data

Value

  • if input is a matrix or data.frame, skew is a vector of skews

Details

given a matrix or data.frame x, find the skew for each column.

See Also

describe, describe.by, kurtosi

Examples

Run this code
round(skew(attitude),2)
## The function is currently defined as
function (x, na.rm = TRUE) 
{
    if (length(dim(x)) == 0) {
        if (na.rm) {
            x <- x[!is.na(x)]
        			}
        sdx <- sd(x,na.rm=na.rm)
        mx <- mean(x)
        skewer <- sum((x - mx)^3)/(length(x) * sd(x)^3) 
        } else {
    
    skewer <- rep(NA,dim(x)[2])
    mx <- colMeans(x,na.rm=na.rm)
    sdx <- sd(x,na.rm=na.rm)
    for (i in 1:dim(x)[2]) {
    skewer[i] <- sum((x[,i] - mx[i])^3,  na.rm = na.rm)/((length(x[,i]) - sum(is.na(x[,i]))) * sdx[i]^3)
            }
    }
    return(skewer)
}

Run the code above in your browser using DataLab