# sum (not count cases) of a numeric vector based on relative value

``````df<-data.frame(a=1:100)
sum(df\$a>50)
[1] 50
``````

The answer I am looking for is the sum of 51+52...100. I tried searching but only finding poor questions/ratings or leads to Excel sumif. Thanks

-
`a > 50` returns a logical, ie, yes/no `TRUE`/`FALSE`. Since R is vectorized, you get a vector of logicals. `TRUE` can be coerced to 1 and `FALSE` to 0, so your sum is `1 + 1 + ... + 1` = 50. You would need to subset the vector first (`a[51:100]`) and then sum that. – rawr Feb 22 '14 at 3:39

Try the following:

``````sum(subset(df,a>50))
``````
-

I think that there are a few things going on with your question. I'll try to answer them one at a time:

You created a data.frame, but then only used it for the vector. Let's try to create just one variable to answer your question:

``````a = 1:50
``````

You created data where each variable (e.g. x[10]) is equal to the value of the index (e.g. 10). As we'll see below, this can lead to some confusion because there are multiple ways to index in R. So let's use this:

``````set.seed(0)
a = sample(100, n=50)
``````

In your question, you call sum(a > 50). First you should try to understand what a > 50 returns. It might not be what you think!

``````a > 50
[1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
``````

That is, `a > 50` returns the result of applying the (logical) test "greater than 50?" to each value in the vector a. This is the "vectorization" that you often hear about in R.

Note that in R, you can put a boolean (logical) vector as a subscript to a vector. It will only return the elements which are TRUE:

``````a[a > 50]
[1] 90 56 88 85 96 61 58 60 66 87 82 63 74 51 62 86 69 54 77 92 81 64 83 91
``````

Of course, these values can then be fed to `sum`

``````sum(a[a > 50])
[1] 1776
``````

At some point this will become second nature to you. I recommend reading John Cook's great blog post: Five Kinds of Subscripts in R - it will help you to be able to answer all sorts of related questions.

-

Unsing the condition in the "address brackets" works without a subset.

``````sum( df[ df\$a>50, ])
``````
-
The text passage contains a discussion about a specific R programming problem, where the user is trying to calculate the sum of a subset of a numeric vector. The user's question is about how to correctly sum the values in the vector that are greater than 50, and multiple answers are provided explaining the correct approach.
single_question_with_answer
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# sum (not count cases) of a numeric vector based on relative value

``````df<-data.frame(a=1:100)
sum(df\$a>50)
[1] 50
``````

The answer I am looking for is the sum of 51+52...100. I tried searching but only finding poor questions/ratings or leads to Excel sumif. Thanks

-
`a > 50` returns a logical, ie, yes/no `TRUE`/`FALSE`. Since R is vectorized, you get a vector of logicals. `TRUE` can be coerced to 1 and `FALSE` to 0, so your sum is `1 + 1 + ... + 1` = 50. You would need to subset the vector first (`a[51:100]`) and then sum that. – rawr Feb 22 '14 at 3:39

Try the following:

``````sum(subset(df,a>50))
``````
-

I think that there are a few things going on with your question. I'll try to answer them one at a time:

You created a data.frame, but then only used it for the vector. Let's try to create just one variable to answer your question:

``````a = 1:50
``````

You created data where each variable (e.g. x[10]) is equal to the value of the index (e.g. 10). As we'll see below, this can lead to some confusion because there are multiple ways to index in R. So let's use this:

``````set.seed(0)
a = sample(100, n=50)
``````

In your question, you call sum(a > 50). First you should try to understand what a > 50 returns. It might not be what you think!

``````a > 50
[1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
``````

That is, `a > 50` returns the result of applying the (logical) test "greater than 50?" to each value in the vector a. This is the "vectorization" that you often hear about in R.

Note that in R, you can put a boolean (logical) vector as a subscript to a vector. It will only return the elements which are TRUE:

``````a[a > 50]
[1] 90 56 88 85 96 61 58 60 66 87 82 63 74 51 62 86 69 54 77 92 81 64 83 91
``````

Of course, these values can then be fed to `sum`

``````sum(a[a > 50])
[1] 1776
``````

At some point this will become second nature to you. I recommend reading John Cook's great blog post: Five Kinds of Subscripts in R - it will help you to be able to answer all sorts of related questions.

-

Unsing the condition in the "address brackets" works without a subset.

``````sum( df[ df\$a>50, ])
``````
-
user
The text passage contains a discussion about a specific R programming problem, where the user is trying to calculate the sum of a subset of a numeric vector. The user's question is about how to correctly sum the values in the vector that are greater than 50, and multiple answers are provided explaining the correct approach.

TYPE: SINGLE_QUESTION_WITH_ANSWER