mercredi 29 juin 2016

How to calculate means from data frame in R for a variable with specific amount of NAs or not NAs at all?

So I have lots of data in the form (4 values for each day)

         date   var1      var2 
1  2003-10-28    1.2       970     
2  2003-10-28     NA       510     
3  2003-10-28     NA       640     
4  2003-10-28     NA       730     
5  2003-10-30    2.0       570     
6  2003-10-30     NA       480     
7  2003-10-30    1.2       580     
8  2003-10-30    1.2       297     
9  2002-05-07    3.0       830     
10 2002-05-07    4.8       507     
11 2002-05-07    4.8       253     
12 2002-05-07     NA       798    

and I need to calculate sums for var1 for every day, IF there is for example less than 2 NA values (or none) for that specific date and otherwise that date should be ignored. At the same time I should calculate means of var2 for the same dates, IF the sum for var1 was also calculated. Then I should save those means, sums and dates to another data frame so that those ignored dates aren't there.

I have tried all kinds of loop structures, but I get confused by the fact that mean and sum have to be calculated for the dates where there are not NAs at all. Also saving the dates, means and sums gets me into difficulties because I have no idea how to do the indexing properly.

Aucun commentaire:

Enregistrer un commentaire