0

I would like to use the Stargazer function for word for my descriptive statistic. I already installed the stargazer package.

This is my code:

#Stargazer tables for word 
stargazer(as.matrix(psych::describe(data_corruption[c("innovation_firm", 
            "corruption", "ownership_gov", "competition", "gender_tm", 
            "size_firm", "age_firm", "RandD", "export", 
            "corruption_constraint")])), type = "html", 
            title="Descriptive statistics", digits = 2, 
            out="Descriptive_statistics.doc")

And this is my error:

Error in if (is.na(s)) { : the condition has length > 1

Can someone please help me?

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
Carla
  • 1
  • Hey, thank you jared_mamrot. But I cannot see a difference to my code. – Carla Feb 22 '23 at 10:25
  • Welcome to SO, Carla! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data so that others could run your code. I just checked your code using `mtcars[c("cyl", "mpg")]` as example data and it works fine. From that I would guess that the issue is related to your data. – stefan Feb 22 '23 at 10:31
  • Thank you so much for your answer. Tbh I am at the very beginning of my knowledge regarding data analytics so it is hard for me to create fake data. What I can say, my data is from the world bank and the following code is working: – Carla Feb 22 '23 at 10:35
  • #Descriptive statistics summary(data_corruption) #shows descriptive statistics but not nicely summary(data_corruption[c("innovation_firm", "corruption", "ownership_gov", "competition", "gender_tm", "size_firm", "age_firm", "RandD", "export", "corruption_constraint")]) describe(data_corruption[c("innovation_firm", "corruption", "ownership_gov", "competition", "gender_tm", "size_firm", "age_firm", "RandD", "export", "corruption_constraint")]) #looks nicer/better overview – Carla Feb 22 '23 at 10:35
  • the describe code. Here I get results that make a lot of sense. But I would like to have it as a word doc. and therefore i tried the stargazer function. i tried this code with a Beeps data before and it worked, so i am very confused – Carla Feb 22 '23 at 10:36
  • First. To provide a snippet of your data run `dput(head(data_corruption[c("innovation_firm", "corruption", "ownership_gov", "competition", "gender_tm", "size_firm", "age_firm", "RandD", "export", "corruption_constraint")]))` and copy the output as an edit into your post. Second, I haven't use stargazer myself. So I might be wrong, but I don't think stargazer is the way to go for Word output, e.g. if I use your code I get an html table as specified via `type="html"`. – stefan Feb 22 '23 at 10:47
  • `stargazer::stargazer(as.matrix(describe(sat.act)), type = "html", title="Descriptive statistics", digits = 2, out="Descriptive_statistics.doc")` seems to work as expected. Does it for you? If so, then the issue is with your data. – user20650 Feb 22 '23 at 11:00
  • @stefan; re output type, this is what was often done in the old pre-knitr et al days; data was wrote out using html tables and then saved file as a word doc .. so a doc/docx can be rproduced – user20650 Feb 22 '23 at 11:04

1 Answers1

0

I think the error is sometimes related to the number of variables or length of variable / model names. Your approach works with minimal data:

library(stargazer)

stargazer(as.matrix(psych::describe(mtcars)), type = "text", 
          title="Descriptive statistics", digits = 2)

Descriptive statistics
========================================================================================
     vars n   mean    sd   median trimmed  mad    min   max  range  skew  kurtosis  se  
----------------------------------------------------------------------------------------
mpg   1   32 20.09   6.03  19.20   19.70   5.41  10.40 33.90 23.50  0.61   -0.37   1.07 
cyl   2   32  6.19   1.79    6     6.23    2.97    4     8     4    -0.17  -1.76   0.32 
disp  3   32 230.72 123.94 196.30 222.52  140.48 71.10  472  400.90 0.38   -1.21   21.91
hp    4   32 146.69 68.56   123   141.19  77.10   52    335   283   0.73   -0.14   12.12
drat  5   32  3.60   0.53   3.70   3.58    0.70  2.76  4.93   2.17  0.27   -0.71   0.09 
wt    6   32  3.22   0.98   3.33   3.15    0.77  1.51  5.42   3.91  0.42   -0.02   0.17 
qsec  7   32 17.85   1.79  17.71   17.83   1.42  14.50 22.90  8.40  0.37    0.34   0.32 
vs    8   32  0.44   0.50    0     0.42     0      0     1     1    0.24   -2.00   0.09 
am    9   32  0.41   0.50    0     0.38     0      0     1     1    0.36   -1.92   0.09 
gear  10  32  3.69   0.74    4     3.62    1.48    3     5     2    0.53   -1.07   0.13 
carb  11  32  2.81   1.62    2     2.65    1.48    1     8     7    1.05    1.26   0.29 
----------------------------------------------------------------------------------------

Also exporting as .doc, try this and tell if you still get the error:

stargazer(as.matrix(psych::describe(mtcars)), type = "html", 
          title="Descriptive statistics", digits = 2, out="Descriptive_statistics.doc")

If this minimal command is working for you, try to outsource the column selection before you do stargazer:

df <- as.matrix(psych::describe(data_corruption[c("innovation_firm", "corruption", "ownership_gov", "competition", "gender_tm", "size_firm", "age_firm", "RandD", "export", "corruption_constraint")])

stargazer(df), type = "html", title="Descriptive statistics", digits = 2, out="Descriptive_statistics.doc")
Marco
  • 2,368
  • 6
  • 22
  • 48