Joe
Joe

Reputation: 397

Formatting the exported data in R

I have a list formed from merging data together in R which was extracted from various excel sheets. When I am exporting this list to the Excel, some numbers get stored as text. How can I ensure that numbers/values are stored in the number format and text stored in the text format?

Example of a table I have:

Name1  Ed     23   0.45  DNR   ST  8732
Name2  Bob    -   0.78  Tik   GH  999
Name3  Jose   26   0.23  DNR   TT  1954

Desired outcome: have exactly the same table exported in excel with numeric values being stored in a numeric/general format.

Upvotes: 0

Views: 73

Answers (2)

Fahadakbar
Fahadakbar

Reputation: 518

easier way would be to use 'import dataset' wizard in R studio IDE. wizard is interactive and you can actually see the corresponding code generated as you change the options in the wizard (bottom right corner). say you change one column type that you think is not appropriate, and then you will see the code will change to reflect that. Once you finish the import, you can save the code for future use.

Upvotes: 1

lalit panwar
lalit panwar

Reputation: 96

There is a reason why this happened, when you import data you need to define colClasses parameter of read.table or read.csv, whichever function you are using to read data. Then merge the data frame and check if the class of each variable is right, if not then convert the data type of the data frame to the required datatype. If you have a list then convert it to data frame because data frames are more handy.

If nothing works then convert the datatypes manually to their respective datatypes and then write the table. It will work.

There is a provision that date datatype when write to a file it is automatically converted to character datatype.

Let me know if it resolves your query

Upvotes: 1

Related Questions