开发者

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.开发者_如何学Python..


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)


Here is an all R solution that uses three read.table calls. The first read.table statement reads each data row as 6 fields; the second read.table statement puts the fields back together properly and reads them and the third grabs the names from the header.

fn <- "data.txt"

# create a test file

Lines <- "A , B , C
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"
cat(Lines, "\n", file = fn)

# now read it back in

DF0 <- read.table(fn, skip = 1, sep = ",", colClasses = "character")
DF <- read.table(
   file = textConnection(do.call("sprintf", c("%s.%s %s.%s %s.%s", DF0))), 
   col.names = names(read.csv(fn, nrow = 0))
)

which gives:

> DF
             A            B            C
1 4.000000e-16 7.999997e-16 9.000002e-16
2 4.999999e-16 7.000000e-16 9.999990e-18
3 9.999998e-16 4.300001e-16 3.630000e-16
4 6.999993e-17 9.999990e-18 5.565758e-15
5 4.000000e-16 1.990000e-16 1.999998e-17

Note: The read.csv statement in the question implies that there is a header but the sample data does not show one. I assumed that there is a header but if not then remove the skip= and col.names= arguments.


It's not pretty, but it should work:

x <- matrix(scan("c:/data.txt", what=character(), sep=","), byrow=TRUE, ncol=6)
y <- t(apply(x, 1, function(a) { left <- seq(1, length(a), by=2)
                               as.numeric(paste(a[left], a[left+1], sep="."))
                             } ))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜