Remove NA when using "order"
I have this huge matrix of data with columns for year, month, day and precipitation which I need to order and also delete the row when the precipitation is NA (which happens on the day 31 of every month that only has 30 days and Februaries...). After consulting the r help files I used the following code:
dat<- aa[order(aa$year, aa$month, aa$day, na.last=NA),]
It ordered my data perfectly but I still have all the NAs... Can anyone tell me why it hasn't work?
thanks
> head(dat)
code year month station ALTITUD PROV LONGITUD LATITUD day P1 id
1.1 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 216372 402200 1 0 1
1.2 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 216372 402200 2 0 1
1.3 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 216372 402200 3 0 1
1.4 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 216372 402200 4 0 1
1.5 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 开发者_开发问答216372 402200 5 0 1
1.6 3059 1940 11 ALBALATE DE LAS NOGUERAS 855 CUENCA 216372 402200 6 0 1
The na.last
argument to order
only removes NA
from the objects passed to order
via ...
. Your NA
are in aa$precipitation
, not aa$year
, aa$month
, or aa$day
, so you need:
dat <- na.omit(aa[order(aa$year, aa$month, aa$day),])
You may want to consider using a time-series class like zoo or xts for time-series data.
Because na.last
is to see if NA
should be last of first when ordering not to remove the NA
. Use na.omit(dat)
to remove the NA
.
Hope that helps.
精彩评论