Re: [Rd] unique.matrix issue [Was: Anomaly with unique and match]
On Wed, Mar 09, 2011 at 02:11:49PM -0500, Simon Urbanek wrote: > match() is a red herring here -- it is really a very specific thing that has > to do with the fact that you're running unique() on a matrix. Also it's much > easier to reproduce: > > > x=c(1,1+0.2e-15) > > x > [1] 1 1 > > sprintf("%a",x) > [1] "0x1p+0" "0x1.1p+0" > > unique(x) > [1] 1 1 > > sprintf("%a",unique(x)) > [1] "0x1p+0" "0x1.1p+0" > > unique(matrix(x,2)) > [,1] > [1,]1 > > and this comes from the fact that unique.matrix uses string representation > since it has to take into account all values of a row/column so it pastes all > values into one string, but for the two numbers that is the same: > > as.character(x) > [1] "1" "1" I understand the use of match() in the original message by Terry Therneau as an example of a situation, where the behavior of unique.matrix() becomes visible even without looking at the last bits of the numbers. Let me suggest to consider the following example. x <- 1 + c(1.1, 1.3, 1.7, 1.9)*1e-14 a <- cbind(rep(x, each=2), 2) rownames(a) <- 1:nrow(a) The correct set of rows may be obtained using unique(a - 1) [,1] [,2] 1 1.110223e-141 3 1.310063e-141 5 1.709743e-141 7 1.909584e-141 However, due to the use of paste(), which uses as.character(), in unique.matrix(), we also have unique(a) [,1] [,2] 112 512 Let me suggest to consider a transformation of the numeric columns by rank() before the use of paste(). For example unique.mat <- function(a) { temp <- apply(a, 2, rank, ties.method="max") temp <- apply(temp, 1, function(x) paste(x, collapse = "\r")) a[!duplicated(temp), , drop=FALSE] } unique.mat(a) [,1] [,2] 112 312 512 712 Petr Savicky. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] unique.matrix issue [Was: Anomaly with unique and match]
It should be possible to run unique()/duplicated() column by column and incrementally update the set of unique/duplicated rows. This would avoid any coercing. The benefit should be even greater for data.frame():s. My $.02 /Henrik On Thu, Mar 10, 2011 at 12:29 AM, Petr Savicky wrote: > On Wed, Mar 09, 2011 at 02:11:49PM -0500, Simon Urbanek wrote: >> match() is a red herring here -- it is really a very specific thing that has >> to do with the fact that you're running unique() on a matrix. Also it's much >> easier to reproduce: >> >> > x=c(1,1+0.2e-15) >> > x >> [1] 1 1 >> > sprintf("%a",x) >> [1] "0x1p+0" "0x1.1p+0" >> > unique(x) >> [1] 1 1 >> > sprintf("%a",unique(x)) >> [1] "0x1p+0" "0x1.1p+0" >> > unique(matrix(x,2)) >> [,1] >> [1,] 1 >> >> and this comes from the fact that unique.matrix uses string representation >> since it has to take into account all values of a row/column so it pastes >> all values into one string, but for the two numbers that is the same: >> > as.character(x) >> [1] "1" "1" > > I understand the use of match() in the original message by Terry Therneau > as an example of a situation, where the behavior of unique.matrix() becomes > visible even without looking at the last bits of the numbers. > > Let me suggest to consider the following example. > > x <- 1 + c(1.1, 1.3, 1.7, 1.9)*1e-14 > a <- cbind(rep(x, each=2), 2) > rownames(a) <- 1:nrow(a) > > The correct set of rows may be obtained using > > unique(a - 1) > > [,1] [,2] > 1 1.110223e-14 1 > 3 1.310063e-14 1 > 5 1.709743e-14 1 > 7 1.909584e-14 1 > > However, due to the use of paste(), which uses as.character(), in > unique.matrix(), we also have > > unique(a) > > [,1] [,2] > 1 1 2 > 5 1 2 > > Let me suggest to consider a transformation of the numeric columns > by rank() before the use of paste(). For example > > unique.mat <- function(a) > { > temp <- apply(a, 2, rank, ties.method="max") > temp <- apply(temp, 1, function(x) paste(x, collapse = "\r")) > a[!duplicated(temp), , drop=FALSE] > } > > unique.mat(a) > > [,1] [,2] > 1 1 2 > 3 1 2 > 5 1 2 > 7 1 2 > > Petr Savicky. > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] about textConnection
I need read a table in a string with special format. I used read.csv and textConnection function. But i am confuse about textConnection by follow code. case A: It is OK£¡ str0 <- '{"abc",{"def","X,1&Y,2&Z,3"}}' str1 <- strsplit(str0,'"')[[1]][6] str2 <- gsub("&","\n", str1) con <- textConnection( str2 ) read.csv(con,header=F) close(con) case B: It is NOK! con <- textConnection( gsub("&","\n",(strsplit('{"abc",{"def","X,1&Y,2&Z,3"}}','"')[[1]][6])) )# Error in here read.csv(con,header=F) close(con) case C: It is OK! str0 <- '{"abc",{"def","X,1&Y,2&Z,3"}}' con <- textConnection( gsub("&","\n", (strsplit(str0,'"')[[1]][6])) ) read.csv(con,header=F) close(con) case D: It is OK! str2 <- gsub("&","\n", strsplit('{"abc",{"def","X,1&Y,2&Z,3"}}','"')[[1]][6]) con <- textConnection( str2 ) read.csv(con,header=F) close(con) Except case B, textConnection report "invalid 'description' argument", in other case, textConnection is OK. I don't known, what is different£¿ I report it as [Bug 14527], But the Answer is : > your usage is incorrect. > object: character. A description of the connection. For an input this > is an R character vector object ... > and you used an expression. Some expressions work, but only simple ones > (and none are guaranteed to). I read the help carefully, but i don't known which usage is incorrect. Would you help me? WangSong [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R and ATLAS and distributing across boxes
On 9 March 2011 at 14:34, Adam D. I. Kramer wrote: | If I have a cluster of heterogenous machines, each with their own | self-optimized ATLAS, do I need to compile R on each machine to tell it to | take advantage of the local ATLAS? Or is it sufficient to compile R once | with the appropriate --with-blas and --with-lapack flags, and then trust | that, once installed on a certain box, R will look in the right place and | find and use the local library? Correct. Shared libraries, well-defined interfaces, hence plug-and-play. | Or will I need to recompile R on each box? No. If they all have libblas and liblapack somewhere were ld.so finds them you should be fine/ | Googling around has shown me how to have R use ATLAS instead of | native BLAS, but none of them note whether changing the BLAS library out | from under an R binary will change the code that R executes. Blas etc use is discussed in Appendix A.3 Linear algebra of the R Installation and Administration manual. Dirk -- Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Create an environment and assign objects to it in one go?
On 3/10/11, Henrik Bengtsson wrote: > Hi, > > I've just created: > > newEnvEval <- function(..., hash=FALSE, parent=parent.frame(), size=29L) { > envir <- new.env(hash=hash, parent=parent, size=size); > evalq(..., envir=envir); > envir; > } # newEnvEval() > > so that I can create an environment and assign objects to it in one go, e.g. > > env <- newEnvEval({ a <- 1; b <- 2; }); > print(env$a); > > Does this already exists somewhere? > > /Henrik > You can do this: e <- as.environment(list(a = 1, b = 2)) e$a; e$b or since proto objects are environments (but the $ has slightly different semantics when applied to functions): library(proto) p <- proto(a = 1, b = 2, expr = { c <- 3 }) p$a; p$b; p$c -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Testing for a reference class object
Hi all, I've constructed the following function to test whether or not an object was created from a reference class: isRefClassObject <- function(x) isS4(x) && is.environment(attr(x,'.xData')) && exists('.refClassDef',attr(x,'.xData')) but I'm unsure if it's a complete test or if there's a better way to test. Regardless, It would be nice to have such a function in the methods package. I have a case where I'd like to ensure that an object is constructed from a reference class AND that it implements a certain method: if (isRefClassObject(x) && 'run' %in% getRefClass(x)$methods()) x$run() Thanks, Jeff -- http://biostat.mc.vanderbilt.edu/JeffreyHorner __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Problem with defining new method for residuals()
Dear all, I'm writing a package and I would like to reuse the residuals() function. When I use a function which calls the redefined residuals (for my custom class) I get an error (see below). It looks like the wrong method is used. The strange this is, that when it execute the code manually it get no error. Any suggestions? Best regards, Thierry The entire source code is at svn://scm.r-forge.r-project.org/svnroot/aflp The code with the error. > normalise(dummy) Error in object$na.action : $ operator not defined for this S4 class > traceback() 5: naresid(object$na.action, object$residuals) at normalise.R#30 4: residuals.default(outliers(data)) at normalise.R#30 3: residuals(outliers(data)) at normalise.R#30 2: nrow(residuals(outliers(data))) at normalise.R#30 1: normalise(dummy) #This works fine > data <- dummy > nrow(residuals(outliers(data))) [1] 0 NAMESPACE importFrom(stats, residuals, resid, hclust, princomp) exportPattern(".") METHODS setMethod("residuals", signature(object = "AFLP.outlier"), function(object, ...){ object@Residual } ) setMethod("resid", signature(object = "AFLP.outlier"), function(object, ...){ object@Residual } ) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek team Biometrie & Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium Research Institute for Nature and Forest team Biometrics & Quality Assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 thierry.onkel...@inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Create an environment and assign objects to it in one go?
Hi, thanks to both of you. My use case was actually to in-the-end create a *list* in a cut'n'paste-friendly way, e.g. env <- function(..., hash=FALSE, parent=parent.frame(), size=29L) { envir <- new.env(hash=hash, parent=parent, size=size); evalq(..., envir=envir); envir; } # env() x <- list(); x$case1 <- env({ # Cut'n'pasted from elsewhere a <- 1; b <- 2; }); x$case2 <- env({ # Cut'n'pasted from elsewhere a <- 3; b <- 1; }); x <- lapply(x, FUN=as.list); > str(x) List of 2 $ case1:List of 2 ..$ b: num 2 ..$ a: num 1 $ case2:List of 2 ..$ b: num 1 ..$ a: num 3 /Henrik On Thu, Mar 10, 2011 at 5:53 AM, Gabor Grothendieck wrote: > On 3/10/11, Henrik Bengtsson wrote: >> Hi, >> >> I've just created: >> >> newEnvEval <- function(..., hash=FALSE, parent=parent.frame(), size=29L) { >> envir <- new.env(hash=hash, parent=parent, size=size); >> evalq(..., envir=envir); >> envir; >> } # newEnvEval() >> >> so that I can create an environment and assign objects to it in one go, e.g. >> >> env <- newEnvEval({ a <- 1; b <- 2; }); >> print(env$a); >> >> Does this already exists somewhere? >> >> /Henrik >> > > You can do this: > > e <- as.environment(list(a = 1, b = 2)) > e$a; e$b > > or since proto objects are environments (but the $ has slightly > different semantics when applied to functions): > > library(proto) > p <- proto(a = 1, b = 2, expr = { c <- 3 }) > p$a; p$b; p$c > > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Testing for a reference class object
There is a virtual class "refClass" that all reference classes subclass, so is(x, "refClass") is the natural test, as in: > foo <- setRefClass("foo", fields = "bar") > x <- foo$new() > is(x, "refClass") [1] TRUE On 3/10/11 7:40 AM, Jeffrey Horner wrote: Hi all, I've constructed the following function to test whether or not an object was created from a reference class: isRefClassObject<- function(x) isS4(x)&& is.environment(attr(x,'.xData'))&& exists('.refClassDef',attr(x,'.xData')) but I'm unsure if it's a complete test or if there's a better way to test. Regardless, It would be nice to have such a function in the methods package. I have a case where I'd like to ensure that an object is constructed from a reference class AND that it implements a certain method: if (isRefClassObject(x)&& 'run' %in% getRefClass(x)$methods()) x$run() Thanks, Jeff __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] unique.matrix issue [Was: Anomaly with unique and match]
Simon pointed out that the issue I observed was due to internal behaviour of unique.matrix. I had looked carefully at the manual pages before posting the question and this was not mentioned. Perhaps an addition could be made? Terry T. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Problem with defining new method for residuals()
On 03/10/2011 08:21 AM, ONKELINX, Thierry wrote: > Dear all, > > I'm writing a package and I would like to reuse the residuals() > function. When I use a function which calls the redefined residuals > (for my custom class) I get an error (see below). It looks like the > wrong method is used. The strange this is, that when it execute the > code manually it get no error. Hi Thierry -- I think this is a bug in the methods package. Your package promotes stats::residuals to an S4 generic. Normally this should work. However, your package Depends: on lme4, which also promotes stats::residuals to an S4 generic. Apparently, this interferes with your own attempt to make a generic, even though you do not import lme4 into your name space. As a consequence of the (putative) bug, your package sees the original stats::residuals. A work-around seems to be to importFrom(lme4, residuals) and do NOT import residuals from stats, so that your own method is associated with the generic defined in lme4. Martin > > Any suggestions? > > Best regards, > > Thierry > > The entire source code is at > svn://scm.r-forge.r-project.org/svnroot/aflp > > The code with the error. > >> normalise(dummy) > Error in object$na.action : $ operator not defined for this S4 class >> traceback() > 5: naresid(object$na.action, object$residuals) at normalise.R#30 4: > residuals.default(outliers(data)) at normalise.R#30 3: > residuals(outliers(data)) at normalise.R#30 2: > nrow(residuals(outliers(data))) at normalise.R#30 1: > normalise(dummy) #This works fine >> data <- dummy nrow(residuals(outliers(data))) > [1] 0 > > NAMESPACE importFrom(stats, residuals, resid, hclust, princomp) > exportPattern(".") > > METHODS setMethod("residuals", signature(object = "AFLP.outlier"), > function(object, ...){ object@Residual } ) > > setMethod("resid", signature(object = "AFLP.outlier"), > function(object, ...){ object@Residual } ) > > > > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek team Biometrie & > Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium > > Research Institute for Nature and Forest team Biometrics & Quality > Assurance Gaverstraat 4 9500 Geraardsbergen Belgium > > tel. + 32 54/436 185 thierry.onkel...@inbo.be www.inbo.be > > To call in the statistician after the experiment is done may be no > more than asking him to perform a post-mortem examination: he may be > able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher > > The plural of anecdote is not data. ~ Roger Brinner > > The combination of some data and an aching desire for an answer does > not ensure that a reasonable answer can be extracted from a given > body of data. ~ John Tukey > > __ R-devel@r-project.org > mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel