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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:19:54+00:00 2026-06-10T10:19:54+00:00

I have a data frame with a large number of variables. I am creating

  • 0

I have a data frame with a large number of variables. I am creating new variables by adding together some of the old ones. The code I am using to do so is:

name_of_data_frame<- transform(name_of_data_frame, new_variable=var1+var2 +....)

When transform comes across a NA in one of the observations, it returns “NA” in the new variable, even if some of the other variables it was adding were not NA.

e.g. if var1= 4, var2=3, var3=NA, then using transform, if I did var1+var2+var3 it would give out NA, whereas I would like it to give me 7.

I don’t want to recode my NAs to zero within the data frame, as I may need to refer back to the NAs later, so don’t want to confuse the NAs with the observations which were genuinely 0.

Any help on how to get around R treating NAs in the way described above with the transform function would be great (or if there are alternative functions to use, that would be great also).

Please note that I am not always just summing variables that are next to each other, I am also often dividing variables, multiplying, subtracting etc.

  • 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-10T10:19:56+00:00Added an answer on June 10, 2026 at 10:19 am

    My first instinct was to suggest to use sum() since then you can use the na.rm argument. However, this doesn’t work, since sum() reduces it arguments to a single scalar value, not a vector.

    This means you need to write a parallel sum function. Let’s call this psum(), similar to the base R function pmin() or pmax():

    psum <- function(..., na.rm=FALSE) { 
      x <- list(...)
      rowSums(matrix(unlist(x), ncol=length(x)), na.rm=na.rm)
    } 
    

    Now set up some data and use psum() to get the desired vector:

    dat <- data.frame(
      x = c(1,2,3, NA),
      y = c(NA, 4, 5, NA))
    
    transform(dat, new=psum(x, y, na.rm=TRUE))
       x  y new
    1  1 NA   1
    2  2  4   6
    3  3  5   8
    4 NA NA   0
    

    Similarly, you can define a parallel product, or pprod() like this:

    pprod <- function(..., na.rm=FALSE) { 
      x <- list(...)
      m <- matrix(unlist(x), ncol=length(x))
      apply(m, 1, prod, na.rm=TRUE)
    } 
    
    transform(dat, new=pprod(x, y, na.rm=TRUE))
       x  y new
    1  1 NA   1
    2  2  4   8
    3  3  5  15
    4 NA NA   1
    

    This example of pprod provides a general template for what you want to do: Create a function that uses apply() to summarize a matrix of input into the desired vector.

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

Sidebar

Related Questions

I have a large data.frame displaying some weird properties when plotted. I'd like to
I have data frame with some numerical variables and some categorical factor variables. The
I have the following type (however very large number of variables and ind) data:
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it
I have large set will a number of variables: set.seed (14) pool = sample
I have a large data frame with the following fields (example data). #dput(data) gives...
I have a large data frame (named z ) that looks like this: RPos
I'm working with a large data frame, and have run up against RAM limits.
I want to rename some random columns of a large data frame and I

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.