0

I have a data frame with a column. it contains digit numbers separated by ',' and its type is chr. (I'm using R and Rstudio)

I'm going to write this data frame to a .csv file using the code below, but it changes in the file as a large number in each row. What should I do please?

write.csv(ds_transaction,"TransactionList.csv", row.names = T)

I expect that for a row in data frame contains

23210084,23210108,23210095,531,23213016,23210119

I have the same row in the .csv file. But instead, I've got

2,321,008,423,210,100,000,000,000,000,000,000,000,000,000
girijesh96
  • 455
  • 1
  • 4
  • 16
Z_DEV
  • 1
  • 2
  • 2
    How do you open the file? If you use Excel, have you checked with a simple text editor like Notepad what the actual content of the file is? – Roland Jan 29 '19 at 08:23
  • Yeah, it was ok in Notepad. I realized that when I write in .csv file, no matter if it changes. when I read it, we have no problem. That's just how it looks in the file. So I didn't need to do anything. Thank you. – Z_DEV Jan 29 '19 at 12:01

2 Answers2

0

First, remove the comma and then convert it to numeric after it write to CSV for eg.

x = "2,321,008"
y = as.numeric((gsub(",","", x)))
write.csv(y,"TransactionList.csv", row.names = T)

if you have a data frame say df and your column is transaction then, in that case, you can try this

df$transaction = as.numeric((gsub(",","", df$transaction)))
write.csv(df,"TransactionList.csv", row.names = T)
girijesh96
  • 455
  • 1
  • 4
  • 16
  • 1. OP doesn't appear to want to convert to numeric. 2. If they do this, they will have loss of precision with floating point numbers. – Roland Jan 29 '19 at 10:20
  • @Roland 1. he can cast back it to the character if he wants. 2: as.numeric() doesn't cause any loss of precision. – girijesh96 Jan 29 '19 at 10:40
  • Have you seen how long OP's strings are? Of course, there will be loss of precision if they convert these to floating point numbers. In fact, they already show such loss of precision, which probably occurs in the software used to open the file. – Roland Jan 29 '19 at 11:04
  • No, it didn't work for me. I realized that when I write in .csv file, no matter if it changes. when I read it, we have no problem. That's just how it looks in the file. So I didn't need to do anything. Thank you. – Z_DEV Jan 29 '19 at 11:46
  • @Z_DEV Can you explain your problem more elaborately, please? – girijesh96 Jan 29 '19 at 13:25
0

I realized that when I write in .csv file, no matter if it changes in look. when I read it in R, we have no problem. That's just how it looks in the file. So I didn't need to do anything. Thank you all.

Z_DEV
  • 1
  • 2