text: # 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, ])
  ``````
  -
explanation: The provided text is a discussion from a forum or Q&A site, likely related to programming in R. It contains a user's question about calculating the sum of a numeric vector based on a condition, and several responses providing explanations and solutions to the problem. The user is asking how to sum the values of a vector that are greater than 50, and the responses explain the issue with the user's initial approach and provide correct solutions.
distilabel_metadata.content: Evaluate the user given text extract and determine if it contains a math question and optionally and answer to this question.
  
  Output rules:
  
  1. Briefly describe the document and mention if it contains a question or a question and answer.
  2. Finalize the output with: Label: no-question,question,question-and-answer,multiple-question,multiple-question-and-answer
distilabel_metadata.role: system
distilabel_metadata.content: # 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, ])
  ``````
  -
distilabel_metadata.role: user
distilabel_metadata.raw_output_q_a_detector_0: The provided text is a discussion from a forum or Q&A site, likely related to programming in R. It contains a user's question about calculating the sum of a numeric vector based on a condition, and several responses providing explanations and solutions to the problem. The user is asking how to sum the values of a vector that are greater than 50, and the responses explain the issue with the user's initial approach and provide correct solutions.
  
  Label: question-and-answer