rowsum

How to get rowSums for selected columns in R

时间秒杀一切 提交于 2019-12-25 15:26:10
问题 I am a newbie to R and seek help to calculate sums of selected column for each row. My simple data frame is as below. data = data.frame(location = c("a","b","c","d"), v1 = c(3,4,3,3), v2 = c(4,56,3,88), v3 =c(7,6,2,9), v4=c(7,6,1,9), v5 =c(4,4,7,9), v6 = c(2,8,4,6)) I want sum of columns V1 to V3 and V4 to V6 for my each row in a new data frame. x1 x2 a 14 13 b 66 18 c d I did something like below. rowSums(data[,2:4][,5:7]) But something should be wrong in my codes. Thanks in advance for any

Row and column sums in R

元气小坏坏 提交于 2019-12-23 20:30:06
问题 This is an example of how my data set ( MergedData ) looks like in R, where each of my participants (5 rows) obtained a score number in every test (7 columns). I would like to know the total score of all tests combined (all columns) but for each participant (row). Also, my complete data set has more than just these few variables, so if possible, I would like do it using a formula & loop and not having to type row by row/column by column. Participant TestScores ParticipantA 2 4 2 3 2 3 4

Omit inf from row sum in R

为君一笑 提交于 2019-12-18 03:51:43
问题 So I am trying to sum the rows of a matrix, and there are inf's within it. How do I sum the row, omitting the inf's? 回答1: Multiply your matrix by the result of is.finite(m) and call rowSums on the product with na.rm=TRUE . This works because Inf*0 is NaN . m <- matrix(c(1:3,Inf,4,Inf,5:6),4,2) rowSums(m*is.finite(m),na.rm=TRUE) 回答2: A[is.infinite(A)]<-NA rowSums(A,na.rm=TRUE) Some benchmarking for comparison: library(microbenchmark) rowSumsMethod<-function(A){ A[is.infinite(A)]<-NA rowSums(A

Error in ncol(xj) : object 'xj' not found using rowSums

╄→尐↘猪︶ㄣ 提交于 2019-12-13 22:17:29
问题 I'm using R and rowSums function for a data frame as discussed in this post: Sum over spatialGridDataFrame: rowSums or colSums. Now suing rowSums as follows: FLintMod[[2+nsim]] <- rowSums(FLintMod@data[, 2:(1+nsim)], na.rm=TRUE) Now I'm facing new error and I really don't have any idea why I'm getting this error: Error in ncol(xj) : object 'xj' not found Could you please help me understand this problem and solve it. For your information, this code worked before but after improving code and

R - How to sum objects in a column between an interval defined by conditions on another column

不羁岁月 提交于 2019-12-13 20:14:06
问题 This comes as an application to this question:Sum object in a column between an interval defined by another column What I would like to know is how to adjust the answer if I want to sum the values in B, for ((A[i+1]-A[i]==0) or (A[i+1]-A[i]==1) or (A[i]-A[i-1]==0) or (A[i]-A[i-1]==1)) where i is the row index, so basically sum B rows for A-s that have the same value +/- 1, but not sum the same row twice? I tried building a loop function but I get stuck when using row indices with data frames.

Calculate rowMeans on a range of column (Variable number)

牧云@^-^@ 提交于 2019-12-10 21:20:54
问题 I want to calculate rowMeans of a range of column but I cannot give the hard-coded value for colnames (e.g c(C1,C3)) or range (e.g. C1:C3) as both names and range are variable. My df looks like: > df chr name age MGW.1 MGW.2 MGW.3 HEL.1 HEL.2 HEL.3 1 123 abc 12 10.00 19 18.00 12 13.00 -14 2 234 bvf 24 -13.29 13 -3.02 12 -0.12 24 3 376 bxc 17 -6.95 10 -18.00 15 4.00 -4 This is just a sample, in reality I have columns ranging in MGW.1 ... MGW.196 and so. Here Instead of giving the exact

Sum all columns whose names start with a pattern, by group

≡放荡痞女 提交于 2019-12-08 18:25:58
问题 I'm fairly new to R and I'm trying to sum columns by groups based on their names. I have a data frame like this one: DT <- data.frame(a011=c(0,10,20,0),a012=c(010,10,0,0),a013=c(10,30,0,10), a021=c(10,20,20,10),a022=c(0,0,0,10),a023=c(20,0,0,0),a031=c(30,0,10,0), a032=c(0,0,10,0),a033=c(20,0,0,0)) I would like to obtain the sum of all the columns starting with "a01", of all the columns starting with "a02" and all the columns starting with "a03": a01tot a02tot a03tot 20 30 50 50 20 0 20 20 20

rowsum for matrix over specified number of columns in R

删除回忆录丶 提交于 2019-12-06 05:39:44
问题 I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function but they are either giving me strange results or an error message. To illustrate, please see example code for an 8x8 matrix below. For the first row I need the sum of the row except item [1,1], for second row the sum except items [2,1] and [2,2] etc

rowsum for matrix over specified number of columns in R

旧时模样 提交于 2019-12-04 11:11:41
I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function but they are either giving me strange results or an error message. To illustrate, please see example code for an 8x8 matrix below. For the first row I need the sum of the row except item [1,1], for second row the sum except items [2,1] and [2,2] etc. m1 <- matrix(c(0.2834803,0.6398198,0.0766999,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000, 0

Efficiently compute the row sums of a 3d array in R

拈花ヽ惹草 提交于 2019-12-03 17:50:41
问题 Consider the array a : > a <- array(c(1:9, 1:9), c(3,3,2)) > a , , 1 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 , , 2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 How do we efficiently compute the row sums of the matrices indexed by the third dimension, such that the result is: [,1] [,2] [1,] 12 12 [2,] 15 15 [3,] 18 18 ?? The column sums are easy via the 'dims' argument of colSums() : > colSums(a, dims = 1) but I cannot find a way to use rowSums() on the array to achieve the desired