search for: seq_along

Displaying 20 results from an estimated 702 matches for "seq_along".

2010 Feb 25
3
behavior of seq_along
I'm trying to understand the behavior of seq_along in the following example: x <- 1:5; sum(x) y <- 6:10; sum(y) data <- c(x,y) S <- sum( data[seq_along(x)] ) S T <- sum( data[seq_along(y)] ) T Why is T != sum(y) ?
2009 Jul 09
1
bug in seq_along
Using the IRanges package from Bioconductor and somewhat recent R-2.9.1. ov = IRanges(1:3, 4:6) length(ov) # 3 seq(along = ov) # 1 2 3 as wanted seq_along(ov) # 1! I had expected that the last line would yield 1:3. My guess is that somehow seq_along don't utilize that ov is an S4 class with a length method. The last line of the *Details* section of ?seq has a typeo. Currently it is 'seq.int', 'seq_along' and 'seq...
2007 Apr 03
1
Behavior of seq_along (was: Create a new var reflecting the order of subjects in existing var)
...el. Based on offline communications with Jim, suppose dat is defined as follows: set.seed(123) dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4), rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1)) # Then this ave call works as expected: ave(dat$ID, dat$ID, FUN = function(x) seq_along(x)) # but this apparently identical calculation gives an error: ave(dat$ID, dat$ID, FUN = seq_along) The only difference between the two calls is that the first one uses seq_along and the second uses function(x) seq_along(x) in its place. Does anyone know why the second gives an error? Is this...
2012 Jan 06
1
seq_along and rep_along
Hi all, A couple of ideas for improving seq_along: * It would be really useful to have a second argument dim: seq_along(mtcars, 1) seq_along(mtcars, 2) # equivalent to seq_len(dim(mtcars)[1]) seq_len(dim(mtcars)[2]) I often find myself wanting to iterate over the rows or column of a data frame, and there isn't a partic...
2019 May 16
3
print.<strorageMode>() not called when autoprinting
...are not called when there is no explicit class attribute. E.g., % R-3.6.0 --vanilla --quiet > print.function <- function(x, ...) { cat("Function with argument list "); cat(sep="\n ", head(deparse(args(x)), -1)); invisible(x) } > f <- function(x, ...) { sum( x * seq_along(x) ) } > f function(x, ...) { sum( x * seq_along(x) ) } > print(f) Function with argument list function (x, ...) Previous to R-3.6.0 autoprinting did call such methods % R-3.5.3 --vanilla --quiet > print.function <- function(x, ...) { cat("Function with argument list "); cat(...
2012 Nov 30
1
xts indexed with Date class
...exed on class Date in the latest versions, versus 2. It seems to be related to changes to/from daylight savings time, happens those weekends. Is it not intended that class Date be used like this, or is this new behaviour incorrect? Giles Example: > a<-as.Date(15423:15426) > x<-xts(seq_along(a),a) > print(x) [,1] 2012-03-24 1 2012-03-25 2 2012-03-25 3 2012-03-26 4 > print(index(x)) [1] "2012-03-24" "2012-03-25" "2012-03-25" "2012-03-26" > print(as.numeric(index(x))) [1] 15423 15424 15424 15425 #for reference, zoo...
2009 Nov 22
2
Help with indexing
Dear R Helpers, I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help. To use an example from an earlier post on this list, I have a dataframe of this kind: dat = data.frame(name = rep(c("Mary", "Sam", "John"), c(3,2,4))) dat$freq = ave(seq_along(dat$name), dat$name, FUN = seq_alo...
2012 Nov 20
2
correct function formation in R
...''data.frame'' and then transform it as output? (fun1 and fun1 is just for illustration). ? Thanks a lot, OV ? code: input <- data.frame(x1 = rnorm(20), x2 = rnorm(20), x3 = rnorm(20)) fun1 <- function(x) { ??? ID <- NULL; minimum <- NULL; maximum <- NULL ??? for(i in seq_along(names(x)))?? { ??????? ID[i]?????? <- names(x)[i] ????????? minimum[i]? <- min(x[, names(x)[i]]) ??????????? maximum[i]? <- max(x[, names(x)[i]]) ??????????????????????????????????? } ??? output <- structure(list(ID, minimum, maximum), row.names = seq_along(names(x)), .Names = c("I...
2010 Sep 21
2
lapply version with [ subseting - a suggestion
Dear R developers, Reviewing my code, I have realized that about 80% of the time in the lapply I need to access the names of the objects inside the loop. In such cases I iterate over indexes or names: lapply(names(x), ... [i]), lapply(seq_along(x), ... x[[i]] ... names(x)[i] ), or for(i in seq_along(x)) ... which is rather inconvenient. How about an argument to lapply which would specify the [ or [[ subseting to use in the splitting of the vector? Or may be a different set of functions lapply1, sapply1? I believe this pattern is rathe...
2012 Dec 23
1
correction needed in codes
...ear useRs,while trying to plot the yearly curves of 1000 stations and overlapping each set of curves with mean curve and then saving it automatically in a pdf file, i tried the following commands >Path = "C:\\R\\003.pdf">pdf(file=Path) for (i in seq(1:1000) >a<-lapply(seq_along(tcp), function(x) tcp[[x]][,-1]) >b<-lapply(seq_along(a), function(a) matrix(rowMeans(tcp[[a]]),ncol=1)) >lapply(seq_along(tcp), function(i) (matplot(tcp[[i]][,-1], type="l",col="grey") ) >lines(b, lwd=2, type="l")) } >dev.off()although i was suc...
2019 May 21
2
print.<strorageMode>() not called when autoprinting
...., > > > % R-3.6.0 --vanilla --quiet > >> print.function <- function(x, ...) { cat("Function with argument > list "); > > cat(sep="\n ", head(deparse(args(x)), -1)); invisible(x) } > >> f <- function(x, ...) { sum( x * seq_along(x) ) } > >> f > > function(x, ...) { sum( x * seq_along(x) ) } > >> print(f) > > Function with argument list function (x, ...) > > > Previous to R-3.6.0 autoprinting did call such methods > > % R-3.5.3 --vanilla --quiet > &...
2012 Dec 24
2
whats wrong in my codes???
.... the name of each plot is contained in "names" file. when i run this loop, i get an error. "Error in plot.new() : Unable to open file 'C:/R/SAVEHERE/myplot_Tak.jpg' for writing". could you please correct the mistake in the loop? >names<-(names(sp)) >for(a in seq_along(names)){ >mypath <- file.path("C:","R","SAVEHERE",paste("myplot_", names[a], ".jpg",sep = "")) >jpeg(file=mypath) >for (i in seq(1)){ >b<-lapply(res,function(x) {if(is.data.frame(x[,-1])) rowMeans(x[,-1]) else mean(x[,-1]...
2008 Jul 11
1
Suggestion: 20% speed up of which() with two-character mod
Hi, by replacing 'll' with 'wh' in the source code for base::which() one gets ~20% speed up for *named logical vectors*. CURRENT CODE: which <- function(x, arr.ind = FALSE) { if(!is.logical(x)) stop("argument to 'which' is not logical") wh <- seq_along(x)[ll <- x & !is.na(x)] m <- length(wh) dl <- dim(x) if (is.null(dl) || !arr.ind) { names(wh) <- names(x)[ll] } ... wh; } SUGGESTED CODE: (Remove 'll' and use 'wh') which2 <- function(x, arr.ind = FALSE) { if(!is.logical(x))...
2009 Aug 23
2
difficult "for"
Hi, My english isn't brilliant and my problem is very difficult to describe but I try ;) My first question is: May I write loop "for" like this or similar - for (i in sth : sth[length(sth)], k in sth_else : length(sth_else) ) - I'd like to have two independent conditions in the same loop "for". My secound question depend on program below. I'd like to write every
2008 Jul 01
1
[.data.frame speedup
...t;- pmatch(i, rows, duplicates.ok = TRUE) } xj <- .subset2(.subset(xx, j), 1L) return(if (length(dim(xj)) != 2L) xj[i] else xj[i, , drop = FALSE]) } if (any(is.na(cols))) stop("undefined columns selected") nxx <- structure(seq_along(xx), names = names(xx)) sxx <- match(nxx[j], seq_along(xx)) } else sxx <- seq_along(x) rows <- NULL if (is.character(i)) { rows <- attr(xx, "row.names") i <- pmatch(i, rows, duplicates.ok = TRUE) } for (j in seq_along(x)) { xj <- xx[[sxx[j]]]...
2009 Jul 29
1
Systematic resampling (in sequential Monte Carlo)
...wing is a piece of R code that implements the procedure. > ### Systematic resampling > set.seed(2) > x <- LETTERS[1 : 6] # labels > w <- rexp(6) > w <- w / sum(w) # probabilities > W <- c(0, cumsum(w)) # cdf > u <- (1 : 6 + runif(1)) / 6 > indNew <- sapply(seq_along(u), + function(i) (sum(W[i] <= u & u < W[i + 1]))) > (y <- rep(x, indNew)) [1] "A" "B" "D" "D" "F" > ## graphically... > plot(stepfun(seq_along(u), W), xaxt = "n") > axis(1, at = seq_along(u), x) &...
2009 Sep 19
1
matrix operations on grobs and grid units
...which are arranged into a matrix layout. Below is my current version, followed by a few questions. e = expression(alpha,"testing very large width", hat(beta), integral(f(x)*dx, a, b)) rowMax.units <- function(u, nrow){ # rowMax with a fake matrix of units matrix.indices <- matrix(seq_along(u), nrow=nrow) do.call(unit.c, lapply(seq(1, nrow), function(ii) { max(u[matrix.indices[ii, ]]) })) } colMax.units <- function(u, ncol){ # colMax with a fake matrix of units matrix.indices <- matrix(seq_along(u), ncol=ncol) do.call(unit.c, lapply(seq(1, ncol), function(ii) { max(u[ma...
2013 Jan 27
1
bug and enhancement to split?
...lit.default(1:5, 1:2) : data length is not a multiple of split variable > x = structure(1:5, class="A") > split(x, 1:2) $`1` [1] 1 $`2` [1] 2 Also, this is inconsistent with split<-, which does have recycling > split(x, 1:2) <- 1:2 Warning message: In split.default(seq_along(x), f, drop = drop, ...) : data length is not a multiple of split variable > x [1] 1 2 1 2 1 attr(,"class") [1] "A" A solution is to change a call to seq_along(f) toward the end of split.default to seq_along(x). @@ -32,7 +32,7 @@ lf <- levels(f) y <-...
2006 Nov 18
2
overhead of function calls
...he function into a single one. I tried to test this by > f <- function(x) 1+x > g <- function(x) f(x) > x <- rnorm(1e6) > system.time(sapply(x,f)) [1] 11.315 0.157 11.735 0.000 0.000 > system.time(sapply(x,g)) [1] 8.850 0.140 9.283 0.000 0.000 > system.time(for (i in seq_along(x)) f(x[i])) [1] 2.466 0.036 2.884 0.000 0.000 > system.time(for (i in seq_along(x)) g(x[i])) [1] 3.548 0.045 4.165 0.000 0.000 but I find that hard to interpret -- the overhead looks significant in the first case, but something strange (at least to my limited knowledge) is happening with sappl...
2012 Dec 03
4
How to calculate the spatial correlation of several files?
...each file in both directories is a vector. I am not sure how to tell R to correlate the first column in dir1 to the correspond column from dir2. we will finally get only one spatial correlation map. I tried to this: # calculate the correlation so we will get a correlation map for (.files in seq_along(dir1)){ results[[length(results) + 1L]]<- cor(file1 ,file2) } I got error:Error in cor(file1, file2) : allocMatrix: too many elements specified` -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-f...