Reputation: 31
I have timestamp of data look like this:
(col V1 is a year, V2 is day of year and V2 is time every 30 minutes).
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
1 2009 1 0 NA NA 112.67702 98.59882 -7.290186 0.3382428 0.14929900 6.668614
2 2009 1 30 NA NA 79.56672 74.99058 -7.356445 0.3258001 0.13855380 6.345498
3 2009 1 100 NA NA 151.99107 129.30848 -7.400850 0.3475700 0.15769250 6.771588
4 2009 1 130 NA NA 85.98330 69.35902 -7.577969 0.2079333 0.05647759 6.148865
5 2009 1 200 NA NA 81.63526 69.48286 -7.587354 0.3306208 0.14272100 5.249530
6 2009 1 230 NA NA 103.38915 90.44430 -7.500925 0.2955433 0.11396920 5.321435
V16 V17 V18 V19
1 -0.000167956 253.9534 0 NA
2 -0.000170257 256.4038 0 NA
3 -0.000112737 239.7196 0 NA
4 -0.000172556 223.8581 0 NA
5 -0.000138045 206.6773 0 NA
6 -0.000144948 201.8427 0 NA
and I want to average temperature which is column V19 which has a lot of NA values. What I did it does not seem correct?
options(stringsAsFactors = FALSE)
stannardrock <-read.table("~/Documents/USstandardrock/stannardrock_2009_14noice.txt", quote="\"", comment.char="", na.strings="-9999",header=F)
head(stannardrock)
stdir <- stannardrock
stdir$Date <-seq(as.POSIXct("2009-01-01 00:00"), as.POSIXct("2014-12-31 23:30"),by = 1800)
stdir[which(stdir < 0)] <- -9999
stdir$dateday <- cut(as.POSIXct(paste(stdir$V1,stdir$V2),format = "%Y%d"),breaks="day")
head(stdir)
means <- aggregate(stdir$V19 ~ dateday, stdir, mean)
head(means)
print(means
Upvotes: 0
Views: 87
Reputation: 19544
First, if V2 is day of the year, you should use %j
instead of %d
:
stdir$dateday <- cut(as.POSIXct(paste(stdir$V1,stdir$V2),format = "%Y%j"),breaks="day")
Then I think you should try something like :
means <- aggregate(V19 ~ dateday, stdir, mean, na.rm=TRUE)
to avoid NAs
in your mean
Upvotes: 1