Liuis@Hurt@do m@iii@g oii uv@es
2019-Aug-08 09:38 UTC
[Rd] Producing different text formats in R
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 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: myfile_cpp.txt URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190808/465ee6d9/attachment-0002.txt> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: myfile_r.txt URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20190808/465ee6d9/attachment-0003.txt>
>>>>> "Llu?s" == <Lluis.Hurtado at 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
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 | edd at debian.org
Liuis@Hurt@do m@iii@g oii uv@es
2019-Aug-09 10:30 UTC
[Rd] Producing different text formats in R
Thank you for your fast answer. It just works fine now. And apologies for attaching heavy files. Llu?s.> >>>>> "Llu?s" == <Lluis.Hurtado at 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://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 >