Producing different text formats in R

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Producing different text formats in R

Lluis.Hurtado
Dear all,

I am facing a strange problem with text files formats.

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

<id> <x> <y> <z>

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

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.

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:

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

And then calling:

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

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

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

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

Thank you in advance,

Lluís Hurtado-Gil

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

myfile_cpp.txt (8M) Download Attachment
myfile_r.txt (8M) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Producing different text formats in R

Enrico Schumann-2
>>>>> "Lluís" ==   <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Producing different text formats in R

Dirk Eddelbuettel
In reply to this post by Lluis.Hurtado

Lluís,

You sent a 8+ mb file to a (I presume) 1000+ subscribers of this list. That
is not generally a good idea, and I am surprised this got past the mailman
software which (usually) filters at 100k (and for a reason).

In the future, please consider uploading the file somewhere (GitHub gists are
easy) and send a link.  We also often talk about "minimally complete
verifiable examples" (mcve) and the minimal is there for a reason.

Thanks for your consideration,  Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Producing different text formats in R

Lluis.Hurtado
In reply to this post by Enrico Schumann-2
Thank you for your fast answer. It just works fine now.

And apologies for attaching heavy files.

Lluís.

> >>>>> "Lluís" ==   <[hidden email]> 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://mathlbl.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
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel