similar to: Avoiding FOR loops

Displaying 20 results from an estimated 2000 matches similar to: "Avoiding FOR loops"

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:
2008 Jan 08
1
using lapply()
useR's, I am trying to find a quick way to change some values in a list that are subject to a condition to be NA. Consider the 3x1 matrix: delta <- matrix(c(2.5,2.5,1), nrow = 1) And consider the list named v that has 3 elements > v v[[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 4.25 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.25
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
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,
2001 Nov 21
2
distances from points to line
Dear all, I have discovered that there are many things that I used to do in my GIS which are easily done directly in R, for example calculating interpoint distances using geoR and pick out points inside a polygon using splancs. I now wonder, is there a function to create a line object like a watercourse and then calculate the distances between many points in space and this line? I couldn't
2007 Aug 29
0
a faster and shorter way to perform calculations?
This is a continuation from a previous posting of mine: The following algorithm below is what I want to accomplish: Z(xk) = Average(Yi, i belongs to Ik), where Ik contains all i such that for each j, |Xi,j - xkj?? 2. Here, j = 1, 2 and i corresponds to the elements in each X and/or xk >data x1 x2 y 1 1 2 2 2 6 3 3 12 Now, consider a second data frame or matrix (xk):
2008 Oct 03
1
NA's in segmented
I am trying to fit a very simple broken stick model using the package "segmented" but I have hit a roadblock. > str(data) 'data.frame': 18 obs. of 2 variables: $ Bin : num 0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 ... $ LnFREQ: num 5.06 4.23 3.50 3.47 2.83 ... I fit the lm easily: > fit.lm<-lm(LnFREQ~Bin, data=id07) But I keep getting an error
2007 Dec 08
0
help for segmented package
Hi, I am trying to find m breakpoints of a linear regression model. I used the segmented package. It works fine for small number of predicators and breakpoints.(3 r.v. 3 points). However, my model has 14 variables it even would not work even for just one breakpoints!. The error message is always estimated breakpoints are out of range. Since my problem is time related problem. So I
2006 Aug 11
1
x tick labels - sparse?
Hi, I'm stuck on creating a plot with x tick labels for every Nth tick mark - how is that done? I don't see a simple solution to this in help(plot) or help(par) and what I've tried is not working, eg, the following does not work, although it seems intuitive to me that it should work: x <- seq(-100,1000,25) y <- x * x % find all the x values that are multiples of 100 tmp <-
2009 Aug 13
1
R code to reproduce (while studying) Bates & Watts 1988
Hi R users, I'm here trying to understand correlated residuals in nonlinear estimation. I'm reading/studying the book Bates, D. M. and D. G. Watts, (1988), /Nonlinear regression analysis and its applications/, Wiley, NY. pages 92-94, trying to reproduce the figures and to find out the code in R to perform the necessary calculations. I also consulted Pinheiro and Bates, but without
2008 Feb 21
0
extending code to handle more variables
useR's, Consider the variables defined below: yvals <- c(25,30,35) x1 <- c(1,2,3) x2 <- c(3,4,5) x3 <- c(6,7,8) x <- as.data.frame(cbind(x1,x2,x3)) delta <- c(2.5, 1.5, 0.5) h <- delta/2 vars <- 3 xk1 <- seq(min(x1)-0.5, max(x1)+0.5, 0.5) xk2 <- seq(min(x2)-0.5, max(x2)+0.5, 0.5) xk3 <- seq(min(x3)-0.5, max(x3)+0.5, 0.5) xks <- list(xk1,xk2,xk3) xk <-
1997 Aug 21
1
R-alpha: another ctest question
I have the following problem. Consider a `classical' test which works for k .ge. 2 samples. Possible interfaces are e.g. xxx.test(x, g) x ... all data, g ... corresponding groups xxx.test(x1, ..., xk) xxx.test(list(x1, ..., xk)) etc etc. Clearly, the first and the second one are nice, but cannot be combined without making `g' (i.e., `group') a named argument. Hence, in
2003 Oct 28
2
outer function problems
I'm pulling my hair (and there's not much left!) on this one. Basically I'm not getting the same result t when I "step" through the program and evaluate each element separately than when I use the outer() function in the FindLikelihood() function below. Here's the functions: Dk<- function(xk,A,B) { n0 *(A*exp(-0.5*(xk/w)^2) + B) } FindLikelihood <-
2009 Jun 18
1
recovering the name of a given value in a matrix
Hi all, I've got object *cumsumtab, *where the first row (1,2,3,4,5,6,8,13,14,17,40) are names (actually, categories), and the second row has numeric values. > cumsumtab 1 2 3 4 5 6 8 13 14 17 40 2 4 6 7 9 11 13 14 15 16 17 I'm trying to find a way of finding to which category ( in *names(cumsumtab) * ) belongs a given value (let's say Q1 <- 4.25). So far, I've
2009 Nov 04
4
unexpected results in comparison (x == y)
Dear readers of the list, I have a problem a comparison of two data from a vector. The comparison yields FALSE but should be TRUE. I have checked for mode(), length() and attributes(). See the following code (R2.10.0): ----------------------------------------------- # data vector of 66 double data X =
2010 Aug 30
1
Help With Post-hoc Testing
I am trying to do post-hoc tests associated with a repeated measures analysis with on factor nested within respondents. The factor (SOI) has 17 levels. The overall testing is working fine, but I can''t seem to get the multiple comparisons to work. The first step is to "stack" the data. Then I used "lme" to specify and test the overall model. Finally
2007 Nov 19
2
All nonnegative integer solution
Dear all, Is there any method in R to find all possible nonnegative integer solutions to the linear equation with unit coefficients as follow: X1+X2+...+Xk=N Thank you, Amin Zollanvari
2009 Jan 19
2
Using apply to generate matrix from rows?
Dear all, I have a simple question which I unfortunately do not seem to be able to solve myself. I have a (NxK) matrix and want to generate a new matrix by multiplying each row with itself such that the new matrix has dimension ((N*K)xK) (or better, generate an array with dimension (K,K,N)). I tried apply, but that did not work. Any suggestions? Thanks! Stephan ## Here is a simple
2007 Sep 10
5
finding the minimum positive value of some data
useRs, I am looking to find the minimum positive value of some data I have. Currently, I am able to find the minimum of data after I apply some other functions to it: > x [1] 1 0 1 2 3 3 4 5 5 5 6 7 8 8 9 9 10 10 > sort(x) [1] 0 1 1 2 3 3 4 5 5 5 6 7 8 8 9 9 10 10 > diff(sort(x)) [1] 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 0 > min(diff(sort(x))) [1] 0
2010 Nov 14
1
score test for logistic regression
Dear R experts, I'm trying to find a code to calculate the p-value from the score test for the logistic regression. My fit is like this: logit=beta0+beta1*x1+beta2*x2 +....+ betak* xk. And my H0 is beta1=beta2=...=betak =0. Any help will be highly appreciated. Thank you! Ying