Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 924
karips Avatar asked Apr 29 '13 13:04

karips


2 Answers

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
like image 171
Didzis Elferts Avatar answered Sep 28 '22 23:09

Didzis Elferts


Actually, there is an option in describeBy that creates matrix form output to just this. Using the original data,

describeBy(pag,pag$Jogo,mat=TRUE)

        item group1 var n     mean        sd median  trimmed    mad min max range      skew  kurtosis        se
Jogo*1     1      A   1 3 1.000000 0.0000000    1.0 1.000000 0.0000   1   1     0          NaN       NaN 0.0000000
Jogo*2     2      B   1 3 2.000000 0.0000000    2.0 2.000000 0.0000   2   2     0       NaN       NaN 0.0000000
Jogo*3     3      C   1 2 3.000000 0.0000000    3.0 3.000000 0.0000   3   3     0       NaN       NaN 0.0000000
Jogo*4     4      D   1 2 4.000000 0.0000000    4.0 4.000000 0.0000   4   4     0       NaN       NaN 0.0000000
Pais*1     5      A   2 3 2.000000 1.0000000    2.0 2.000000 1.4826   1   3     2 0.0000000 -2.333333 0.5773503
Pais*2     6      B   2 3 2.000000 1.0000000    2.0 2.000000 1.4826   1   3     2 0.0000000 -2.333333 0.5773503
Pais*3     7      C   2 2 2.500000 0.7071068    2.5 2.500000 0.7413   2   3     1 0.0000000 -2.750000 0.5000000
Pais*4     8      D   2 2 2.500000 0.7071068    2.5 2.500000 0.7413   2   3     1 0.0000000 -2.750000 0.5000000
Numero1    9      A   3 3 2.666667 2.8867513    1.0 2.666667 0.0000   1   6     5 0.3849002 -2.333333 1.6666667
Numero2   10      B   3 3 3.333333 3.2145503    2.0 3.333333 1.4826   1   7     6 0.3434206 -2.333333 1.8559215
Numero3   11      C   3 2 1.500000 0.7071068    1.5 1.500000 0.7413   1   2     1 0.0000000 -2.750000 0.5000000
Numero4   12      D   3 2 3.500000 0.7071068    3.5 3.500000 0.7413   3   4     1 0.0000000 -2.750000 0.5000000

Unfortunately, the current version does not have the rounded output that describeBy does normally. I have fixed this in Version 1.3.6 (about to be released).

Bill

like image 39
William Revelle Avatar answered Sep 28 '22 22:09

William Revelle