Hina
Hina

Reputation: 129

Writing a row with mutiple data into csv r

So my dataframe looks like this

> df
 file_name        some_value               
 test.csv         12815415,5712365,12815383,13700109,12806007 

 write.csv(df,file="E:/output.csv",row.names = F)

And the resulting csv table looks like this

file_name        some_value   
test.csv         1.281545E+38

It seems that only the first entry in some_value is written into the csv, but where does the E+38 come from? Any advice to fix this, so that there are 5 entries under the column some_value?

> dput(df)
structure(list(file_name = structure(1L, .Label = "test.csv", class = 
"factor"), 
some_value = structure(1L, .Label = 
"12815415,5712365,12815383,13700109,12806007", class = "factor")), .Names = 
c("file_name", 
"some_value"), row.names = c(NA, -1L), class = "data.frame")

Upvotes: 1

Views: 47

Answers (3)

Kerry Jackson
Kerry Jackson

Reputation: 1871

To fix this, you can start the field with a single quote.

df$some_value <- paste0("'",df$some_value) #Add a single quote to the beginning to indicate to Excel that it is a text field

Please note that the E+38 is really an Excel issue not an R issue. It looks like the E+38 comes in because the comma separated values are entered in a single field in the csv file. If you open this in Excel, Excel is treating it like a single number where the commas are formatting (e.g. separating thousands from hundreds), so the number in scientific notation has 38 zeros. If you open the csv file in a text editor I think it would not look like this.

If I look at the file (without the apostrophe) in Notepad++ I see File in text editor with all numbers and commas

If I look at the same file in Excel, I see:enter image description here Excel has interpreted some_value as a single number and the commas as formatting and shown that number in scientific notation.

Excel can be forced to treat the value as text by having a single quote before the numbers.

Upvotes: 2

Raja
Raja

Reputation: 167

Your are getting those kind of value because of the default visual property of MS excel (In windows). Open the .CSV file in Note pad (Right click and open with notepad) to get the exact output.

Upvotes: 0

Nar
Nar

Reputation: 658

Just use at least one character as first element of some_values e.g. s12815415,5712365,12815383,13700109,12806007 . Then export do consider some_values as a text and extract whole string

Upvotes: 0

Related Questions