我想将variable
转换为z分数。如何使用循环分别为每个因子cell
级别执行此操作?
示例数据:
df = data.frame(Cell = c(rep("13a",5),rep("1b",5),rep("5b",5)),
condition = rep(c("a","b","c","d","e"),3),
variable = c(58,55,36,29,53,57,53,54,52,52,45,49,48,46,45))
这是一个好的开始吗?...也许不需要循环,我想学习如何编写循环...
# Final data frame containing the results of all loops
df_z = data.frame()
# Loop through by cell
for (i in 1:unique(df$Cell)) {
df_z$myZ <- scale(variable)
}
可以通过group_by
操作来完成
library(dplyr)
df %>%
group_by(Cell) %>%
mutate(myZ = as.numeric(scale(variable)))
或带有data.table
library(data.table)
setDT(df)[, myZ := as.numeric(scale(variable)), by = Cell][]
在for
循环的情况下,我们可以在每次迭代中将其作为子集,并将scale
d值分配给创建的'myZ'变量
un1 <- unique(df$Cell)
df$myZ <- NA
for(un in un1) {
i1 <- df$Cell == un
df$myZ[i1] <- as.numeric(scale(df$variable[i1]))
}
或带有split
df$myZ <- unsplit(lapply(split(df$variable, df$Cell), scale), df$Cell)
我们可以在基数R中使用ave
:
df$myZ <- with(df, ave(variable, Cell, FUN = scale))
df
# Cell condition variable myZ
#1 13a a 58 0.917
#2 13a b 55 0.684
#3 13a c 36 -0.792
#4 13a d 29 -1.336
#5 13a e 53 0.528
#6 1b a 57 1.640
#7 1b b 53 -0.289
#8 1b c 54 0.193
#9 1b d 52 -0.772
#10 1b e 52 -0.772
#11 5b a 45 -0.881
#12 5b b 49 1.321
#13 5b c 48 0.771
#14 5b d 46 -0.330
#15 5b e 45 -0.881