R xlsx Excel number formats - r

I have created a workbook (createWorkbook) and formatted the headings, however how do I format the number values such that when I write/save to Excel I get the following:
For the first field e.g.
3615 becomes 3,615
For subsequent fields e.g.
70.658 becomes 70.7
Note, I still want Excel to recognize the values as doubles.
Thank you!

Related

How to stop R from automatically converting string to date?

I have a large excel file I'm reading into R for a sports analytics project. One of the columns is height, and the format in excel is ft-in (i.e. 5-10). This is fine in excel because that column was specifically formatted to take in plain text and not convert it to date. But when I read the csv to R, the data frame auto converts to date. Is there a command/parameter to have it not do this?

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.
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.
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

How do I control the format of columns when I use the write.csv function?

I've created a dataframe in R and one of my columns convert a date such as 01/08/2018 (dd/mm/yyyy) into text form Aug-18 (mmm-yy). However, when I write this to csv using the write.csv function, Excel automatically converts this to date.
Is there a way I can specify the column type to be "Text" so that Excel doesn't change it to date format?
One simple trick that IMHO gets far too little attention is to pad your date colums with whitespace, e.g. df$mydate <- paste(' ', df$mydate, sep=''). This stops Excel from translating the text as dates.
I have started routinely doing that for all kinds of risky columns when doing R<->Excel transformations.
Taken from here: https://support.office.com/en-us/article/stop-automatically-changing-numbers-to-dates-452bd2db-cc96-47d1-81e4-72cec11c4ed8

How to split one column containing several values so each column only contains one value?

starting situation as follows:
I've got a csv files with roughly 3000 rows, but only 1 column. In each of the rows there are several values included.
Now I want to assign only one value per column.
How do I manage to do that?
convert the file into txt format and then open the data using MS excel. Don't directly open the file. Open it using Open option in file menu. When you do this a text wizard will appear. You can then split your data by using delimited such as commas, spaces and form multiple columns. Once you are done with it, you save the file in csv format

Read excel data as is using readxl in R

I have to read an excel file in R. The excelfile has a column with values such as 50%,20%... and another column with dates in the format "12-December-2017" but R converts both the column datas.
I am using readxl package and i specified in col_types parameter all the columns to be read as text but when i check the dataframe all the column types are characters but the percentage data and date changes to decimals and numbers respectively.
excelfile2<-read_excel(filePath,col_types=rep("text",8))
I want to read the excel file as is.Any help will be appreciated.
This is because what you visualize inside the Excel is not what actually is stored.
For example, if in excel you visualize "12-December-2017", what is stored in reality is the number of days since 1-1-1899.
My suggestion is to open the Excel file with the TextReader so you have a grasp what really you are reading in R.
Then, you can either define everything as text in excel or you can apply some transformations in R in order to convert the days since 1-1-1899 into a POSIXct format.

Resources