规范化每组变量

时间:2014-03-09 21:33:55

标签: r

鉴于我有一些数据,它被分组两次(一次由What组成,一次由Who组成):

data <-read.table(header = TRUE, text="What  Who    Time
aaa Alice   17
aaa Bob 20
aaa Carol   16
bbb Alice   23
bbb Bob 22
bbb Carol   19
ccc Alice   19
ccc Bob 19
ccc Carol   23
")

如何将组内的一个变量(Time)(例如what)规范化为其他组成员之一(例如Alice Who) ?

  What   Who Time               What   Who Time                What   Who  Time
1  aaa Alice   17             1  aaa Alice   17/17           1  aaa Alice     1
2  aaa   Bob   20             2  aaa   Bob   20/17           2  aaa   Bob 1.176
3  aaa Carol   16       \     3  aaa Carol   16/17      \    3  aaa Carol 0.941
4  bbb Alice   23    ----\    4  bbb Alice   23/23   ----\   4  bbb Alice     1
5  bbb   Bob   22    ----/    5  bbb   Bob   22/23   ----/   5  bbb   Bob 0.957
6  bbb Carol   19       /     6  bbb Carol   19/23      /    6  bbb Carol 0.826
7  ccc Alice   19             7  ccc Alice   19/19           7  ccc Alice     1
8  ccc   Bob   19             8  ccc   Bob   19/19           8  ccc   Bob     1
9  ccc Carol   23             9  ccc Carol   23/19           9  ccc Carol 1.211

编辑:根据@ amit的回答,我想出了以下功能

normalizeTo <- function(df, supergroup, group, val, var, vars=c(var)) {
  data <- df
  indexes <- which(df[[group]] == val)
  norm.factor <- (df[[var]])[indexes]
  names(norm.factor) <- (df[[supergroup]])[indexes]
  for (normvar in vars) {
    data[[paste0(normvar,".norm")]] <- df[[normvar]]/norm.factor[ df[[supergroup]] ]
  }
  data
}

1 个答案:

答案 0 :(得分:1)

这样可行:

a = which(data$Who=="Alice")
norm.factor = data$Time[a]
names(norm.factor) = data$What[a]
data$norm.time = data$Time/norm.factor[data$What]