Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read dataset in R in which comma is used for field separator and decimal point

How could you read this dataset in R, the problem is that the numbers are floats and are like 4,000000059604644E+16 and they are separated by a ,

4,000000059604644E-16 ,  7,999997138977056E-16,   9,000002145767216E-16
4,999999403953552E-16 ,  6,99999988079071E-16 ,   0,099999904632568E-16
9,999997615814208E-16 ,  4,30000066757202E-16 ,   3,630000114440918E-16
0,69999933242798E-16  ,  0,099999904632568E-16,  55,657576767799999E-16 
3,999999761581424E-16,   1,9900000095367432E-16,  0,199999809265136E-16

How would you load this kinf of dataset in R so it has 3 columns.

If I do

dataset <- read.csv("C:\\data.txt",header=T,row.names=NULL)

it would return 6 columns instead 3...

like image 494
edgarmtze Avatar asked Sep 24 '11 19:09

edgarmtze


People also ask

How do you set a decimal separator in R?

Thus, we might want to create data or perform calculations with comma as decimal separator. In R, we can do this by just using the code options(OutDec=","). Once we will type this in R console, all the numerical values with decimals will be printed with commas in place of dots.

Can R read decimals?

If you don't get the number of columns you expect In many countries this is not an issue, but the Swedish standard is using a comma as decimal separator, while R uses a decimal point.


1 Answers

It might be best to transform that input data to use decimal points, rather than commas, in the floating point numbers. One way you could do this is to use sed (it looks like you are using Windows, so you would likely need to sed to use this approach):

sed 's/\([0-9]\),\([0-9]\)/\1.\2/g' data.txt  > data2.txt

File data2 looks like this:

4.000000059604644E-16 ,  7.999997138977056E-16,   9.000002145767216E-16
4.999999403953552E-16 ,  6.99999988079071E-16 ,   0.099999904632568E-16
9.999997615814208E-16 ,  4.30000066757202E-16 ,   3.630000114440918E-16
0.69999933242798E-16  ,  0.099999904632568E-16,  55.657576767799999E-16 
3.999999761581424E-16,   1.9900000095367432E-16,  0.199999809265136E-16

Then in R:

dataset <- read.csv("data2.txt",row.names=NULL)
like image 170
David Alber Avatar answered Oct 25 '22 08:10

David Alber