如何获得R中数据帧上每一行的偏度



我试图用获得每一行数据臂的偏度

library(moments)

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

我的数据帧:

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

您可以applye1071::skewness而不是MARGIN=1,即行。

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

如果你真的有逗号作为小数分隔符,请事先清理:

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

首先将,更改为.

然后转换为数字。

然后将moments包中的skewness函数与c_across一起使用

要将其应用于每行,请在之前使用rowwise()

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

非常感谢您的帮助我用不同的方式做了它,它起到了的作用

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

相关内容

  • 没有找到相关文章

最新更新