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 7679675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:55:58+00:00 2026-05-31T17:55:58+00:00

Fake data for illustration: df <- data.frame(a=c(1,2,3,4,5), b=(c(2,2,2,2,NA)), c=c(NA,2,3,4,5))) This would get me the

  • 0

Fake data for illustration:

df <- data.frame(a=c(1,2,3,4,5), b=(c(2,2,2,2,NA)), 
                 c=c(NA,2,3,4,5)))

This would get me the answer I want IF it weren’t for the NA values:

df$count <- with(df, (a==1) + (b==2) + (c==3)) 

Also, would there be an even more elegant way if I was only interested in, e.g. variables==2?

df$count <- with(df, (a==2) + (b==2) + (c==2)) 

Many thanks!

  • 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-05-31T17:55:59+00:00Added an answer on May 31, 2026 at 5:55 pm

    The following works for your specific example, but I have a suspicion that your real use case is more complicated:

    df$count <- apply(df,1,function(x){sum(x == 1:3,na.rm = TRUE)})
    > df
      a  b  c count
    1 1  2 NA     2
    2 2  2  2     1
    3 3  2  3     2
    4 4  2  4     1
    5 5 NA  5     0
    

    but this general approach should work. For instance, your second example would be something like this:

    df$count <- apply(df,1,function(x){sum(x == 2,na.rm = TRUE)})
    

    or more generally you could allow yourself to pass in a variable for the comparison:

    df$count <- apply(df,1,function(x,compare){sum(x == compare,na.rm = TRUE)},compare = 1:3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a good freeware that generates fake data for SQL Server? It would
I would like to know if a fake data generator exists for Java. In
I would like to make a subset of a data frame in R that
I want to generate fake data for use in testing a graphing library (highcharts
I want to fake a simple animation on page load by sequentially revealing several
Is there a good way in C++ to implement (or fake) a type for
IN PHP: Is there a way for the user to fake a session variable?
I am starting with an Activity based off of this ShakeActivity and I want
I'm dealing with very big data in matlab and used to store this data
I have a data.frame in panel format (country-year) and I need to calculate the

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.