Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of the SumIf function in R

Tags:

r

I am new to R and this site, but I searched and didn't find the answer I was looking for.

If I have the following data set "total":

names <- c("a", "b", "c", "d", "a", "b", "c", "d")
x <- cbind(x1 = 3, x2 = c(3:10))
total <- data.frame(names, x)
total

  names x1 x2
1     a  3  3
2     b  3  4
3     c  3  5
4     d  3  6
5     a  3  7
6     b  3  8
7     c  3  9
8     d  3  10

How can I create a new data set that works like the SumIf Excel function with just unique rows?

The answer should be a new data set "summary" that is 4 x 3.

names <- unique(names)
summary <- data.frame(names)
summary$Sumx1 <- ?????
summary$Sumx2 <- ?????
summary

 names Sumx1 Sumx2
1  a     6    10
2  b     6    12
3  c     6    14
4  d     6    16
like image 721
RabbleRouser Avatar asked Feb 06 '14 15:02

RabbleRouser


People also ask

Is there a Sumif function in R?

In this article, we will discuss the sumif function in R Programming Language. This function is used to group the data and get the sum of the values by a group of values in the dataframe, so we are going to perform this operation on the dataframe.

What can I use instead of Sumif?

With SUMPRODUCT In spirit, the SUMPRODUCT option is closest to the SUMIFS formula since we are summing values based on multiple criteria.

Is Sumif same as Sumifs?

The only difference between Excel SUMIFS & SUMIF functions is that SUMIFs can check for multiple criteria at once, while SUMIF can check for one criterion at a time. The SUMIF formula returns the sum of cells based on one criterion (a result that matches one condition).

Is there a Sumif equivalent for text?

The SUMIF function is conditional if the function used to sum the cells based on certain criteria, not the criteria can be a certain text too. For example, we want to sum up a group of cells. If the adjacent cell has a specified text, we can use the function: =SUMIF(Text Range,” Text,” cells range for sum).


2 Answers

In base R:

aggregate(. ~ names, data=total, sum)
like image 57
Matthew Plourde Avatar answered Oct 23 '22 08:10

Matthew Plourde


You can use ddply from the plyr package:

library(plyr)
ddply(total, .(names), summarise, Sumx1 = sum(x1), Sumx2 = sum(x2))

  names Sumx1 Sumx2
1     a     6    10
2     b     6    12
3     c     6    14
4     d     6    16

You can also use data.table:

library(data.table)
DT <- as.data.table(total)
DT[ , lapply(.SD, sum), by = "names"]

   names x1 x2
1:     a  6 10
2:     b  6 12
3:     c  6 14
4:     d  6 16

With the new dplyr package, you can do:

library(dplyr)
total %>%
  group_by(names) %>%
  summarise(Sumx1 = sum(x1), Sumx2 = sum(x2))

  names Sumx1 Sumx2
1     d     6    16
2     c     6    14
3     b     6    12
4     a     6    10
like image 44
Sven Hohenstein Avatar answered Oct 23 '22 07:10

Sven Hohenstein