Martin Maechler
1997-Nov-10 16:00 UTC
R-alpha: Re: R/R-minus incompatibility [ an old one ]
>>>>> "FrL" == Friedrich Leisch <Friedrich.Leisch@ci.tuwien.ac.at> writes:FrL> Hmm, I'm quite perplexed by something very trivial/stupid/whatever FrL> ... don't know, if this one has been reported before: R> x<-1 x[1:2] FrL> Error: subscript out of bounds Splus> x<-1 x[1:2] FrL> [1] 1 NA FrL> Or is there an option to control this behaviour? Fritz, this *is* known and is on purpose. However, it seems that it is NOT in the R-FAQ (Kurt ?!?) -- Martin --------------- Here are two example mails from R-devel ------------------ From: Peter Dalgaard BSA <p.dalgaard@kubism.ku.dk> Date: 25 Apr 1997 01:36:23 +0200 To: r-devel@stat.math.ethz.ch Subject: R-alpha: list assignment This works in Splus: > x<-list() > x[["f"]]<-1 > zz<-"g" > x[[zz]]<-2 > x $f: [1] 1 $g: [1] 2 In R both variants fail unless the name is already on the list. The first one can be replaced by x$f, but there's seems to be no substitute for the other one (oh yes I found one, but it's not fit to print!). This comes up if you e.g. want to create a variable in a data frame with a name given by a character string. ------------------------------------------------------ From: Robert Gentleman <rgentlem@stat.auckland.ac.nz> Date: Fri, 25 Apr 1997 13:12:36 +1200 (NZST) To: r-devel@stat.math.ethz.ch Subject: Re: R-alpha: list assignment Peter, They don't work on purpose. Basically this is the same problem as indexing beyond the end of an array. In almost all cases it is an error. To add a variable with a specific name to a data frame how about x (the dataframe) var1 the variable name1 the name x<-cbind(x,var1) names(x)[ncol(x)]<-name1 robert =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>> x[["f"]]<-1>> zz<-"g" >In R both variants fail unless the name is already on the list. The >first one can be replaced by x$f, but there's seems to be no >substitute for the other one (oh yes I found one, but it's not fit to A long time ago it seems it was decided to remain incompatible with S on this, I think because people often do this in error. (Indeed, it did point out some errors in my code.) However, there were some instances in my code where I did something like x[[c("f","g","h")]] <- 1:3 and wanted the corresponding elements updated or inserted as appropriate. I have replaced those instances with listadd(x, c("f","g","h")) <- 1:3 where listadd is as defined below (more or less as suggested by Ross or Robert at the time). It is one of the functions in the compatibility code I have circulated previously. Paul Gilbert ________ "list.add<-" <- function(x, replace, value) {# replace or add elements to a list. if (is.numeric(replace)) {x[[replace]] <- value return(x) } if (is.null(value)) value <- list(NULL) if (!is.list(value)) value <- list(value) if (1 == length(value)) {for (i in seq(length(replace))) x<- do.call("$<-", list(x,replace[i],value[[1]])) } else {if(length(value) != length(replace) ) stop("number of replacement values != number of elements to replace") for (i in seq(length(replace))) x<- do.call("$<-", list(x,replace[i],value[[i]])) } x } =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=