Home Aggregate and NA values
Reply: 0

Aggregate and NA values

user1064 Published in April 25, 2018, 12:34 pm

This question already has an answer here:

  • Calculate mean across rows with NA values in R 2 answers

I've got a dataframe with 1000 observations.
For each observation I got five variables. Now I'd like to create a new variable which is an aggregation from those 5 variables.
I typed the following:

df$aggr_variable <- (1/5)*(var1+var2+var3+var4+var5)

I then got the new aggregated variable, but also a problem. If let's say observation 839 got a missing value NA in var2, but still values for the other four variables, it gives me NA in the aggregated variable.

How can I leave the NA's of the five variables out without having to leave out the whole observation when one variable contains an NA?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.454971 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO