similar to: Idiom for column operations?

Displaying 20 results from an estimated 10000 matches similar to: "Idiom for column operations?"

2010 Nov 28
4
how to divide each column in a matrix by its colSums?
Hi, I have a matrix, say m=matrix(c( 983,679,134, 383,416,84, 2892,2625,570 ),nrow=3 ) i can find its row/col sum by rowSums(m) colSums(m) How do I divide each row/column by its rowSum/colSums and still return in the matrix form? (i.e. the new rowSums/colSums =1) Thanks. Casper -- View this message in context:
2008 Mar 07
4
Warning: matrix by vector division
Dear list, I just made a very simple mistake, but it was hard to spot. And I think that I should warn other people, because it is probably so simple to make... === R code === # Let us create a matrix: (a <- cbind(c(0,1,1), rep(1,3))) # [,1] [,2] # [1,] 0 1 # [2,] 1 1 # [3,] 1 1 # That is a MISTAKE: a/colSums(a) # [,1] [,2] # [1,] 0.0000000 0.3333333
2002 Aug 14
3
t-test via matrix operations
I need to calculate a large number of t statistics, and would like to do so via matrix operations. So far I have figured out a way to calculate the mean of each row of the matrix: d <- matrix(runif(100000,1,10), 1000, 10) # some test data s <- rep(1,ncol(d)) # a sum vector to use for matrix multiplication means <- (d%*%s)/ncol(d) This is at least 1 order of magnitude faster than
2011 Nov 27
1
generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t - i + 1})
Dear R-help, I have been trying really hard to generate the following vector given the data (x) and parameter (alpha) efficiently. Let y be the output list, the aim is to produce the the following vector(y) with at least half the time used by the loop example below. y[1] = alpha * x[1] y[2] = alpha^2 * x[1] + alpha * x[2] y[3] = alpha^3 * x[1] + alpha^2 * x[2] + alpha * x[3] ..... below are
2024 Feb 27
2
converting MATLAB -> R | element-wise operation
Why anything but sweep? The fundamental data type in Matlab is a matrix... they don't have vectors, they have Nx1 matrices and 1xM matrices. Vectors don't have any concept of "row" vs. "column". Straight division is always elementwise with recycling as needed, and matrices are really vectors in row-major order: 1 2 3 4 5 6 is really 1 4 2 5 3 6 and when you do
2009 Dec 24
3
Newbie: colSums() compared with Matlab's sum()
Hi all, I'm trying to learn R after years of Matlab's experience. Here is an issue I couldn't solve today. Consider the following piece of code (written by memory): for(i in 1:n){ submat <- data[1:i,] C <- colSums(submat) } The problem is that at the first iteration, data[1:1,] reduces to a vector and colSums returns an error. This sounds really strange to me
2012 Mar 08
4
Correlation between 2 matrices but with subset of variables
Dear All, I have two matrices A (40 x 732) and B (40 x 1230) and would like to calculate correlation between them.  I can use: cor(A,B, method="pearson") to calculate correlation between all possible pairs. But the issue is that there is one-many specific mappings between A and B and I just need to calculate correlations for those pairs (not all). Some variables in A (proteins, say p1)
2024 Feb 29
2
[External] converting MATLAB -> R | element-wise operation
I decided to do a direct comparison of transpose and sweep. library(microbenchmark) NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix lambda <- c(2, 3, 4) # Example vector colNN <- t(NN) microbenchmark( sweep = sweep(NN, 2, lambda, "/"), transpose = t(t(NN)/lambda), colNN = colNN/lambda ) Unit: nanoseconds expr min lq
2002 Mar 17
5
compute variance of every column in a matrix without a loop
Is it possible to compute the variance of every column in a matrix without a loop? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
2002 Nov 15
2
Why no colSDs etc
Hi people, If there is a fn "colMeans" why isn't there a "colSDs" etc Thanks, Phil. -- Philip Rhoades Pricom Pty Limited (ACN 003 252 275) GPO Box 3411 Sydney NSW 2001 Australia Mobile: +61:0411-185-652 Fax: +61:2:8923-5363 E-mail: pri at chu.com.au -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read
2007 Nov 09
1
Recycling Rule
Hi, I have an efficiency question. To centralise a dataset (in a data frame) I have been using: a <- mean(Data) Data <- t(t(Data)-a) ...to force row-wise recycling. Is there a better/more efficient/more elegant way than transposing the dataset twice? Thanks [[alternative HTML version deleted]]
2005 Apr 05
3
How to do aggregate operations with non-scalar functions
Hi, I have a data set, the structure of which is something like this: > a <- rep(c("a", "b"), c(6,6)) > x <- rep(c("x", "y", "z"), c(4,4,4)) > df <- data.frame(a=a, x=x, r=rnorm(12)) The true data set has >1 million rows. The factors "a" and "x" have about 70 levels each; combined together they subset
2010 Nov 13
1
Efficient marginal sums?
Hi Folks, [This is not unrelated to the current "vector of vectors" thread, but arises quite independently] Say I have a function f(x,y) which computes a value for scalar x and y; or, if either x=X or y=Y is a vector, a corresponding vector of values f(X,y) or f(x,Y) (with the usual built-in vectorisation of operations). Now I have X=(x.1,x.2,...,x.m) and Y=(y.1,y.2,...,y.n). I'm
2013 Jan 27
2
Loops
Dear Contributors, I am asking help on the way how to solve a problem related to loops for that I always get confused with. I would like to perform the following procedure in a compact way. Consider that p is a matrix composed of 100 rows and three columns. I need to calculate the sum over some rows of each column separately, as follows: fa1<-(colSums(p[1:25,])) fa2<-(colSums(p[26:50,]))
2007 Jul 10
5
Seg Fault caused by StatusBar. Am I doing something wrong?
Some odd bug cropped up when I made a bunch of code changes to my app recently. I started getting the following everytime I closed the app: ---------------------------------- [BUG] Segmentation fault ruby 1.8.6 (2007-03-13) [i386-mswin32] This application has requested the Runtime to terminate it in an unusual way. Please contact the application''s support team for more information.
2005 Apr 21
1
colSums and rowSums with arrays - different classes and dim ?
Hi, I'm using colSums and rowSums to sum the first dimensions of arrays. It works ok but the resulting object is different. See > a3d <- array(rnorm(120, mean=2), dim=c(20,6,1)) > dim(colSums(a3d)) [1] 6 1 > dim(rowSums(a3d)) NULL > class(colSums(a3d)) [1] "matrix" > class(rowSums(a3d)) [1] "numeric" I was expecting rowSums to preserve the array
2008 Nov 14
1
# values used in a function in tapply
Hello, I am using tapply to pull out data by the day of week and then perform functions (e.g. mean). I would like to have the number of values used for the calcuation for the functions, sorted by each day of week. A number of entries in any given column are NAs. I have tried the following code and simple variants with no luck. for (i in 1:length(a[1,])){ x<-tapply(a[,i],a[,1],mean,
2007 Jan 26
2
%*% in Matrix objects
Dear R users, I need to normalize a bunch of row vectors. At a certain point I need to divide a matrix by a vector of norms. I find that the behavior of Matrix objects differs from normal matrix objects. Example the following code examples differ only in xnormed changing from normal to Matrix object: x = matrix(1:12,3,4) x = as(x, "CsparseMatrix") xnorms = sqrt(colSums(x^2))
2008 Feb 29
4
Column sums from a data frame (without the headers)
Does anyone know how to get a vector of column sum from a data frame? You can use colSums(), but this gives you a object of type "numeric" with the column labels in the first row, and the sums in the second row. I just want a vector of the sums, and I can't figure out a way to index the "numeric" object. Thanks!
2001 Dec 14
2
colSums in C
Hi, all! My project today is to write a speedy colSums(), which is a function available in S-Plus to add the columns of a matrix. Here are 4 ways to do it, with the time it took (elapsed, best of 3 trials) in both R and S-Plus: m <- matrix(1, 400, 40000) x1 <- apply(m, 2, sum) ## R=16.55 S=52.39 x2 <- as.vector(rep(1,nrow(m)) %*% m) ## R= 2.39 S= 8.52 x3 <-