similar to: vectorize an expression

Displaying 20 results from an estimated 9000 matches similar to: "vectorize an expression"

2004 Nov 15
2
argument rationalization
Hi I am writing a bunch of functions that take two, three or four arguments. These functions operate on vectors of the same length; but I want the function to behave sensibly if one or more arguments are scalars. "+" does this for two arguments: "+"(1:10,3) # interpreted as "+"(1:10,rep(3,10)) But my functions can take more arguments. Say f() takes three:
2004 Jun 11
4
rownames of single row matrices
Hi I want to extract rows of a matrix, and preserve rownames even if only one row is selected. Toy example: R> a <- matrix(1:9,3,3) R> rownames(a) <- letters[1:3] R> colnames(a) <- LETTERS[1:3] R> a A B C a 1 4 7 b 2 5 8 c 3 6 9 Extract the first two rows: R> wanted <- 1:2 R> a[wanted,] A B C a 1 4 7 b 2 5 8 rownames come through fine. Now extract just
2004 Sep 08
3
do.call("[", ...) question
Hi again everyone I have an arbitrarily dimensional array "a" and a list "jj" of length length(dim(a)). The elements of jj are vectors of indexes. How do I use do.call() to extract a[ jj[[1]], jj[[2]], jj[[3]], ...] ? Toy example follows: a <- matrix(1:30,5,6) jj <- list(5:1,6:1) I want the following a[ jj[[1]],jj[[2]] ] How do I do this? OBAttempts:
2004 Oct 18
2
concatenating lists elementwise
Hi How do I concatenate two lists element-by-element? Example: list.1 <- list(temperature=c("hot","cold") , size=c("big","medium")) list.2 <- list(temperature=c("lukewarm") , size=c("massive","tiny")) list.wanted <- list(temperature=c("hot","cold","lukewarm") ,
2004 Jan 14
1
arrows on contour lines
Hello everybody I'm using contour() to draw streamlines of potential flow, eg jj <- seq(from= -4, to=4,len=20) jj <- outer(jj,jj,function(x,y){x})+1i*outer(jj,jj,function(x,y){y}) f <- function(x){x^2} contour(Im(f(jj)), nlevels=44 , labels="") How best to put arrows on the contour lines to show the direction of flow? (ie I want contour lines looking like
2004 Nov 11
1
axis lines crossing at origin
Hi how do I make my axes cross at the origin? x <- seq(from=-pi,to=pi,len=30) plot(x,sin(x)) makes the axes cross at about (-pi,-1). How do I get my x and y axes to cross in the centre of the graph, with the sine curve passing through the intersection? I couldn't find anything in ?par or ?axis; searching R-FAQ for "axis" didn't help. -- Robin Hankin Uncertainty
2004 Sep 13
1
Rd files with "%" (was: permuting dimensions)
Professor Ripley thanks for this. Very much appreciated. The original subject line reflected my late-night conviction that the answer might involve passing a strange list to do.call(). Anyway, package magic is broken (only in R-devel, I might add) because I have a function called "%eq%". R-2.0.0 CMD check is stopping (I think) because it interprets the "%" as a
2004 Mar 10
3
aperm() and as.list() args to "["
Hi everyone. I'm playing with aperm(): a <- 1:24 dim(a) <- c(2,3,2,2) permutation <- c(1,2,4,3) b <- aperm(a,permutation) So if my understanding is right, a[1,3,2,1] == b[c(1,3,2,1)[permutation] ] but this isn't what I want because the RHS evaluates to a vector, and I am trying to identify a single element of b. How do I modify the RHS to give what I want? Following
2004 May 27
2
block diagonal matrix function
Hello List I have just written a little function that takes two matrices as arguments and returns a large matrix that is composed of the two input matrices in upper-left position and lower-right position with a padding value everywhere else. (function definition and toy example below). I need nonsquare matrices and rowname() and colname() inherited appropriately. Two questions: (1) Is there a
2004 Nov 18
0
Fwd: Re: 3d scatter plot with drop line
Hi try this: p3dpairs <- function(x,x1, xlim=NULL,ylim=NULL,zlim=NULL,col=par("col"), pch=par("pch"), cex=par("cex"), ...){ if(is.matrix(x)){ z <- x[,3] y <- x[,2] x <- x[,1] } if(is.matrix(x1)){ z1 <- x1[,3] y1 <- x1[,2] x1 <- x1[,1] } if(missing(zlim)) { z.grid <-
2004 Mar 12
1
another do.call() problem.
Hi everyone suppose I have a <- array(1:256,rep(4,4)) and want to access a[1,2,3,1] by the vector c(1,2,3,1). As per yesterday, I can use do.call(): a[1,2,3,1] == do.call("[",c(list(a),c(1,2,3,1))) Now how do I apply the above technique (or indeed any other technique!) to get a[1,2,3,] [1] 37 101 165 229 from a vector like c(1,2,3,0) or c(1,2,3,NULL) or c(1,2,3,NA)?
2004 Jul 05
1
"make" error for R-1.9.1
Hello everybody. I am trying to upgrade from R-1.9.0 to R-1.9.1 on a RedHat linux 2.4.18 system. I get the following error after "tar -xvzf R-1.9.1.tgz ; cd ./R-1.9.1/ ; ./configure" and "make" : [make works for 10 minutes ... snip ...] varExp text html latex varFixed text html latex varFunc
2004 Oct 06
3
crossprod vs %*% timing
Hi the manpage says that crossprod(x,y) is formally equivalent to, but faster than, the call 't(x) %*% y'. I have a vector 'a' and a matrix 'A', and need to evaluate 't(a) %*% A %*% a' many many times, and performance is becoming crucial. With f1 <- function(a,X){ ignore <- t(a) %*% X %*% a } f2 <- function(a,X){ ignore <-
2004 Jul 27
1
library manual: documentation of funcs not alphabetically ordered
Hello everybody I'm putting finishing touches to a library, and have noticed that the .dvi file that R CMD check creates does not sort the functions in alphabetical order. I find this odd because I used the "tidy" routines in section 3.1 of the R-exts manual, which produce R code in which the functions are alphabetically ordered (and I performed R CMD check using the
2004 Sep 07
2
noncommutative addition: NA+NaN != NaN+NA
Hi guys. Check this out: > NaN +NA [1] NaN > NA + NaN [1] NA I thought "+" was commutative by definition. What's going on? > R.version _ platform powerpc-apple-darwin6.8 arch powerpc os darwin6.8 system powerpc, darwin6.8 status major 1 minor 9.0 year 2004 month 04 day 12 language R > (Both give NA under linux, so it looks
2004 Nov 08
1
conv() example in R-exts
Hi [I'm not sure if this is "intelligible to non-programmers" or not] R-exts (section 4.2) gives an example of the .C() function whose third argument is "as.integer(length(a))", and urges the user to coerce all the arguments to the correct form (on pain of "hard-to-catch errors" which I now know to be very appropriate, if understated, phrasing). The
2004 Sep 13
1
do.call("dim<-" , ... )
OK guys another problem. I have a 3D array "x" with dim(x)=c(a,a,b^2) and I want to rearrange the elements of x to make a matrix "y" with dimensions c(a*b,a*b). Neither a nor b is known in advance. I want the "n-th" a*a submatrix of y to be x[,,n] (where 1 <= n <= b^2). Needless to say, this has gotta be vectorized! Toy example with a=2, b=3 follows:
2004 Mar 19
5
asp=1 and aspect ratio
Hi everyone I want a square scatterplot with abline(0,1) going exactly through the SW and NE corners. By "square" I mean that the plotting region is exactly square, and that the axis limits are identical. x <- 1:20 y <- x+rep(c(-1,1),10) lims <- range(c(x,y)) None of the following do this: plot(x,y) ; abline(0,1) #not square plot(x,y,asp=1);abline(0,1) #diagonal
2004 Nov 29
1
data() in data/*.R files
Hi I'm having difficulty making a package pass R CMD check. I need to read in a dataset from another package, modify it, and have the modified object available in the first package. help(require) says: The source code for a package that requires one or more other packages should have a call to 'require', preferably near the beginning of the source, and of course
2004 Oct 26
1
persp(), scatterplot3d(), "..." argument
Hello list. I very often need 3d scatterplots, and use scatterplot3D quite a lot. I am trying to modify persp() to plot scatterplots, and make use of the theta and phi arguments that persp() offers. I am having some difficulty passing the correct arguments to persp(). Here is my function so far. Much of it is copied from the persp() manpage. points3d <- function(x,y,z,