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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:47:36+00:00 2026-06-13T11:47:36+00:00

It seems that R might be missing an obvious simple function: psum . Does

  • 0

It seems that R might be missing an obvious simple function: psum. Does it exist as a different name, or is it in a package somewhere?

x = c(1,3,NA,5)
y = c(2,NA,4,1)

min(x,y,na.rm=TRUE)    # ok
[1] 1
max(x,y,na.rm=TRUE)    # ok
[1] 5
sum(x,y,na.rm=TRUE)    # ok
[1] 16

pmin(x,y,na.rm=TRUE)   # ok
[1] 1 3 4 1
pmax(x,y,na.rm=TRUE)   # ok
[1] 2 3 4 5
psum(x,y,na.rm=TRUE)
[1] 3 3 4 6                             # expected result
Error: could not find function "psum"   # actual result

I realise that + is already like psum, but what about NA?

x+y                      
[1]  3 NA NA  6        # can't supply `na.rm=TRUE` to `+`

Is there a case to add psum? Or have I missed something.

This question is a follow up from this question :
Using := in data.table to sum the values of two columns in R, ignoring NAs

  • 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-13T11:47:37+00:00Added an answer on June 13, 2026 at 11:47 am

    Following @JoshUlrich’s comment on the previous question,

    psum <- function(...,na.rm=FALSE) { 
        rowSums(do.call(cbind,list(...)),na.rm=na.rm) } 
    

    edit: from Sven Hohenstein:

    psum2 <- function(...,na.rm=FALSE) { 
        dat <- do.call(cbind,list(...))
        res <- rowSums(dat, na.rm=na.rm) 
        idx_na <- !rowSums(!is.na(dat))
        res[idx_na] <- NA
        res 
    }
    
    x = c(1,3,NA,5,NA)
    y = c(2,NA,4,1,NA)
    z = c(1,2,3,4,NA)
    
    psum(x,y,na.rm=TRUE)
    ## [1] 3 3 4 6 0
    psum2(x,y,na.rm=TRUE)
    ## [1] 3 3 4 6 NA
    
    n = 1e7
    x = sample(c(1:10,NA),n,replace=TRUE)
    y = sample(c(1:10,NA),n,replace=TRUE)
    z = sample(c(1:10,NA),n,replace=TRUE)
    
    library(rbenchmark)
    benchmark(psum(x,y,z,na.rm=TRUE),
              psum2(x,y,z,na.rm=TRUE),
              pmin(x,y,z,na.rm=TRUE), 
              pmax(x,y,z,na.rm=TRUE), replications=20)
    
    ##                          test replications elapsed relative 
    ## 4  pmax(x, y, z, na.rm = TRUE)           20  26.114    1.019 
    ## 3  pmin(x, y, z, na.rm = TRUE)           20  25.632    1.000 
    ## 2 psum2(x, y, z, na.rm = TRUE)           20 164.476    6.417
    ## 1  psum(x, y, z, na.rm = TRUE)           20  63.719    2.486
    

    Sven’s version (which arguably is the correct one) is quite a bit slower,
    although whether it matters obviously depends on the application.
    Anyone want to hack up an inline/Rcpp version?

    As for why this doesn’t exist: don’t know, but good luck getting R-core to make additions like this … I can’t offhand think of a sufficiently widespread *misc package into which this could go …

    Follow up thread by Matthew on r-devel is here (which seems to confirm) :
    r-devel: There is pmin and pmax each taking na.rm, how about psum?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to solr, so I might be missing something very obvious. It seems
From another question I recently posted, it seems that Classic ASP might not be
Reading the documentation it seems this might not be possible, but it seems that
It seems that we can show layers and even use a different zPosition for
It seems that JavaScript's Date() function can only return local date and time. Is
I might be missing something obvious here, but when trying to expose a Q_ENUM
I'm trying to write a simple java applet program, but it seems that I'm
I just stumbled about some situation that seems weird to me. I might very
The question might seem a bit confusing but I have an if statement that
So, the question might seem strange, but let's say that I compile: int main(void)

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.