stargazer: can't get a HTML-output on a simple summary function, $ operator is invalid for atomic vectors

Hi there!

I have a data.frame "tab" with several columns, one of the colums is "DD", containing Integers.
I just would like to get a nice HTML-table with the summary output, just like the console output.
All I get is: Error: $ operator is invalid for atomic vectors
The code is simple:

dd["SD"] <- round(sd(tab$DD, na.rm = TRUE),2) # adding column "SD" and it's value for Standard Deviation "sd"

print(dd)  # the wanted console output
#> print(dd)
#  Min. 1st Qu.  Median    Mean 3rd Qu.    Max.      SD    NA's 
 #  1.00    7.00   12.00   12.42   16.50   30.00    6.86       2 

print(class(dd)) #results in:
 #> print(class(dd))
#[1] "summaryDefault" "table" (What ever that might be)

stargazer(dd, out = "StargazerTestDD.html")
#results in:  $ operator is invalid for atomic vectors

I have read dozends of posts regarding this error, but not was dealing with my problem.
Can somebody pls help me to recieve the print(dd)-console output as Html-file.

Many thanks in advance!!!

Hi @raptorrs,
Welcome to the RStudio Community Forum.

The stargazer package may not be the best for this job but nonetheless I got it to work:


dd <- summary(mtcars$mpg) 
dd["SD"] <- round(sd(mtcars$mpg, na.rm = TRUE),2)
print(dd)  # This is in a table.

# Stargazer needs a dataframe or matrix to output tabular information.
# I tried a matrix:

stargazer(t(as.matrix(dd)), type="html", out = "StargazerTestDD.html")

The output file renders correctly in my browser.

Hi DavoWW

Thanks a lot for your answer. It works as sweet as sugar for me :grinning:
I will add it to my code snippets collection.

In the meantime I have found another solution with describe(...) from the package "psych" which is giving me a lot more, but also undesired information:

dd <- summary(tab["DD"]) 
ddd<-describe(dd, na.rm = TRUE, quant=c(.25,.75))
stargazer(ddd, type = "html", summary = FALSE, out = "StargazerTestDD3.html")

Strange enough that this above code works with tab["DD"] but not with tab$DD because it throws

$ operator is invalid for atomic vectors

In your post you mentioned there would be a better package for this job. What would be your recommendation?

Anyway, best regards from Germany and Thanks again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.