% -6ren">
gpt4 book ai didi

r - 如何为每个条件添加中位数和标准偏差

转载 作者:行者123 更新时间:2023-12-05 03:25:02 25 4
gpt4 key购买 nike

我的数据是这样的

df<-structure(list(Data = c("P718", "Trans17", "Ham8Y"), Rep1_Condition = c(30100000, 
1.11e+10, 2.05e+09), Rep2_Condition = c(37700000, 1.3e+10, 3.52e+09
), Rep3_Condition = c(4.27e+10, 0, 1.75e+10), Rep1_H1 = c(4.49e+08,
1.28e+11, 5.5e+10), Rep2_H1 = c(4.31e+08, 1.5e+11, 6.38e+10),
Rep3_H1 = c(3.89e+10, 0, 1.89e+10), Rep1_H2 = c(4.07e+08,
1.24e+11, 4.41e+10), Rep2_H2 = c(1.98e+08, 8.21e+10, 3.14e+10
), Rep3_H2 = c(1.75e+10, 0, 1.46e+09)), class = "data.frame", row.names = c(NA,
-3L))

例如,我想为每 3 个重复添加中值和 SD

P718  3.01e+07  3.77e+07  4.27e+10

值可以在旁边

类似这样的结构

out<- structure(list(Data = c("P718", "Trans17", "Ham8Y"), Rep1_Condition = c(30100000, 
1.11e+10, 2.05e+09), Rep2_Condition = c(37700000, 1.3e+10, 3.52e+09
), Rep3_Condition = c(4.27e+10, 0, 1.75e+10), Condition_median = c(NA,
NA, NA), Condition_SD = c(NA, NA, NA), Rep1_H1 = c(4.49e+08,
1.28e+11, 5.5e+10), Rep2_H1 = c(4.31e+08, 1.5e+11, 6.38e+10),
Rep3_H1 = c(3.89e+10, 0, 1.89e+10), H1_Median = c(NA, NA,
NA), H1_SD = c(NA, NA, NA), Rep1_H2 = c(4.07e+08, 1.24e+11,
4.41e+10), Rep2_H2 = c(1.98e+08, 8.21e+10, 3.14e+10), Rep3_H2 = c(1.75e+10,
0, 1.46e+09), H2_Median = c(NA, NA, NA), H2_SD = c(NA, NA,
NA)), class = "data.frame", row.names = c(NA, -3L))

最佳答案

一个选项是使用 pivot_longer reshape 为“长”格式,排除“Data”列,然后按“Data”分组,mutate numeric 列创建两个额外的列 medianSD(因为有多个列,当我们在 named 中应用函数时,这将作为后缀名返回 列表),最后,使用 pivot_wider (

library(dplyr)
library(tidyr)
out2 <- df %>%
pivot_longer(cols = -Data, names_to = c("grp", ".value"),
names_sep = "_") %>%
group_by(Data) %>%
mutate(across(where(is.numeric),
list(median= ~ median(.x), SD = ~ sd(.x)))) %>%
ungroup %>%
pivot_wider(names_from = grp, values_from = Condition:H2)

-检查

> out2$Condition_median
[1] 3.77e+07 1.11e+10 3.52e+09
> matrixStats::rowMedians(as.matrix(df[2:4]))
[1] 3.77e+07 1.11e+10 3.52e+09

-输出

> out2
# A tibble: 3 × 16
Data Condition_median Condition_SD H1_median H1_SD H2_median H2_SD Condition_Rep1 Condition_Rep2 Condition_Rep3 H1_Rep1 H1_Rep2 H1_Rep3
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 P718 37700000 24633284613. 449000000 2.22e10 4.07e 8 9.93e 9 30100000 37700000 42700000000 4.49e 8 4.31e 8 3.89e10
2 Trans17 11100000000 7021633238. 128000000000 8.10e10 8.21e10 6.31e10 11100000000 13000000000 0 1.28e11 1.5 e11 0
3 Ham8Y 3520000000 8527443931. 55000000000 2.38e10 3.14e10 2.19e10 2050000000 3520000000 17500000000 5.5 e10 6.38e10 1.89e10
# … with 3 more variables: H2_Rep1 <dbl>, H2_Rep2 <dbl>, H2_Rep3 <dbl>

关于r - 如何为每个条件添加中位数和标准偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72131583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com