dataframe - Query and aggregate data based on conditions in R -


i have data frame , want mean of values of type b each year, if type a have values equal 1.

year  type   value1   value2  value3  value4  value5 1           1        1        2       3       4 1     b       10       12       9       8       10 2           1        2        2       2       1 2     b       11       10       13      9       14 

so final product looks this:

year  type_b_values 1      11 2      12.5 

which averages of value1 , value2 year1 , average of value1 , 5 year2. thanks!

here approach using base functions. i'm guessing plyr or reshape may useful packages here i'm less familiar them:

dat <- read.table(text="year  type   value1   value2  value3  value4  value5 1           1        1        2       3       4 1     b       10       12       9       8       10 2           1        2        2       2       1 2     b       11       10       13      9       14", header=true)   dat_split <- split(dat, dat$year)       # split our data list year  output <- sapply(dat_split, function(x) {     y <- x[x$type == "a", -c(1:2)] == 1 # in year = 1     z <- x[x$type == "b", -c(1:2)][y]   # grab b values = 1     if (sum(y) == 0) {                  # eliminate if no = 1         return(na)     }     mean(z) })  data.frame(year = names(output), type_b_values = output)  ## > data.frame(year = names(output), type_b_values = output) ##   year type_b_values ## 1    1          11.0 ## 2    2          12.5 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -