我正在使用看起来与此类似的数据框
Ind Pos Sample Ct LogConc RelConc
1 B1 wt1A 26.93 -2.0247878 0.009445223
2 B2 wt1A 27.14 -2.0960951 0.008015026
3 B3 wt1B 26.76 -1.9670628 0.010787907
4 B4 wt1B 26.94 -2.0281834 0.009371662
5 B5 wt1C 26.01 -1.7123939 0.019391264
6 B6 wt1C 26.08 -1.7361630 0.018358492
7 B7 wt1D 25.68 -1.6003396 0.025099232
8 B8 wt1D 25.75 -1.6241087 0.023762457
9 B9 wt1E 22.11 -0.3881154 0.409151879
10 B10 wt1E 22.21 -0.4220713 0.378380453
11 B11 dko1A 22.20 -0.4186757 0.381350463
12 B12 dko1A 22.10 -0.3847199 0.412363423
我的目标是计算RelConc的样本均值,这将导致数据帧看起来像这样。
Ind Pos Sample Ct LogConc RelConc AverageRelConc
1 B1 wt1A 26.93 -2.0247878 0.009445223 0.008730124
2 B2 wt1A 27.14 -2.0960951 0.008015026 0.008730124
3 B3 wt1B 26.76 -1.9670628 0.010787907 0.010079785
4 B4 wt1B 26.94 -2.0281834 0.009371662 0.010079785
5 B5 wt1C 26.01 -1.7123939 0.019391264 0.018874878
6 B6 wt1C 26.08 -1.7361630 0.018358492 0.018874878
7 B7 wt1D 25.68 -1.6003396 0.025099232 0.024430845
8 B8 wt1D 25.75 -1.6241087 0.023762457 0.024430845
9 B9 wt1E 22.11 -0.3881154 0.409151879 0.393766166
10 B10 wt1E 22.21 -0.4220713 0.378380453 0.393766166
11 B11 dko1A 22.20 -0.4186757 0.381350463 0.396856943
12 B12 dko1A 22.10 -0.3847199 0.412363423 0.396856943
我对R相当陌生,不知道如何完成这种看似简单的任务。在python中,我可能会遍历每一行并检查是否遇到了新的样品名称,然后计算上述所有样品的平均值。但是,这似乎不太像“ R like”。 如果有人可以指出解决方案,我将非常高兴!
干杯!
答案 0 :(得分:0)
在base R
中,我们可以使用ave
,它非常快
df1$AverageRelConc <- with(df1, ave(RelConc, Sample))
-输出
df1$AverageRelConc
#[1] 0.008730125 0.008730125 0.010079784 0.010079784 0.018874878 0.018874878 0.024430844 0.024430844 0.393766166 0.393766166
#[11] 0.396856943 0.396856943
或者使用tidyverse
,我们将'Sample'分组,并获得'RelConc'的mean
library(dplyr)
df1 %>%
group_by(Sample) %>%
mutate(AverageRelConc = mean(RelConc, na.rm = TRUE))
-输出
# A tibble: 12 x 7
# Groups: Sample [6]
# Ind Pos Sample Ct LogConc RelConc AverageRelConc
# <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
# 1 1 B1 wt1A 26.9 -2.02 0.00945 0.00873
# 2 2 B2 wt1A 27.1 -2.10 0.00802 0.00873
# 3 3 B3 wt1B 26.8 -1.97 0.0108 0.0101
# 4 4 B4 wt1B 26.9 -2.03 0.00937 0.0101
# 5 5 B5 wt1C 26.0 -1.71 0.0194 0.0189
# 6 6 B6 wt1C 26.1 -1.74 0.0184 0.0189
# 7 7 B7 wt1D 25.7 -1.60 0.0251 0.0244
# 8 8 B8 wt1D 25.8 -1.62 0.0238 0.0244
# 9 9 B9 wt1E 22.1 -0.388 0.409 0.394
#10 10 B10 wt1E 22.2 -0.422 0.378 0.394
#11 11 B11 dko1A 22.2 -0.419 0.381 0.397
#12 12 B12 dko1A 22.1 -0.385 0.412 0.397
df1 <- structure(list(Ind = 1:12, Pos = c("B1", "B2", "B3", "B4", "B5",
"B6", "B7", "B8", "B9", "B10", "B11", "B12"), Sample = c("wt1A",
"wt1A", "wt1B", "wt1B", "wt1C", "wt1C", "wt1D", "wt1D", "wt1E",
"wt1E", "dko1A", "dko1A"), Ct = c(26.93, 27.14, 26.76, 26.94,
26.01, 26.08, 25.68, 25.75, 22.11, 22.21, 22.2, 22.1), LogConc = c(-2.0247878,
-2.0960951, -1.9670628, -2.0281834, -1.7123939, -1.736163, -1.6003396,
-1.6241087, -0.3881154, -0.4220713, -0.4186757, -0.3847199),
RelConc = c(0.009445223, 0.008015026, 0.010787907, 0.009371662,
0.019391264, 0.018358492, 0.025099232, 0.023762457, 0.409151879,
0.378380453, 0.381350463, 0.412363423)), class = "data.frame",
row.names = c(NA,
-12L))
答案 1 :(得分:-1)
尝试使用此void MainWindow::edithelp()
{
QListWidgetItem* item_1 = ui->xml_scripts_textbox->takeItem(ui->xml_scripts_textbox->currentRow());
item_1->setFlags(Qt::ItemIsEditable); // still not getting editable ?? why ??
}
选项:
tidyverse
输出:
library(tidyverse)
#Code
df %>% group_by(Sample) %>%
mutate(AvgRelConc=mean(RelConc,na.rm=T))
使用了一些数据:
# A tibble: 12 x 7
# Groups: Sample [6]
Ind Pos Sample Ct LogConc RelConc AvgRelConc
<int> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 B1 wt1A 26.9 -2.02 0.00945 0.00873
2 2 B2 wt1A 27.1 -2.10 0.00802 0.00873
3 3 B3 wt1B 26.8 -1.97 0.0108 0.0101
4 4 B4 wt1B 26.9 -2.03 0.00937 0.0101
5 5 B5 wt1C 26.0 -1.71 0.0194 0.0189
6 6 B6 wt1C 26.1 -1.74 0.0184 0.0189
7 7 B7 wt1D 25.7 -1.60 0.0251 0.0244
8 8 B8 wt1D 25.8 -1.62 0.0238 0.0244
9 9 B9 wt1E 22.1 -0.388 0.409 0.394
10 10 B10 wt1E 22.2 -0.422 0.378 0.394
11 11 B11 dko1A 22.2 -0.419 0.381 0.397
12 12 B12 dko1A 22.1 -0.385 0.412 0.397
或者您可以使用#Data
df <- structure(list(Ind = 1:12, Pos = c("B1", "B2", "B3", "B4", "B5",
"B6", "B7", "B8", "B9", "B10", "B11", "B12"), Sample = c("wt1A",
"wt1A", "wt1B", "wt1B", "wt1C", "wt1C", "wt1D", "wt1D", "wt1E",
"wt1E", "dko1A", "dko1A"), Ct = c(26.93, 27.14, 26.76, 26.94,
26.01, 26.08, 25.68, 25.75, 22.11, 22.21, 22.2, 22.1), LogConc = c(-2.0247878,
-2.0960951, -1.9670628, -2.0281834, -1.7123939, -1.736163, -1.6003396,
-1.6241087, -0.3881154, -0.4220713, -0.4186757, -0.3847199),
RelConc = c(0.009445223, 0.008015026, 0.010787907, 0.009371662,
0.019391264, 0.018358492, 0.025099232, 0.023762457, 0.409151879,
0.378380453, 0.381350463, 0.412363423)), class = "data.frame", row.names = c(NA,
-12L))
并将结果保存在其他数据框中,然后可以与原始aggregate()
合并:
df
输出:
#Compute means
dfmeans <- aggregate(RelConc~Sample,df,mean,na.rm=T)
#Now match
df$AvgRelConc <- dfmeans[match(df$Sample,dfmeans$Sample),"RelConc"]