【问题标题】:How To get skewness for every row on dataframe in R如何获取 R 中数据帧上每一行的偏度
【发布时间】:2022-01-23 07:13:19
【问题描述】:

我尝试使用

获取每一行数据场的偏度

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

【问题讨论】:

  • 不要发布数据帧的图片。而是使用dput(df)。见这里 stackoverflow.com/questions/5963269/…>

标签: r dataframe row


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以apply e1071::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")
    

    【讨论】:

      【解决方案3】:

      首先将, 更改为.

      然后转换为数字。

      然后使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        • 2021-09-16
        • 2013-07-19
        相关资源
        最近更新 更多