-
Notifications
You must be signed in to change notification settings - Fork 2
/
05C_calcRecip.R
25 lines (23 loc) · 1.15 KB
/
05C_calcRecip.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ------------------------------------------------------------------------------
# Function to calculate reciprocal effects by environment
# Reference: B. Griffing (1956) Aust. J. Biol. Sci. 9:463-493
# S. Turner
# 20 February 2017
# ------------------------------------------------------------------------------
# inputs:
# trait = name of column with phenotype
# .id = observation # (from mice mids object) (factor)
# year = name of column with location/year information (factor)
# cross = column with hybrid (jk = kj) (factor)
# recip = column with hybrid (jk != kj) (ordered by male and female parent)
# data = name of dataframe where info is stored
calcRecip <- function(trait, .id, year, cross, recip, data) {
df <- data.frame(trait = data[, trait], .id = data[, .id], year = data[, year],
cross = data[, cross], recip = data[, recip])
# means for directed crosses
r_ijbar <- aggregate(trait ~ cross:year, data = df, FUN = mean, na.action = na.pass)
colnames(r_ijbar) <- c("recip", "year", "r_ji")
df <- merge(df, r_ijbar, by = c("recip", "year"))
newdf <- merge(data, df[, c(".id", "r_ji")], by = ".id")
return(newdf)
}