about calculating row and columns in r -
i want ask how calculate mean value specific rows , columns? example, dataframe this:
df: precip tmax tmin wind date year month day 1.75 18.38 6.29 2.06 1952-05-26 1952 5 26 2.12 14.45 1.97 3.50 1952-05-27 1952 5 27 0.00 18.98 1.95 2.82 1952-05-28 1952 5 28 0.00 24.22 5.19 4.12 1952-05-29 1952 5 29 2.00 21.66 6.41 1.90 1952-05-30 1952 5 30 35.38 18.79 5.34 3.13 1952-05-31 1952 5 31 0.62 22.64 6.79 3.50 1952-06-01 1952 6 1 2.30 24.58 9.98 2.07 1952-06-02 1952 6 2
(1)how calculate mean tmax specific month, such in may? dataset 1950 until 2000. used code:
df_jul = apply(df[,'month'==5],2,mean,na.rm=t)
but result is: numeric(0)
if in way:
df_jul.entry = which(df[,7]==5) df.tmax = apply(df[c(df_jul.entry),2],2,mean,na.rm=t)
it throws error: dim(x) must have positive length can't check out problem.
(2) how calculate yearly precip? say, add precip each year. help.
for first question,
mean(df[df$month==5,'tmax'])
and second either
aggregate(precip~year, df, mean)
or
library(data.table) setdt(df)[, list(precip= mean(precip)), by=year]
or
library(dplyr) df %>% group_by(year) %>% summarise(precip=mean(precip))
regarding code
df[, 'month'] #[1] 5 5 5 5 5 5 6 6
but,
df[, 'month'==5] #data frame 0 columns , 8 rows
it can be
df[, 'month']==5 #[1] true true true true true true false false
which may used row index
df[df[, 'month']==5,]
and specifying column tmax
, elements of column vector.
df[df[, 'month']==5,'tmax'] #[1] 18.38 14.45 18.98 24.22 21.66 18.79 mean(df[df[, 'month']==5,'tmax']) #[1] 19.41333
by default, when use [
, default drop=true
, if there single column,it drop dimensions become vector. apply
won't work, can change drop=false
, use apply
. but, using apply
single column not necessary.
apply(df[df[, 'month']==5,'tmax',drop=false], 2, mean) # tmax #19.41333
it same problem second code
df_jul.entry <- which(df[,7]==5) df[c(df_jul.entry),2]#become vector #[1] 18.38 14.45 18.98 24.22 21.66 18.79
which can avoided drop=false
Comments
Post a Comment