Export a numeric vector as a symbol in CSV

Let the following vectors:

x <- c(123456789123456789, 987654321987654321) y <- as.character(x) 

I am trying to export y to a csv file for later conversion to XLSX (do not ask, the client requested), for example:

  write.csv(y, file = 'y.csv', row.names = F) 

If I open y.csv in a pure word processor, I see that he correctly inserted the quotation marks around the elements, but when I open it in Excel, the program insists on converting the column to numbers and displaying the contents in a scientific format. This requires an additional reformatting step column, which can be in real time when you work with a large number of files.

How to format a character vector from 20-digit numbers to R so that Excel does not display them in scientific notation?

+4
source share
2 answers

Instead of opening the csv file through File->Open you can go to Data->From Text in Excel and specify Column data format as Text in the last step.

Not sure, however, that you save this time, you can also use the WriteXLS package (or some other direct to xls).


change

Here is a much better way to get Excel to read as text:

 write.csv(paste0('"=""', y, '"""'), 'y.csv', row.names = F, quote = F) 
+5
source

In Excel, select a column of numbers and format them as text. (Cell format → Number tab → text in the list on the left)

+1
source

Source: https://habr.com/ru/post/1488744/


All Articles