Home > Enterprise >  How To get skewness for every row on dataframe in R
How To get skewness for every row on dataframe in R

Time:12-22

i try to get skewness for every row datafarme with

library(moments)

install.packages("FactoMineR")
data <-read.csv("data.csv",header=TRUE,sep=";",dec="."=)

My dataframe:

df <- structure(list(Marque = c("x1", "x2", "x3", "x4", "x5", "x6", 
"x7", "x8", "x9"), V1 = c("2", "3", "1,5", "1,2", "1,9", "3,5", 
"3,3", "4", "3,6"), V2 = c("2,4", "4", "3,4", "3,6", "1,6", "4,8", 
"4,1", "3,5", "4,5"), V3 = c("1,7", "4,4", "3,8", "3,9", "3,4", 
"4,5", "3,9", "4,3", "2,1"), V4 = c("2,3", "4,9", "4,7", "4,3", 
"4", "4,6", "4,9", "2", "3,6"), V5 = c("3,3", "3,9", "2,3", "1,3", 
"1,2", "3,9", "3,6", "3,3", "4")), class = "data.frame", row.names = c(NA, 
-9L))

CodePudding user response:

You may apply e1071::skewness over MARGIN=1 i.e. rows.

apply(df[-1], MARGIN=1, e1071::skewness)
# [1]  0.53353911 -0.25708728 -0.09059249 -0.23259728  0.26626966 -0.34683620
# [7]  0.44701696 -0.56946469 -0.61043085

If you really have commas as decimal separators, clean beforehand:

df[2:6] <- lapply(df[2:6], \(x) as.numeric(gsub(',', '.', x)))

df <- structure(list(Marque = c("x1", "x2", "x3", "x4", "x5", "x6", 
"x7", "x8", "x9"), V1 = c(2, 3, 1.5, 1.2, 1.9, 3.5, 3.3, 4, 3.6
), V2 = c(2.4, 4, 3.4, 3.6, 1.6, 4.8, 4.1, 3.5, 4.5), V3 = c(1.7, 
4.4, 3.8, 3.9, 3.4, 4.5, 3.9, 4.3, 2.1), V4 = c(2.3, 4.9, 4.7, 
4.3, 4, 4.6, 4.9, 2, 3.6), V5 = c(3.3, 3.9, 2.3, 1.3, 1.2, 3.9, 
3.6, 3.3, 4)), row.names = c(NA, -9L), class = "data.frame")

CodePudding user response:

First change , to ..

Then convert to numeric.

Then use skewness function from moments package with c_across

to apply it to each row use rowwise() before:

library(dplyr)
library(moments)

df %>% 
  mutate(across(-1, ~str_replace(., ',', '.'))) %>% 
  type.convert(as.is = TRUE) %>% 
  rowwise() %>% 
  mutate(Skew = skewness(c_across(V1:V5)))
  Marque    V1    V2    V3    V4    V5   Skew
  <chr>  <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
1 x1       2     2.4   1.7   2.3   3.3  0.746
2 x2       3     4     4.4   4.9   3.9 -0.359
3 x3       1.5   3.4   3.8   4.7   2.3 -0.127
4 x4       1.2   3.6   3.9   4.3   1.3 -0.325
5 x5       1.9   1.6   3.4   4     1.2  0.372
6 x6       3.5   4.8   4.5   4.6   3.9 -0.485
7 x7       3.3   4.1   3.9   4.9   3.6  0.625
8 x8       4     3.5   4.3   2     3.3 -0.796
9 x9       3.6   4.5   2.1   3.6   4   -0.853

CodePudding user response:

thanks a lot for your help I did it differently, and it works

vect1= c(1:5)
for(x in vect1)
{
  print(x)
  skew_test = skewness(data[,x])
  print(skew_test)
}
  • Related