rowsum

Equivalent of rowsum function for Matrix-class (dgCMatrix)

…衆ロ難τιáo~ 提交于 2020-02-23 07:54:08
问题 For the base R matrix class we have the rowsum function, which is very fast for computing column sums across groups of rows. Is there an equivalent function or approach implemented in the Matrix-package? I'm particularly interested in a fast alternative to rowsum for large dgCMatrix-objects (i.e. millions of rows, but roughly 95% sparse). 回答1: I know this is an old question, but Matrix::rowSums might be the function you are looking for. 来源: https://stackoverflow.com/questions/51025979

Calculating Sum Column and ignoring Na [duplicate]

笑着哭i 提交于 2020-01-30 05:19:12
问题 This question already has answers here : ignore NA in dplyr row sum (5 answers) Closed 2 years ago . I am trying to create a Total sum column that adds up the values of the previous columns. However I am having difficulty if there is an NA. If there is an NA in the row, my script will not calculate the sum. How do I edit the following script to essentially count the NA's as 0, or just ignore them completely but still calculate the sum. I don't want to actually change the NA to 0. CTDB %>%

How to sum every nth (200) observation in a data frame using R [duplicate]

别来无恙 提交于 2020-01-14 06:24:10
问题 This question already has answers here : calculating mean for every n values from a vector (3 answers) Closed 2 years ago . I am new to R so any help is greatly appreciated! I have a data frame of 278800 observations for each of my 10 variables, I am trying to create an 11th variable that sums every 200 observations (or rows) of a specific variable/column (sum(1:200, 201:399, 400:599 etc.) Similar to the offset function in excel. I have tried subsetting my data to just the variable of

Rowsums conditional on column name in a loop

て烟熏妆下的殇ゞ 提交于 2020-01-05 03:48:29
问题 This is a follow-up question from this one: Rowsums conditional on column name My data frame is called wiod and looks like this: VAR1 VAR2 AUS1 ... AUS56 BEL1 ... BEL56 NLD1 ... NLD56 A D 23 ... 99 0 ... 444 123 ... 675 B D 55 ... 6456 0 ... 557 567 ... 4345 I'd like to calculate the row-sums for the variables AUS, BEL, NLD and then drop the old variables. Like this: wiot <- wiot %>% mutate(AUS = rowSums(.[grep("AUS", names(.))])) %>% mutate(BEL = rowSums(.[grep("BEL", names(.))])) %>% mutate

R sum of rows for different group of columns that start with similar string

本秂侑毒 提交于 2020-01-03 10:45:19
问题 I'm quite new to R and this is the first time I dare to ask a question here. I'm working with a dataset with likert scales and I want to row sum over different group of columns which share the first strings in their name. Below I constructed a data frame of only 2 rows to illustrate the approach I followed, though I would like to receive feedback on how I can write a more efficient way of doing it. df <- as.data.frame(rbind(rep(sample(1:5),4),rep(sample(1:5),4))) var.names <- c("emp_1","emp_2

Ragged rowSums in R

巧了我就是萌 提交于 2019-12-25 18:41:50
问题 I am trying to do a rowSum for the actuals columns. However, I would like to include the values up to the UpTo date for certain observations. Here is the data frame: dat <- structure(list(Company = c("ABC", "DEF", "XYZ"), UpTo = c(NA, "Q2", "Q3"), Actual.Q1 = c(100L, 80L, 100L), Actual.Q2 = c(50L, 75L, 50L), Forecast.Q3 = c(80L, 50L, 80L), Forecast.Q4 = c(90L, 80L, 100L)), .Names = c("Company", "UpTo", "Actual.Q1", "Actual.Q2", "Forecast.Q3", "Forecast.Q4"), class = "data.frame", row.names =

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