similar to: avoid a loop

Displaying 20 results from an estimated 30000 matches similar to: "avoid a loop"

2008 Oct 29
2
definition of a function
Hello, R subscribers. I would like to define a function like this one: Fun <- function(x) sin(x)/x with value 1 at x=0. How can I do that? Is there a way to plot it "symbolically", without using x as a vector? e.g. x <- seq(from=-10, to=10, length=100) plot(x, Fun(x)) Thank you in advance.
2010 Sep 11
2
Latex fonts in R graphics
Hello, R users. I am trying to embed Computer modern fonts to an R plot and I get the following error. CM <- Type1Font("CM", + c(paste("cm-lgc/fonts/afm/public/cm-lgc/", + c("fcmr8a.afm", "fcmb8a.afm", "fcmri8a.afm", "fcmbi8a.afm"), sep=""), + "./cmsyase.afm")) > pdf("cm.pdf",
2009 May 07
2
for loop vectorization
Hi, I am still not familiar with vectorization. Could you help with making this for loop more efficient? The code is trying to make a Q matrix for a multidimensional state space with specific conditions. thanks Mira tmp = 0:(maxvals[1]) for(i in 2:nchars) { tmp <- outer(tmp, 0:(maxvals[i]), FUN="paste", sep=".") } states = tmp stateidx = array(1:length(states),
2013 Oct 04
3
Trying to avoid nested loop
Dear R users. I'm trying to avoid using nested loops in the following code but I'm not sure how to proceed. Any help would be greatly appreciated. With regards,Phil X = matrix(rnorm(100), 10, 10) ## Version with nested loopsresult = 0 for(m in 1:nrow(X)){ for(n in 1:ncol(X)){ if(X[m,n] != 0){ result = result + (X[m,n] / (1 + abs(m - n))) } }} ## No loop-sum(ifelse(M
2001 Apr 06
2
bug in contour() ? (PR#897)
The following example just "hangs", i.e. the final contour() does not terminate. x <- pi*(0.5+((-13):12))/36 w <- 0.377*cos(2*x)*sqrt(abs(cos(2*x))) v <- c(rep(-1.47,13),rep(1.722,13))*abs(sin(x))^1.5 cr <- outer(w,w)+outer(v,v) s <- sqrt(diag(cr)) cr <- cr/outer(s,s) contour(cr[1:26,26:1],levels=seq(-1,1,0.1),axes=T) --please do not edit the information below--
2007 Aug 31
2
memory.size help
I keep getting the 'memory.size' error message when I run a program I have been writing. It always it cannot allocate a vector of a certain size. I believe the error comes in the code fragement below where I have multiple arrays that could be taking up space. Does anyone know a good way around this? w1 <- outer(xk$xk1, data[,x1], function(y,z) abs(z-y)) w2 <- outer(xk$xk2,
2013 Aug 23
2
Re: virsh "cpu-stats" not working !!!
Hi Eric, Thanks for your valuable time and comments. >> Are you sure cpuacct was mounted prior to libvirtd starting? Yes, we are mounting cpuacct before starting the libvirtd daemon. >> Libvirt won't mount cgroups; Yes, Agree. >>it only uses what was mounted at the time it was started. But we are mounting it well before it is started. We are facing the issue only while
2010 Sep 15
2
Programming: loop versus vector oriented
Dear all, I am new to R and to it's programming philosophy. The following function is supposed to work on a vector, but I can't figure out how to do that without looping through every element of it. Is there a more elegant way? Note: I have shortened it, so it is NOT correct from the pipe hydraulics point of view # Calculate wall friction factor # reynolds: Reynolds number # dk:
2007 Aug 28
1
alternate methods to perform a calculation
Consider a data frame (x) with 2 variables, x1 and x2, having equal values. It looks like: x1 x2 1 1 2 2 3 3 Now, consider a second data frame (xk): xk1 xk2 0.5 0.5 1.0 0.5 1.5 0.5 2.0 0.5 0.5 1 1.0 1 1.5 1 2.0 1 0.5 1.5 1.0 1.5 1.5 1.5 2.0 1.5 0.5 2 1.0 2 1.5 2 2.0 2 I have written code to calculate some differences between these
2011 Dec 04
4
equating approximate values
Hello List, I am having trouble finding the command for my problem. I have two arrays x and y. now i would like to compare the values of x and y and then get the index of x which is exactly or approximately equal(+/- some value ) to the values in y. x <- runif(100,min=0,max=5) y <- runif(10,min=0,max=5) the threshold value(+/-) value can vary. for this example lets take it to be .5
2008 Jul 29
3
finding a faster way to do an iterative computation
useR's, I am trying trying to find out if there is a faster way to do a certain computation. I have successfully used FOR loops and the apply function to do this, but it can take some time to fully compute, but I was wondering if anyone may know of a different function or way to do this: > x [1] 1 2 3 4 5 > xk [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 I want to do:
2003 Apr 24
5
Fast R implementation of Gini mean difference
I have written the following function to calculate the weighted mean difference for univariate data (see http://www.xycoon.com/gini_mean_difference.htm for a related formula). Unsurprisingly, the function is slow (compared to sd or mad) for long vectors. I wonder if there's a way to make the function faster, short of creating an external C function. Thanks very much for your advice. gmd
2004 May 13
2
watermarks
Hi, I would like to plot a graph which sits in the background as a watermark with other plots in the foreground - on top. I have looked through the threads on the r-project website but they seem to concern background colours rather than actual background plots. I have also searched through the demo's and pars but can't find any eg's any ideas - I only want to plot a simple graph
2005 Dec 13
3
help with writing function
I'm trying to write a function that takes a vector of length n and then takes the first value of the vector i.e j=1 and forms a new vector of length n (i.e replicate the first value n times). This function will then calculate the absoulte difference of the original vector and the new vector and store the results omitting the difference between the value and itself. This function should
2011 Jul 02
5
%dopar% parallel processing experiment
dear R experts--- I am experimenting with multicore processing, so far with pretty disappointing results. Here is my simple example: A <- 100000 randvalues <- abs(rnorm(A)) minfn <- function( x, i ) { log(abs(x))+x^3+i/A+randvalues[i] } ?## an arbitrary function ARGV <- commandArgs(trailingOnly=TRUE) if (ARGV[1] == "do-onecore") { ?library(foreach) ?discard <-
2008 Jan 23
1
newbie:looking for an efficient way to compute distance vector
dear experts, I am new to R and am trying to compute a vector y from a vector x where : y[i] = sign(x[j]-x[i])*(j-i) with j the first index after i where abs(x[j]-x[i]) > to a given step y[i] is 0 if there is no such j I can write this in R as follows for(i in 1:length(x)) { y[i]=0 for(j in i:length(x)) { if (abs(x[j]-x[i]) > step) { y[i]=sign(x[j]-x[i])*(j-i)
2008 Aug 31
1
how to draw a perspective pyramid
Dear list, I am trying to construct a perspective plot of a 2D triangle, or pyramid. I can easily do the one dimensional plot (code below), but I can't figure out how to do it in a perspective plot for two input variables. Does anyone have a simple solution? Thanks! Derek #code for a triangle u <- seq(-1,1,.001) tri <- (1-abs(u)) plot(tri~u,type="l",main="Second
2009 Nov 18
1
bug in '...' of constrOptim (PR#14071)
Dear all, There appears to be a bug in how constrOptim handles ... arguments that are suppose to be passed to optim, according to the documentation. This means you can't get the hessian to be returned, for example (so this is a real problem, and not just a question of mistaken documentation). Looking at the code, it appears that a call to the user-defined f includes the ..., when the ...
2009 Jul 07
2
Tex fonts in R plots
Hello, R users. I would like to display the font of Math Mode of MikTex 2.3, WinEdt 5.4 in R plots, e.g. in xlab, ylab or legend. How can I do that? Thank you in advance.
2007 Apr 17
1
no visible binding for global variable
Hello everyone I am trying to get one of my packages through R's QC. The package is clean for me under R-2.4.1, R-2.5.0, and R-devel, but Kurt gets > > * checking R code for possible problems ... WARNING > hypercube: no visible binding for global variable ?f? Function hypercube() [cut-&-pasted below] is intended to return an adjacency matrix for an n-dimensional