I have a dataframe that looks like this:
x1 x2 a b c
1 7 5 1 0 0
2 3 6 1 0 0
3 2 -1 0 1 0
4 0 1 0 1 0
5 0 6 1 0 0
6 8 10 0 0 1
7 14 8 0 0 1
8 12 11 0 0 1
9 11 13 0 0 1
10 10 12 0 0 1
and I would like to sum of the values for x1 when a=1. I was thinking I could use a if statement for this and have something like follows:
if(X$a==0){
result = sum(X$a)
}
I would like to have an output like the following:
result = 10
What would be the best way to achieve this?
You could also use dplyr for this. The code would be:
library(dplyr)
df<-data.frame(x1=c(7,3,2,0,0,8,14,12,11,10),
a = c(1,1,0,0,1,0,0,0,0,0))
df %>%
summarise(result = sum(x1[a==1]))
You push the df in the pipeline, and ask to summarise in a variable called "result" which is the sum of column x1, but only where a==1.
Also with dplyr, but with a more explicit subsetting:
df %>%
filter(a==1) %>%
summarise(result=sum(x1))
First filter the rows where a == 1, then sum up the remaining x1.
Another way without dplyr:
df$result<-df$x1*df$a
colSums(df)[3]
This works because the values in column a are 1 or zero, so the product will be zero when there is a zero in the column a.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.