[Rd] Producing different text formats in R

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Thu Aug 8 14:46:50 CEST 2019


>>>>> "Lluís" ==   <Lluis.Hurtado using uv.es> writes:

    Lluís> Dear all,
    Lluís> I am facing a strange problem with text files formats.

    Lluís> I am currently using a C++ code named voro++ (http://math.lbl.gov/voro++/). This code accepts text files with four columns:

    Lluís> <id> <x> <y> <z>

    Lluís> where id is an identification number and x,y,z are the coordinates of a point in a 3D space.

    Lluís> The input file, name it myfile_cpp.txt, is generated by another C++ code written by myself (named quadscheme.cpp). So far, these calculations involve no R code and it works just fine.

    Lluís> However, now I am trying to replace my C++ code quadscheme.cpp by a R function. The four columns (id,x,y,z) are produced in a matrix or Data Frame and then saved in a file myfile_r.txt using write.table(). For example using the following function:

    Lluís> quadscheme <- function(window, ntile) {
    Lluís>   # Creates a grid of points in a 3D orthohedron. 
    Lluís>   # First point lies at the (mix,miny,miz) corner of the volume and the last one at (maxx,maxy,maxz)
    Lluís>   # window: vector. Contains the minimum and maximum values of a 3D orthohedron
    Lluís>   #       window <- c(minx, maxx, miny, maxy, minz, maxz)
    Lluís>   # ntile: vector. Number of points per dimension minus one. We manually add a extra row of points per dimension
    Lluís>   #       ntile <- c(nstepx, nstepy, nstepz)
    Lluís>   M <- prod(ntile+1) 
    Lluís>   mat <- matrix(NA,M,4)
    Lluís>   mat[,1] <- 1:M # id column
  
    Lluís>   # step length per dimension
    Lluís>   hi <- (window[2] - window[1])/ntile[1]
    Lluís>   hj <- (window[4] - window[3])/ntile[2]
    Lluís>   hk <- (window[6] - window[5])/ntile[3]
  
    Lluís>   c <- 1
    Lluís>   for(i in 0:ntile[1]) {
    Lluís>     x <- i*hi + window[1]
    Lluís>     for(j in 0:ntile[2]) {
    Lluís>       y <- hj*j + window[3]
    Lluís>       for(k in 0:ntile[3]) {
    Lluís>         z <- k*hk + window[5]
    Lluís>         mat[c,2:4] <- c(x,y,z) # Add a new point to the grid
    Lluís>         c <- c + 1
    Lluís>       }
    Lluís>     }
    Lluís>   }
    Lluís>   write.table(mat, file="myfile_r.txt", row.names=FALSE, col.names=FALSE)
    Lluís> }

    Lluís> And then calling:

    >> window <- c(18, 43, 171, 196, 95, 102)
    >> ntile <- c(100,100,28)
    >> quadscheme(window, ntile)

    Lluís> I see no difference between both myfile_*.txt files,
    Lluís> one is created with C++ and the other one with
    Lluís> R. However, function voro++ do not accept the later one
    Lluís> (created with R and saved with write.table). I've also
    Lluís> noted that voro++ usually accepts R generated files
    Lluís> when they are small enough, but it accepts all C++
    Lluís> generated files, regardless the size.

    Lluís> I know this is more a C++ than a R question, but may be
    Lluís> you can help me as well. I suspect that even if both
    Lluís> files are *.txt they have some differences that I
    Lluís> cannot see. Is there any way in R to produce different
    Lluís> kinds of txt files? Like binary instead of text files?
    Lluís> Could this be the problem?

    Lluís> I attach two identical files, one produced by C++
    Lluís> (myfile_cpp.txt) and another one by the previous R
    Lluís> function (myfile_r.txt). I attach as well the C++ code
    Lluís> used to generate myfile_cpp.txt.

    Lluís> Thank you in advance,

    Lluís> Lluís Hurtado-Gil

In your R file, scientific notation is used:

R:
  99999 26.5 174.5 96.5
  1e+05 26.5 174.5 96.75
  100001 26.5 174.5 97

cpp:
  99999 26.5 174.5 96.5
  100000 26.5 174.5 96.75
  100001 26.5 174.5 97

Try setting options(scipen = 20) before you write the
file in R.



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-devel mailing list