Questions tagged [rowsum]

Questions regarding rowsum, R function which gives column sums of a matrix or data frame, based on a grouping variable

208 questions
2
votes
4 answers

count the number of columns for each row by condition on character and missing

I want to count the number of columns for each row by condition on character and missing. For example, I have this dataset, test. I want to create num columns, counting the number of columns 'not' in missing or empty…
ESKim
  • 422
  • 4
  • 14
2
votes
1 answer

Creating a new variable that contains conditional rowsums in R

I have a dataframe with 12 variables: id_group1, id_group2, ..., id_group11 : 11 variables with a numeric value mean_id: mean over all the above mentioned id_group variables What I would need now is a new variable that contains the rowsum only for…
Sarah1989
  • 23
  • 2
2
votes
2 answers

Getting rowSums for triplicate records and retaining only the one with highest value

I have a data frame with 163 observations and 65 columns with some animal data. The 163 observations are from 56 animals, and each was supposed to have triplicated records, but some information was lost so for the majority of animals, I have…
czarniutki
  • 41
  • 5
2
votes
1 answer

How does the 'group' argument in rowsum work?

I understand what rowsum() does, but I'm trying to get it to work for myself. I've used the example provided in R which is structured as such: x <- matrix(runif(100), ncol = 5) group <- sample(1:8, 20, TRUE) xsum <- rowsum(x, group) What is the…
D.C. the III
  • 340
  • 3
  • 14
2
votes
1 answer

R: Is there a column version for "rowsum", but to compute the mean and not just the sum?

I have a matrix, with columns grouped according to a grouping variable: x <- matrix(sample(1:10,20,T),ncol=4) [,1] [,2] [,3] [,4] [1,] 7 8 5 3 [2,] 7 5 4 7 [3,] 7 1 9 3 [4,] 4 8 8 8 [5,] 9 9…
2
votes
2 answers

Conditional value change across columns

I need to calculate when a value switched between 0 and 1, values are distributed across columns, the switch is not given, and NAs are present. I attempted with mutate and rowSums with little results. Example: df <- data.frame(entry = c(1:5), …
MCS
  • 1,071
  • 9
  • 23
2
votes
4 answers

Summing rows of a matrix based on column index

I am trying to go from a matrix that has columns that "belong together" to one where the row-sums of the relevant sub-matrices have been formed. I.e. going from [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]…
Björn
  • 644
  • 10
  • 23
2
votes
6 answers

Simplify multiple rowSums looping through columns

I'm currently on R trying to create for a DF multiple columns with the sum of previous one. Imagine I got a DF like this: df= sep-2016 oct-2016 nov-2016 dec-2016 jan-2017 1 70 153 NA 28 19 2 57 …
2
votes
2 answers

Error when filering on rowSums using dplyr

I have the following df where df <- data.frame(V1=c(0,0,1),V2=c(0,0,2),V3=c(-2,0,2)) If I do filter(df,rowSums!=0) I get the following error: Error in filter_impl(.data, quo) : Evaluation error: comparison (6) is possible only for atomic and list…
David R
  • 77
  • 8
2
votes
2 answers

Using read.csv for rowSums with blanks in R

I'm working in R with data imported from a csv file and I'm trying to take a rowSum of a subset of my data. The data can either be 0, 1, or blank. I'd like to take a sum of all the 1s across all these rows (and ideally find a count of how many…
Ardyn
  • 125
  • 2
  • 7
2
votes
2 answers

Summing every 4 columns together (convert quarterly data to annual on a large dataset)

I have a (large) dataframe of the form: Variable Country 2007-Q1 2007-Q2 2007-Q3 2007-Q4 2008-Q1 2008-Q2 2008-Q3 2008-Q4 Var1 AR:Argentina 69.8 67.3 65 63.6 60.4 56.6 54.4 57.3 Var2 AR:Argentina …
Atom Vayalinkal
  • 2,642
  • 7
  • 29
  • 37
2
votes
4 answers

Columns Equal in R

How do I determine if columns values are equal? Sample dataframe: A B C D E ab NA NA NA NA ed dd NA NA NA ll NA NA NA NA dd NA NA ef NA How do I determine from columns 2:5 if they…
nak5120
  • 4,089
  • 4
  • 35
  • 94
2
votes
1 answer

rowsum on a nested table in r

I have a complex data frame and a minimal example is as follows: df <- structure(list(District = c("Adilabad", "Adilabad", "Adilabad", "Adilabad", "Adilabad", "Adilabad", "Adilabad", "Adilabad", "Adilabad", …
LeArNr
  • 635
  • 1
  • 6
  • 12
2
votes
2 answers

how to use the 'group' argument in function rowsum

M.5 M5.9 M10.14 M14.17 F.5 F5.9 F10.14 F14.17 Alabama 154902 158765 163731 97673 146906 154067 157592 91339 Alaska 27593 27033 26425 15899 26341 25172 24487 14315 Arizona 223705 236024 …
Zi Xin Lee
  • 43
  • 1
  • 1
  • 7
2
votes
1 answer

Row sums of matrix over/under diagonal

I want to calculate row/col sums for upper/lower triangle matrix (with diagonal). Example: m <- matrix(1:9, nrow=3) #result: upperRowSums(m) ## [1] 12 13 9 lowerRowSums(m) ## [1] 1 7 18 I know that this could be done with simple for loop, but I…
bartektartanus
  • 15,284
  • 6
  • 74
  • 102