簡體   English   中英

如何獲取 R 中 dataframe 上每一行的偏度

[英]How To get skewness for every row on dataframe in R

我嘗試為每一行數據農場獲得偏度

library(moments)

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

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

首先改變,. .

然后轉換為數字。

然后使用來自moments package 和 c_across 的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

您可以在MARGIN=1即行上apply e1071::skewness

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

非常感謝您的幫助我做了不同的事情,它的工作原理

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM