我有一个具有以下结构的R数据框
Iterations Subset EqIT lSBR rSBR contrast_rl contrast_rb contrast_lb noise_r noise_l noise_bg
1 2 10 20 14.26 10.82 0.24 0.82 0.85 0.78 0.66 1.16
2 3 10 30 14.15 10.84 0.25 0.83 0.86 0.82 0.67 1.28
3 4 10 40 13.93 10.73 0.25 0.83 0.86 0.83 0.68 1.33
4 5 10 50 13.85 10.65 0.25 0.83 0.86 0.83 0.69 1.39
5 6 10 60 13.84 10.68 0.25 0.83 0.86 0.83 0.69 1.39
6 7 10 70 13.68 10.54 0.25 0.83 0.86 0.83 0.70 1.39
我想将lSBR和rSBR的值分配给变量l_norm和r_norm,然后通过这些值归一化所有其他lSBR和rSBR值。我不确定最好的方法。我目前的繁琐方法是
subs <- subset(df,Subset==20 & EqIT==200)
l_norm <- subs[1,4]
r_norm <- subs[1,5]
df <- transform(df, lSBR = lSBR / l_norm, rSBR = rSBR/r_norm)
有人可以给我一些关于在R中处理这个简单任务的更好方法的指导,因为我仍然在努力学习基础知识。
数据框架结构如下:
structure(list(Iterations = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L), Subset = c(10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), EqIT = c(20L, 30L, 40L,
50L, 60L, 70L, 80L, 90L, 100L, 110L, 120L, 130L, 140L, 150L,
160L, 170L, 180L, 190L, 200L, 20L, 40L, 60L, 80L, 100L, 120L,
140L, 160L, 180L, 200L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L,
50L, 55L, 60L, 65L), lSBR = c(14.26, 14.15, 13.93, 13.85, 13.84,
13.68, 13.78, 13.76, 13.71, 13.71, 13.59, 13.58, 13.7, 13.6,
13.57, 13.53, 13.57, 13.58, 13.56, 14.17, 13.66, 13.4, 13.35,
13.3, 13.3, 13.29, 13.26, 13.29, 13.35, 14.62, 14.64, 14.58,
14.51, 14.41, 14.35, 14.3, 14.25, 14.19, 14.11, 14.06, 14.07),
rSBR = c(10.82, 10.84, 10.73, 10.65, 10.68, 10.54, 10.65,
10.61, 10.56, 10.56, 10.44, 10.43, 10.54, 10.41, 10.4, 10.4,
10.4, 10.41, 10.39, 11.03, 10.73, 10.54, 10.49, 10.43, 10.45,
10.43, 10.42, 10.42, 10.49, 10.78, 10.9, 10.87, 10.89, 10.89,
10.87, 10.83, 10.8, 10.78, 10.74, 10.69, 10.65), contrast_rl = c(0.24,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.25, 0.25, 0.24, 0.23, 0.23, 0.23, 0.23, 0.28, 0.27,
0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26
), contrast_rb = c(0.82, 0.83, 0.83, 0.83, 0.83, 0.83, 0.83,
0.83, 0.83, 0.83, 0.83, 0.83, 0.83, 0.82, 0.82, 0.82, 0.82,
0.82, 0.82, 0.81, 0.82, 0.82, 0.82, 0.82, 0.82, 0.82, 0.82,
0.82, 0.82, 0.81, 0.82, 0.82, 0.83, 0.83, 0.83, 0.83, 0.83,
0.83, 0.83, 0.83, 0.83), contrast_lb = c(0.85, 0.86, 0.86,
0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86,
0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.86, 0.86, 0.86,
0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.85, 0.86, 0.85, 0.86,
0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86, 0.86), noise_r = c(0.78,
0.82, 0.83, 0.83, 0.83, 0.83, 0.83, 0.84, 0.84, 0.84, 0.84,
0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.84, 0.78, 0.81,
0.82, 0.84, 0.84, 0.84, 0.83, 0.83, 0.83, 0.84, 0.73, 0.77,
0.79, 0.8, 0.81, 0.82, 0.82, 0.83, 0.83, 0.83, 0.84, 0.84
), noise_l = c(0.66, 0.67, 0.68, 0.69, 0.69, 0.7, 0.7, 0.7,
0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71,
0.71, 0.67, 0.69, 0.7, 0.7, 0.71, 0.71, 0.71, 0.71, 0.71,
0.72, 0.6, 0.63, 0.65, 0.66, 0.67, 0.68, 0.68, 0.69, 0.69,
0.69, 0.69, 0.69), noise_bg = c(1.16, 1.28, 1.33, 1.39, 1.39,
1.39, 1.44, 1.44, 1.44, 1.44, 1.44, 1.44, 1.44, 1.37, 1.37,
1.37, 1.37, 1.37, 1.37, 1.16, 1.33, 1.39, 1.44, 1.44, 1.44,
1.44, 1.5, 1.5, 1.5, 0.9, 1.05, 1.11, 1.22, 1.28, 1.33, 1.33,
1.33, 1.39, 1.39, 1.39, 1.39)), .Names = c("Iterations",
"Subset", "EqIT", "lSBR", "rSBR", "contrast_rl", "contrast_rb",
"contrast_lb", "noise_r", "noise_l", "noise_bg"), row.names = c(NA,
-41L), class = "data.frame")
答案 0 :(得分:2)
你的方法:
subs <- subset(df,Subset==20 & EqIT==200)
l_norm <- subs[1,4]
r_norm <- subs[1,5]
df2 <- transform(df, lSBR = lSBR / l_norm, rSBR = rSBR/r_norm)
这对我来说似乎并不那么糟糕,但你可以用两行代替它 四个如下:
subline <- with(df,which(Subset==20 & EqIT==200))
## OR subline <- which(df$Subset==20 & df$EqIT==200)
df3 <- transform(df, lSBR = lSBR/lSBR[subline], rSBR=rSBR/rSBR[subline])
all.equal(df2,df3) ## TRUE