Exporting to Excel doesn't work

I am completely new with R/Rstudio, and have no time or desire to learn all these chinese commands; I just want to export a filtered selection from a table (called 'NHANES') to Excel (or XML or TXT). But, something that should be a very basic standard option in a statistical software package seems to be a very complicated task in this very user unfriendly software.
After a long search I found this:
install.packages("writexl")
write_xlsx(NHANES, "c:\temp\test.xls", col_names=TRUE)

But this only works half:

  • Only numbers are saved, not strings and not the column names.
  • All the data are saved, while I want only my filtered selection.

I hope someone here can give me a simple instruction 'for dummies' to accomplish this.
Thanks in advance!

Ok, I'll bite.

R is not a statistical software package, it's a programming language, primarily for statistical purposes. RStudio is an IDe (integrated development environment) which makes work easier.

What you describe as "Chinese" are commands or functions to perform certain actions.

What you have written will write the object NHANES to a file, but you have not specified a valid file name. You either need to change this to "c:/temp/test.xls" (any system) or "c:\temp\test.xls" (Windows only).

For anythingelse you'll need to provide something we can reproduce:

1 Like

Thanks for replying. The filename doesn't seem to be the problem. It is created indeed and I can open it, but as I said, it only contains the numbers from the table, not the strings and not the titles, which seems like a serious bug to me.

Ok, as you have not provided a reproducible example that would allow people to help and diagnose your issue, maybe reflect on what's more likely: a serious bug not identified in software used by millions of people daily or you have made an error?

Without knowing your code it's only possible to speculate, but I would guess that NHANES is a matri and not a dataframe. write_xlsx() requires a dataframe.

I have absolutely no idea how to produce new data in this program. It is just a '.RDA' file that was sent to me that I was able to open with RStudio. All I want to do is make some selections in it (which works fine) and then save the result in excel.

You may prefer to write your object not to xlsx but to CSV format which is perhaps more convenient (plainer less to go wrong) and has the benefit of being directly viewable in a pure text form. that would be something like

write.csv(NHANES, "c:\\temp\\test.csv")

If this is problematic as well, or you insist on xlsx, then I would ask that you consider using

dput(head(NHANES))

which will provide us a textual representation ('dput') of the R object based on the top 20 rows ('the head')
we can then try to write is as xlsx

1 Like

Thank you. The csv works nicely.
Is it possible to convert only the filtered selection that I see in RStudio and not the entire file?

At the moment Rstudio does not support exporting the product of interactive filtering of its 'view', therefore at this time some code would have to be written.
Can you be explicit here about what filter you want to apply ?


I want to save only this.

So you picked Male and did what for Age?

Age was 26 - 30. The filtering worked as expected.

( newout <- NHANES [26<=NHANES$age & 
                  NHANES$age<=30 &
                  NHANES$sex=='Male',] )

then write out this dataframe 'newout' to csv

1 Like

This topic was automatically closed 21 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.