similar to: avoiding loop

Displaying 20 results from an estimated 10000 matches similar to: "avoiding loop"

2009 Oct 30
2
Efficient way to code using optim()
Hi all, I am trying to estimate a simple logit model. By using MLE, I am maximizing the log likelihood, with optim(). The thing is, each observation has different set of choice options, so I need a loop inside the objective function, which I think slows down the optimization process. The data is constructed so that each row represent the characteristics for one alternative, and CS is a
2011 Aug 29
2
splitting into multiple dataframes and then create a loop to work
Dear All Sorry for this simple question, I could not solve it by spending days. My data looks like this: # data set.seed(1234) clvar <- c( rep(1, 10), rep(2, 10), rep(3, 10), rep(4, 10)) # I have 100 level for this factor var; yvar <- rnorm(40, 10,6); var1 <- rnorm(40, 10,4); var2 <- rnorm(40, 10,4); var3 <- rnorm(40, 5, 2); var4 <- rnorm(40, 10, 3); var5 <- rnorm(40, 15,
2010 Feb 23
1
function on all pairs of vector entries
Hello all, Is there a way in R to compute the multivariate normal density of every pair of entries in a vector efficiently instead of using for loop? For example Suppose I have a vector a=c(v_1,...,v_p)=c(0.5343909, -0.7784353, -0.0568370, 1.8772838, -1.3183407, 0.8227418,...) I want to compute density(v_i, v_j) for every pair of entries (i,j) (i!=j) in a. The joint bivariate distribution
2009 Jul 13
4
Combine two matricies
Hi, I have two matricies a and x: a<-matrix(c(3,4,5,2,3,4,1,1,2), nrow=3, ncol=3) [,1] [,2] [,3] [1,] 3 2 1 [2,] 4 3 1 [3,] 5 4 2 x<-matrix(c(3, NA, NA, NA, 2, 5, NA, 2, 2), nrow=3, ncol=3) [,1] [,2] [,3] [1,] 3 NA NA [2,] NA 2 2 [3,] NA 5 2 I wish to combine these two into one matrix using the values from x where x has values, and
2009 Feb 12
3
get top 50 correlated item from a correlation matrix for each item
Hi, I have a correlation matrix of about 3000 items, i.e., a 3000*3000 matrix. For each of the 3000 items, I want to get the top 50 items that have the highest correlation with it (excluding itself) and generate a data frame with 3 columns like ("ID", "ID2", "cor"), where ID is those 3000 items each repeat 50 times, and ID2 is the top 50 correlated items with ID,
2008 Oct 14
4
request: How to ignore columns having zero sums
Dear friends I have an array consist of r-rows and c-columns e.g. x=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,0,0,0,0,0,0,0,0); x1=array(x, dim=c(4,6)) output is > x1 [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 3 4 0 0 [2,] 1 2 3 4 0 0 [3,] 1 2 3 4 0 0 [4,] 1 2 3 4 0 0 How can i ignore columns having zero sums? Help in this regard
2010 Sep 06
3
Aggregate certain rows in a matrix
Hi, I have a matrix that looks like this a <- c(1,1,1,1,2,2,3,3,3,3) b <- c(2,2,2,3,4,4,4,5,5,6) c <- c(1,2,3,4,5,6,7,8,9,10) M <- matrix(nr=10,nc=3) M[,1] <- a M[,2] <- b M[,3] <- c > M [,1] [,2] [,3] [1,] 1 2 1 [2,] 1 2 2 [3,] 1 2 3 [4,] 1 3 4 [5,] 2 4 5 [6,] 2 4 6 [7,] 3 4 7
2009 Dec 14
1
fast matrix-vector multiplication
Hi all, Is there a way to do a matrix multiplication in a faster way? I am making a product of a matrix (composed of a lot of dummy variables) and a vector, and is there any way to make it faster? The simple X %*% y takes too long a time. I know using sparse matrix would help, but don't know how to do it i R. Thank you.
2009 Sep 06
1
struggling with "split" function
I am very sorry for such a simple question, but I am struggling with "split". I have the following data frame: x<-data.frame(A=c(NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA),
2010 Sep 27
1
compare two matrices
Hi everyone: I have a kinda easy question but i do not know how to solve that in a simple way. I want to compare the rows of two matrices. col1 <- c(1,2,3,4,5,6) col2 <- c(6,5,4,3,2,1) m <- cbind(col1, col2) col3 <- c(1,3,2,6) col4 <- c(6,3,5,1) n <- cbind(col3, col4) In matrix n, for example the first row is (1,6), it is also some row
2008 Sep 21
2
Symmetric matrix
I have following matrix : a = matrix(rnorm(36), 6) Now I want to replace the lower-triangular elements with it's upper-triangular elements. That is I want to make a symmetric matrix from a. I have tried with lower.tri() and upper.tri() function, but got desired result. Can anyone please tell me how to do that?
2009 Feb 24
1
replace zeros in a block diagonal matrix with small random values?
Hi All. Imagine you have a large block diagonal matrix. I'd like to replace the zeros in this matrix with small random (runif) numbers. Any ideas for a simple and efficient way to do this? Best regards, Rick DeShon
2010 Apr 20
1
how to select the last non-'NA' observation in a row
I have a matrix of the following form: time id 0 2 4 6 9 12 14 3 9 8 NA NA NA NA NA 7 3 NA 3 NA 3 NA 4 13 11 6 7 NA 5 NA 6 ..... I hope for each row to select the last observation which is not 'NA'. For example, for the first row, id=3, the value I want to select is 8 for the second row, id=7, the value I want to select is 4 for the third row, id=13, the
2010 Aug 09
1
Smart Indexing
Hi all, Suppose that I've two data frames, a and b say, both containing a column 'id'. While data frame 'a' contains multiple rows sharing the same id, data frame 'b' contains just one entry per id (i.e. a 1 to n relationship). For the ease of modeling I now want to generate a new data frame c, which is basically a copy of data frame 'a' augmented by the values
2008 Oct 22
2
Creating list from matrix
Hello everyone,   I have a matrix like :   mat <- matrix(rnorm(12*3), 3)   Now I want to break that matrix in 4 parts each of them are matrix of (3x3) and put those 4 matrices in a "list" object of length 4   Can anyone please tell me how to do that? Get your new Email address! Grab the Email name you&#39;ve always wanted before someone else does! [[alternative HTML
2010 Apr 23
3
reordering of matrix rows to maximize the sum of the diagonal
Hi r-help community, This question isn't so much a syntax/coding one, but here goes: Let's say I have matrix of arbitrary dimensions and I'd like to reorder the rows in such a way that I could maximize the sum of the entries along the diagonal. For example, for this 3x3 matrix: [,1] [,2] [,3] [1,] 3 4 13 [2,] 9 1 2 [3,] 2 11 1 rearranging the rows
2009 Nov 05
2
sort of cumulative counting in a vector
Dear list, I need help, since I can not come up with an easy solution to convert this vector test <- c('p','p','t','t','t') to [1] NA NA 1 2 3 which means the occurences of 't' should be summed up at the corresponding positions. The solution should also be able to handle the following scenarios: test2 <-
2008 Nov 05
2
matrix indexing and update
Folks, I have a matrix: set.seed(123) a <- matrix(rnorm(100), 10) And a vector: b <- rnorm(10) Now, I want to switch the signs of those rows of a corresponding to indices in b whose values exceed the 75 %-ile of b which(b > quantile(b)[4]) [1] 2 6 10 so I want, in effect: a[2, ] <- -a[2, ] a[6, ] <- -a[6, ] a[10, ] <- -a[10, ] I thought I could do a[which(b >
2011 Feb 22
4
identify an element in a column
Hi, R users, I'm wondering if I can identify an element in a column by an element in another column. For example: x<-1:10 y<-11:20 z<-cbind(x,y) z x y [1,] 1 11 [2,] 2 12 [3,] 3 13 [4,] 4 14 [5,] 5 15 [6,] 6 16 [7,] 7 17 [8,] 8 18 [9,] 9 19 [10,] 10 20 What I want to do is: when x=5, y=y-1 Anyone can tell me how to do this? Thanks. Gary [[alternative
2009 Jan 09
1
[Fwd: Excluding data with apply]
Dear all, I've got many responses to my initial question, which is stated below. However, from those responses it has become clear that I need to rephrase my problem. All responses dealt with subscripting the data matrix before 'apply' is run on it. But this is not want I wanted to do. 'apply' cycles through rows or columns of a matrix, and runs a function on each row or