Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:27:30+00:00 2026-06-12T00:27:30+00:00

I have been searching for this for a while, but haven’t been able to

  • 0

I have been searching for this for a while, but haven’t been able to find a clear answer so far. Probably have been looking for the wrong terms, but maybe somebody here can quickly help me. The question is kind of basic.

Sample data set:

set <- structure(list(VarName = structure(c(1L, 5L, 4L, 2L, 3L),
 .Label = c("Apple/Blue/Nice", 
"Apple/Blue/Ugly", "Apple/Pink/Ugly", "Kiwi/Blue/Ugly", "Pear/Blue/Ugly"
), class = "factor"), Color = structure(c(1L, 1L, 1L, 1L, 2L), .Label = c("Blue", 
"Pink"), class = "factor"), Qty = c(45L, 34L, 46L, 21L, 38L)), .Names = c("VarName", 
"Color", "Qty"), class = "data.frame", row.names = c(NA, -5L))

This gives a data set like:

set


      VarName      Color Qty
1 Apple/Blue/Nice  Blue  45
2  Pear/Blue/Ugly  Blue  34
3  Kiwi/Blue/Ugly  Blue  46
4 Apple/Blue/Ugly  Blue  21
5 Apple/Pink/Ugly  Pink  38

What I would like to do is fairly straight forward. I would like to sum (or averages or stdev) the Qty column. But, also I would like to do the same operation under the following conditions:

  1. VarName includes “Apple”
  2. VarName includes “Ugly”
  3. Color equals “Blue”

Anybody that can give me a quick introduction on how to perform this kind of calculations?

I am aware that some of it can be done by the aggregate() function, e.g.:

aggregate(set[3], FUN=sum, by=set[2])[1,2]

However, I believe that there is a more straight forward way of doing this then this. Are there some filters that can be added to functions like sum()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:27:32+00:00Added an answer on June 12, 2026 at 12:27 am

    Is this what you’re looking for?

     # sum for those including 'Apple'
     apple <- set[grep('Apple', set[, 'VarName']), ]
     aggregate(apple[3], FUN=sum, by=apple[2])
      Color Qty
    1  Blue  66
    2  Pink  38
    
     # sum for those including 'Ugly'
     ugly <- set[grep('Ugly', set[, 'VarName']), ]
     aggregate(ugly[3], FUN=sum, by=ugly[2])
      Color Qty
    1  Blue 101
    2  Pink  38
    
     # sum for Color==Blue
     sum(set[set[, 'Color']=='Blue', 3])
    [1] 146
    

    The last sum could be done by using subset

    sum(subset(set, Color=='Blue')[,3])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching this for quite a while but couldn't find a solution
After extensive searching on this matter, I have been unable to find an answer
I've been searching and experimenting for a while with this, but I have had
I've been searching for this for quite a while but can't seem to find
I've been searching for a while but didn't find the solutions. I have an
I have been searching for this for quite a while and couldn't find a
I have been searching the answer to this question for a while. It seemed
I've been searching for a while for a solution, but haven't been able to
I've been searching this web-site and other sources for an answer, but haven't found
I've been searching on this for a while now, but I can't find anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.