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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:05+00:00 2026-06-17T16:44:05+00:00

I need to sum some columns in a data.frame with a rule that says,

  • 0

I need to sum some columns in a data.frame with a rule that says, a column is to be summed to NA if more than one observation is missing NA if only 1 or less missing it is to be summed regardless.

Say I have some data like this,

dfn <- data.frame(
a  = c(3, 3, 0, 3),
b  = c(1, NA, 0, NA),
c  = c(0, 3, NA, 1))

dfn
  a  b  c
1 3  1  0
2 3 NA  3
3 0  0 NA
4 3 NA  1

and I apply my rule, and sum the columns with less then 2 missing NA. So I get something like this.

  a  b  c
1 3  1  0
2 3 NA  3
3 0  0 NA
4 3 NA  1
5 9 NA  4

I’ve played around with colSums(dfn, na.rm = FALSE) and colSums(dfn, na.rm = TRUE). In my real data there is more then three columns and also more then 4 rows. I imagine I can count the missing some way and use that as a rule?

  • 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-17T16:44:06+00:00Added an answer on June 17, 2026 at 4:44 pm

    I don’t think you can do this with colSums alone, but you can add to its result using ifelse:

    colSums(dfn,na.rm=TRUE) + ifelse(colSums(is.na(dfn)) > 1, NA, 0)
     a  b  c 
     9 NA  4 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate sum of occurences of some data in two columns in
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
I need an aggregate sum for a table column (grouped by other columns), but
Possible Duplicate: Get sum of two columns in one LINQ query I need to
I need to find the sum of the ranks from 13 cards that a
I'm working on this website http://www.eluminatecreative.com/new/index.html and where it says sum'r fling I need
I need some help to build SQL Query. I have table having data like:
I have a bunch of columns and some columns which are values I need
I need to generate some big data from many tables, regarding filters, at there
I have some data that looks like this: +---+--------+-------------+---------------+--------------+ | | A | B

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.