search for: colsum

Displaying 20 results from an estimated 444 matches for "colsum".

Did you mean: colsums
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: http://r.789695.n4.nabble.com/how-to-divide-each-column-in-a-matrix-by-its-colSums-tp3062739p3062739.html Sent from t...
2007 Apr 16
1
colSum() in Matrix objects
Hi, I'd like to simply add column-wise using Matrix objects (Csparse). It looks like one can apply mosty any base function to these objects (i.e., apply, colSums), but there is a nasty conversion to traditional matrix objects if one does that. Is there any workaround? I can see colSum listed in the help for Class 'CsparseMatrix' , but I wonder whether I'm using the default colSums() or the one specific to CsparseMatrix... #example (z = M...
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...
2013 Jan 27
2
Loops
...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,])) fa3<-(colSums(p[51:75,])) fa4<-(colSums(p[76:100,])) fa5<-(colSums(p[1:100,])) and then I need to apply to each of them the following: fa1b<-c() for (i in 1:3){ fa1b[i]<-(100-(100*abs(fa1[i]/sum(fa1[i])-(1/3)))) } fa2b<-c()...
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...
2009 Nov 30
2
command similar to colSums for rowSums?
Working with an NxMxO sized matrix, currently I can do this in my code: if (max(colSums(array)) >= number) But to get an equivalent result using rowSums, I have to do: for (i in 1:10) { if (max(rowSums(array[,,i])) >= number) } I'm running both in a much larger loop that loops millions of times, so speed and such is quite a big factor for me. Currently, the colSums line...
2009 Dec 04
5
logical masking of a matrix converts it to a vector
One problem I've been having is the special case in which only one row/column remains and the variable gets converted into a vector when entries are removed by logical masking. This is a problem because subsequent code may rely on matrix operations (apply, colsums, dim, etc) For example: > a <- matrix(c(1, 2, 3, 4), nrow = 2) > a [,1] [,2] [1,] 1 3 [2,] 2 4 > rmask <- c(TRUE, FALSE) > b <- a[rmask, ] > colSums(b) Error in colSums(b) : 'x' must be an array of at least two dimensions To ensure the code works...
2008 Mar 07
4
Warning: matrix by vector division
...ple 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 # [2,] 0.3333333 0.5000000 # [3,] 0.5000000 0.3333333 # I just wonder if some R warning should be issued here? # That is what I actually needed (column-wise frequencies): t(t(a)/colSums(a)) # [,1] [,2] # [1,] 0.0 0.3333333 # [2...
2008 Nov 14
1
# values used in a function in tapply
...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, na.rm=TRUE) amn<-tapply(a[,i],a[,1],colSums(!is.na),na.rm=T)} Adding an explicit argument to colSums(!is.na) {e.g. colSums(!is.na(a[,i]))} produced an error that said colSums was not a function. Trying an imbedded tapply instead of the colSums didn't work either. Using the sum function added the values, not the count of the values. Tha...
2012 Jul 12
3
Add row into a Matrix witout headers from Function
...matrix like this, OLDMatrix <- X1 X2 X3 ----- ------ ------ 22 24 23 25 27 27 10 13 15 the thing is, im running two function(SUM,COUNT) to get output in another matrix called NEWMatrix NEWMatrix <- c("SUM",colSums(OLDMatrix )) NEWMatrix <- c("COUNT",colSums(!is.na(OLDMatrix ))) Actually i need to get output like this, NEWMatrix <- SUM 57 64 65 COUNT 3 3 3 Instead of getting out put like above, new row getting replaced by new row of vales. and after...
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 because a sum-over-columns operation should have no problem working with columns of length 1. Matlab's "sum()" works just fine in s...
2002 Jan 07
0
New package: colSums
I've uploaded a package colSums_1.0.tar.gz to CRAN /src/contrib/Devel. It contains functions colSums, colMeans, colVars, colStdevs, rowSums, rowMeans, rowVars, and rowStdevs. These do simple, fast arithmetic on columns/rows of a matrix, or more generally across dimensions of an array, e.g. colSums(m) = apply(m, 2, sum) but fas...
2001 Jul 18
1
colSums
As best I understand it, colSums (and associated functions) in S+ 6 are optimized functions (calling special C routines) for doing simple matrix math. For example, it seems like (in S+): all.equal(colSums(m), apply(m, 2, sum)) should be TRUE for any matrix m. It also seems like colSums (and its brethren) are very fast. My ques...
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!
2011 Aug 14
3
Not sure how to use aggregate, colSums, by
...the two tables I'm looking for are also shown below in case my description isn't completely clear I would also like to be able to use the Totals of y and f for the two tables in other calculations. I can get the two sets of totals with the following commands but not the sums by country. colSums(test[test$e2=="std", c(3,4)]) colSums(test[test$e2=="con", c(3,4)]) I know there's an easy way to do this with a combination of colSums, by, aggregate but I can't seem to get it. std y f usa sum sum france sum sum can sum sum ital...
2003 Feb 13
1
colSums etc. documentation (PR#2545)
For your consideration: > z [,1] [,2] [1,] 1 NA [2,] 2 NA [3,] 3 NA > colSums(z) [1] 6 NA Correct, according to the documentation > colSums(z,na.rm=T) [1] 6 0 Surprising to me, but, as documented, correctly consistent with apply() and >sum(NULL) [1] 0 The documentation for sum() explicitly notes that the sum of an empty set is 0 by definition, so that users will...
2006 Dec 01
3
Make many barplot into one plot
..., dim=c(2,4), dimnames=list(c("Negative", "Positive"), c("Black", "Brown", "Red", "Blond"))) ## with barplot I can make a plot for each table: barplot(satu, beside=TRUE, legend.text=rownames(satu), ylim = c(0, max(colSums(satu)) * 1.2)) x11() barplot(dua, beside=TRUE, legend.text=rownames(dua), ylim = c(0, max(colSums(dua)) * 1.2)) x11() barplot(tiga, beside=TRUE, legend.text=rownames(tiga), ylim = c(0, max(colSums(tiga)) * 1.2)) x11() barplot(empat, beside=TRUE, legend.text=rownames(empat),...
2013 Sep 20
3
search species with all absence in a presence-absence matrix
Dear list I have a matrix composed of islandID as rows and speciesID as columns. IslandID: Island A, B, C….O (15 islands in total) SpeciesID: D0001, D0002, D0003….D0100 (100 species in total) The cell of the matrix describes presence (1) or absence (0) of the species in an island. Now I would like to search the species with absence (0) in all the islands (Island A to Island O.)
2009 Jan 28
3
for/if loop
...9;t return. I think it must be some problem in my loop. Probably something stupid or easy. But I tried to look for previous posts in forum and read R language help. But none can help.. Thanks! for (ii in 1:100){ for (pp in 1:pp+1){ for (rr in 1:rr+1){ if (panel[ii,1]!=pp) { hll(pp,1)=ColSums(lselb1(rr:ii-1,1)) hll(pp,2)=ColSums(lselb2(rr:ii-1,1)) rr=ii pp=pp+1 } else { hll(pp,1)=ColSums(lselb1(rr:ii,1)) hll(pp,2)=ColSums(lselb2(rr:ii,1)) rr=ii pp=pp+1} } }}} in fact I have the corresponding Gauss code here. But I really don't know how to wri...
2003 Sep 14
1
Documentation of colSums et. al (PR#4154)
Full_Name: Doug Grove Version: 1.7.0 OS: Linux Submission from: (NULL) (209.31.211.56) Hi, Minor mistake in the documentation on the colSums page. In the ARGUMENTS section it states for 'dims' that: For `col*', the sum or mean is over dimensions `dims+1, ...'; for `row*' it is over dimensions `1:dims'. These two are reversed. Thanks, Doug Grove