Search code examples
rpsychdescribe.by

Export describe.by (package psych) as csv file in R


Does anyone know how to export describe.by statistics to csv in R? I get this message:

estatistica <- describe.by(pag,list(pag$Jogo)

  write.table(estatistica,file="H:/Myfile.csv",sep=",")
  "Erro em as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""by"" to a data.frame"

Since its such a general question, a general example would do.

        Jogo  Pais  Numero
          A  Canada    1
          B  Canada    2
          C  Canada    1
          D  Canada    4
          A  Brazyl    6
          B  Brazyl    7
          A  France    1
          B  France    1
          C  France    2
          D  France    3

Solution

  • Function describeBy() (this should be used instead of descrive.by() that is deprecated) produces list of data frame and therefore can't be written to file with write.table().

    library(psych)    
    estatistica <- describeBy(pag,list(pag$Jogo))
    estatistica
    
    : A
           var n mean   sd median trimmed  mad min max range skew kurtosis   se
    Jogo*    1 3 1.00 0.00      1    1.00 0.00   1   1     0  NaN      NaN 0.00
    Pais*    2 3 2.00 1.00      2    2.00 1.48   1   3     2 0.00    -2.33 0.58
    Numero   3 3 2.67 2.89      1    2.67 0.00   1   6     5 0.38    -2.33 1.67
    -------------------------------------------------------------------- 
    : B
           var n mean   sd median trimmed  mad min max range skew kurtosis   se
    Jogo*    1 3 2.00 0.00      2    2.00 0.00   2   2     0  NaN      NaN 0.00
    Pais*    2 3 2.00 1.00      2    2.00 1.48   1   3     2 0.00    -2.33 0.58
    Numero   3 3 3.33 3.21      2    3.33 1.48   1   7     6 0.34    -2.33 1.86
    

    To solve this problem one way would be to put all list elements into one data frame with do.call() and rbind() and then write to file. This will make data frame where group names will be added before original variable names.

    estatistica2<-do.call("rbind",estatistica)
    estatistica2
    
             var n mean   sd median trimmed  mad min max range skew kurtosis   se
    A.Jogo*    1 3 1.00 0.00    1.0    1.00 0.00   1   1     0  NaN      NaN 0.00
    A.Pais*    2 3 2.00 1.00    2.0    2.00 1.48   1   3     2 0.00    -2.33 0.58
    A.Numero   3 3 2.67 2.89    1.0    2.67 0.00   1   6     5 0.38    -2.33 1.67
    B.Jogo*    1 3 2.00 0.00    2.0    2.00 0.00   2   2     0  NaN      NaN 0.00
    B.Pais*    2 3 2.00 1.00    2.0    2.00 1.48   1   3     2 0.00    -2.33 0.58
    B.Numero   3 3 3.33 3.21    2.0    3.33 1.48   1   7     6 0.34    -2.33 1.86
    C.Jogo*    1 2 3.00 0.00    3.0    3.00 0.00   3   3     0  NaN      NaN 0.00
    C.Pais*    2 2 2.50 0.71    2.5    2.50 0.74   2   3     1 0.00    -2.75 0.50
    C.Numero   3 2 1.50 0.71    1.5    1.50 0.74   1   2     1 0.00    -2.75 0.50
    D.Jogo*    1 2 4.00 0.00    4.0    4.00 0.00   4   4     0  NaN      NaN 0.00
    D.Pais*    2 2 2.50 0.71    2.5    2.50 0.74   2   3     1 0.00    -2.75 0.50
    D.Numero   3 2 3.50 0.71    3.5    3.50 0.74   3   4     1 0.00    -2.75 0.50