Calculate a mean, by a condition, within a factor [r]

alex

I'm looking to calculate the simple mean of an outcome variable, but only for the outcome associated with the maximal instance of another running variable, grouped by factors.

Of course, the calculated statistic could be substituted for any other function, and the evaluation within the group could be any other function.

library(data.table) #1.9.5
dt <- data.table(name   = rep(LETTERS[1:7], each = 3),
                 target = rep(c(0,1,2), 7),
                 filter = 1:21) 
dt

##    name target filter
## 1:    A      0      1
## 2:    A      1      2
## 3:    A      2      3
## 4:    B      0      4
## 5:    B      1      5
## 6:    B      2      6
## 7:    C      0      7

With this frame, the desired output should return a mean value for target that meets the criteria of exactly 2.

Something like:

dt[ , .(mFilter = which.max(filter),
        target = target), by = name][ , 
      mean(target), by = c("name", "mFilter")]

... seems close, but isn't hitting it quite right.

The solution should return:

##    name   V1 
## 1:    A    2
## 2:    B    2
## 3:  ...
David Robinson

You could do this with:

dt[, .(meantarget = mean(target[filter == max(filter)])), by = name]
#    name meantarget
# 1:    A      2
# 2:    B      2
# 3:    C      2
# 4:    D      2
# 5:    E      2
# 6:    F      2
# 7:    G      2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate Mean of Multiply Columns with Condition in R

From Dev

R column mean by factor

From Dev

How to calculate mean value in R summarize statement based on a condition?

From Dev

R: do calculation for each factor level separately, then calculate min/mean/max over levels

From Dev

Calculate mean for subgroup within group

From Dev

Calculate row mean with condition in dplyr

From Dev

R create factor based on condition

From Dev

R - Calculate mean of rasterbrick

From Dev

Running mean with condition in R?

From Dev

R: How to calculate mean/sd within a group, always adding one row by row

From Dev

Calculate the mean difference within and between groups

From Dev

Calculate mean based on range within a cell

From Dev

Calculate the mean of a nested group if the condition holds

From Dev

awk - calculate the mean age if condition is met

From Dev

R: mean of all cases with a certain factor level

From Dev

Calculate confidence interval for factor levels in r

From Dev

R: replacing <NA> within factor variables as 0

From Dev

Extract the rest of the rows within a group / factor in R

From Dev

R: calculate the row mean in a matrix

From Dev

Calculate a lag or lead mean in r

From Dev

Calculate min, maximum and mean in R

From Dev

Insert rows in R based on a factor condition

From Dev

Assign factor for row that meets condition in R

From Dev

Rename factor levels based on condition in R

From Dev

Rename factor levels based on a condition in R

From Dev

How to recode factor based on condition in r

From Dev

"mean" within "by" throwing warning in R

From Dev

Calculating the mean within clusters in r

From Dev

Calculate percentage within a subgroup in R

Related Related

HotTag

Archive