R 早些时候
我认为你的出发点是正确的。接下来,您可以获取长格式的数据,按需要的方式更改列名,并以宽格式返回数据R 早些时候,r,R,我认为你的出发点是正确的。接下来,您可以获取长格式的数据,按需要的方式更改列名,并以宽格式返回数据 library(dplyr) library(tidyr) df1 %>% group_by(days_incubated = as.numeric(days_incubated)) %>% summarise(across(i.x33.031:i.x40.023, list(mean = mean, se = plotrix::std.error))) %>%
library(dplyr)
library(tidyr)
df1 %>%
group_by(days_incubated = as.numeric(days_incubated)) %>%
summarise(across(i.x33.031:i.x40.023, list(mean = mean, se = plotrix::std.error))) %>%
pivot_longer(cols = -days_incubated,
names_to = c('mass', 'col'),
names_sep = '_') %>%
#If you need column exactly as shown
mutate(col = paste(col, 'day')) %>%
pivot_wider(names_from = c(col, days_incubated), values_from = value, names_sep = ' ')
# mass `mean day 0` `se day 0` `mean day 4` `se day 4` `mean day 10` `se day 10`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 i.x3… 24.2 13.9 45.1 17.5 3.29 0.932
#2 i.x3… 0.036 0.0287 0.048 0.0116 0.014 0.00748
#3 i.x3… 0.178 0.0867 0 0 0 0
#4 i.x3… 0.02 0.0155 0 0 0 0
#5 i.x4… 0.182 0.0978 0.294 0.240 0.174 0.0941
# … with 12 more variables: `mean day 17` <dbl>, `se day 17` <dbl>, `mean day
# 24` <dbl>, `se day 24` <dbl>, `mean day 66` <dbl>, `se day 66` <dbl>, `mean day
# 81` <dbl>, `se day 81` <dbl>, `mean day 94` <dbl>, `se day 94` <dbl>, `mean day
# 116` <dbl>, `se day 116` <dbl>
库(dplyr)
图书馆(tidyr)
df1%>%
分组依据(孵育天数=as.numeric(孵育天数))%>%
总结(跨越(i.x33.031:i.x40.023,列表(平均值=平均值,se=曲线::标准误差)))%>%
pivot_更长(cols=孵化天数),
名称_to=c('mass','col'),
名称\u sep=''.''%>%
#如果您需要完全如图所示的列
突变(列=粘贴(列,'天')%>%
枢轴宽度(名称从=c(列,孵化天数),值从=value,名称从=sep=“”)
#质量`平均日0``se日0``平均日4``se日4``平均日10``se日10`
#
#1 i.x3…24.2 13.9 45.1 17.5 3.29 0.932
#2 i.x3…0.036 0.0287 0.048 0.0116 0.014 0.00748
#3 i.x3…0.178 0.0867 0 0
#4 i.x3…0.02 0.0155 0 0
#5 i.x4…0.182 0.0978 0.294 0.240 0.174 0.0941
#…还有12个变量:`mean day 17`、`se day 17`、`mean day 17`
#24`、`se day 24`、`MEAND day 66`、`se day 66`、`MEAND day
#81`、`se日81`、`se日94`、`se日94`、`se日94`、`平均日
#116`,`se第116天`
我想提供一个主要采用base R的解决方案
library(dplyr)
# Define a function that computes the standard error of a vector.
f.SE <- function(x) sd(x)/sqrt(length(x))
# Define a function that compute the standard error of a column (MARGIN = 2 for column).
f.colSE <- function(mydf) apply(mydf, MARGIN = 2, FUN = f.SE)
# Define a function that combines the means and the standard errors of a column
f.colMeanSE <- function(mycol) cbind(means = colMeans(mycol), se = f.colSE(mycol))
# Apply the function to all numeric columns in your data frame by using the days_incubated subgroup as the indices. The result is a list.
my_mean_se1 <- by(data = df1[,-1], INDICES = df1$days_incubated, FUN= f.colMeanSE); my_mean_se1
#df1$days_incubated: 0
#means se
#i.x33.031 24.182 13.91245823
#i.x35.034 0.036 0.02874022
#i.x36.017 0.178 0.08668333
#i.x39.959 0.020 0.01549193
#i.x40.023 0.182 0.09779571
#------------------------------------------------------------
# df1$days_incubated: 10
#means se
#i.x33.031 3.294 0.932119091
#i.x35.034 0.014 0.007483315
#i.x36.017 0.000 0.000000000
#i.x39.959 0.000 0.000000000
#i.x40.023 0.174 0.094106323
#------------------------------------------------------------
# Bind the columns.
my_mean_se2 <- do.call(cbind, my_mean_se1); my_mean_se2
# means se means se means se means se means se means se means se means
#i.x33.031 24.182 13.91245823 3.294 0.932119091 0.036 0.036000000 26.628 19.46476571 113.340 68.5767486 45.140 17.49416646 10.144 4.665935 13.036
#i.x35.034 0.036 0.02874022 0.014 0.007483315 0.008 0.004898979 0.060 0.04289522 0.198 0.1212601 0.048 0.01157584 0.016 0.006000 0.010
#i.x36.017 0.178 0.08668333 0.000 0.000000000 0.028 0.023323808 0.000 0.00000000 0.010 0.0100000 0.000 0.00000000 0.000 0.000000 0.000
#i.x39.959 0.020 0.01549193 0.000 0.000000000 0.006 0.004000000 0.000 0.00000000 0.008 0.0080000 0.000 0.00000000 0.002 0.002000 0.006
#i.x40.023 0.182 0.09779571 0.174 0.094106323 0.018 0.015620499 0.158 0.11939012 0.378 0.2103188 0.294 0.23972067 0.006 0.004000 0.018
# se means se
#i.x33.031 6.002108463 0.094 0.07304793
#i.x35.034 0.007745967 0.002 0.00200000
#i.x36.017 0.000000000 0.136 0.07194442
#i.x39.959 0.004000000 0.024 0.01122497
#i.x40.023 0.011135529 0.016 0.00600000
# Bind the names of the columns to the names of days_incubated
names_days <- unique(df1$days_incubated)
names_meanse <- rbind(paste0("mean day ", names_days), paste0("se day ", names_days))
colnames(my_mean_se2) <- names_meanse
# Round the numbers to 2 decimal places and convert the numeric table to a data frame.
mean_se <- as.data.frame(round(my_mean_se2, digits = 2)); mean_se
# mean day 0 se day 0 mean day 4 se day 4 mean day 10 se day 10 mean day 17
#i.x33.031 24.18 13.91 3.29 0.93 0.04 0.04 26.63
#i.x35.034 0.04 0.03 0.01 0.01 0.01 0.00 0.06
#i.x36.017 0.18 0.09 0.00 0.00 0.03 0.02 0.00
#i.x39.959 0.02 0.02 0.00 0.00 0.01 0.00 0.00
#i.x40.023 0.18 0.10 0.17 0.09 0.02 0.02 0.16
# se day 17 mean day 24 se day 24 mean day 66 se day 66 mean day 81 se day 81
#i.x33.031 19.46 113.34 68.58 45.14 17.49 10.14 4.67
#i.x35.034 0.04 0.20 0.12 0.05 0.01 0.02 0.01
#i.x36.017 0.00 0.01 0.01 0.00 0.00 0.00 0.00
#i.x39.959 0.00 0.01 0.01 0.00 0.00 0.00 0.00
#i.x40.023 0.12 0.38 0.21 0.29 0.24 0.01 0.00
# mean day 94 se day 94 mean day 116 se day 116
#i.x33.031 13.04 6.00 0.09 0.07
#i.x35.034 0.01 0.01 0.00 0.00
#i.x36.017 0.00 0.00 0.14 0.07
#i.x39.959 0.01 0.00 0.02 0.01
#i.x40.023 0.02 0.01 0.02 0.01
# Convert the data frame to a tibble with rowname set as a new column
as_tibble(mean_se, rownames = "rowname")
# A tibble: 5 x 19
# rowname `mean day 0` `se day 0` `mean day 4` `se day 4` `mean day 10` `se day 10` `mean day 17` `se day 17` `mean day 24` `se day 24`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 i.x33.~ 24.2 13.9 3.29 0.93 0.04 0.04 26.6 19.5 113. 68.6
#2 i.x35.~ 0.04 0.03 0.01 0.01 0.01 0 0.06 0.04 0.2 0.12
#3 i.x36.~ 0.18 0.09 0 0 0.03 0.02 0 0 0.01 0.01
#4 i.x39.~ 0.02 0.02 0 0 0.01 0 0 0 0.01 0.01
#5 i.x40.~ 0.18 0.1 0.17 0.09 0.02 0.02 0.16 0.12 0.38 0.21
# ... with 8 more variables: `mean day 66` <dbl>, `se day 66` <dbl>, `mean day 81` <dbl>, `se day 81` <dbl>, `mean day 94` <dbl>, `se day
# 94` <dbl>, `mean day 116` <dbl>, `se day 116` <dbl>
库(dplyr)
#定义一个计算向量标准误差的函数。
f、 SE这个代码不适合我。您是否在使用数据表中的转置()
?否则,我假设它来自purrr
,因为您加载了tidyverse
。我编辑了包,代码似乎按照您的预期运行。如果没有,请随意编辑。谢谢@Adam,它看起来很棒!列的顺序非常重要。我该如何使用relocate()
我想知道groups=“drop”)
和names\u glue=“{.value}\u day{days\u incubined}”)
该怎么做?哇,这真是太棒了!如果你还没有,可以查阅一些关于“整洁数据”(tiddata)的文献。当您能够识别数据的结构以及如何改进它时,计算通常会变得更加精简。没有理由对我的代码做任何事情。在原始代码中,检查df2\u trans
。看第二排。查看列的顺序?它是0,10,116,17,等等。稍后使用rename()
时,您会说第一列是第0天,第二列是第4天,依此类推。这是不正确的,因为数据是在某处排序的。我不相信列的标签是正确的。我想这和OP的问题是一样的,他们被分类为字符而不是数字。
library(dplyr)
library(tidyr)
df1 %>%
group_by(days_incubated = as.numeric(days_incubated)) %>%
summarise(across(i.x33.031:i.x40.023, list(mean = mean, se = plotrix::std.error))) %>%
pivot_longer(cols = -days_incubated,
names_to = c('mass', 'col'),
names_sep = '_') %>%
#If you need column exactly as shown
mutate(col = paste(col, 'day')) %>%
pivot_wider(names_from = c(col, days_incubated), values_from = value, names_sep = ' ')
# mass `mean day 0` `se day 0` `mean day 4` `se day 4` `mean day 10` `se day 10`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 i.x3… 24.2 13.9 45.1 17.5 3.29 0.932
#2 i.x3… 0.036 0.0287 0.048 0.0116 0.014 0.00748
#3 i.x3… 0.178 0.0867 0 0 0 0
#4 i.x3… 0.02 0.0155 0 0 0 0
#5 i.x4… 0.182 0.0978 0.294 0.240 0.174 0.0941
# … with 12 more variables: `mean day 17` <dbl>, `se day 17` <dbl>, `mean day
# 24` <dbl>, `se day 24` <dbl>, `mean day 66` <dbl>, `se day 66` <dbl>, `mean day
# 81` <dbl>, `se day 81` <dbl>, `mean day 94` <dbl>, `se day 94` <dbl>, `mean day
# 116` <dbl>, `se day 116` <dbl>
library(dplyr)
# Define a function that computes the standard error of a vector.
f.SE <- function(x) sd(x)/sqrt(length(x))
# Define a function that compute the standard error of a column (MARGIN = 2 for column).
f.colSE <- function(mydf) apply(mydf, MARGIN = 2, FUN = f.SE)
# Define a function that combines the means and the standard errors of a column
f.colMeanSE <- function(mycol) cbind(means = colMeans(mycol), se = f.colSE(mycol))
# Apply the function to all numeric columns in your data frame by using the days_incubated subgroup as the indices. The result is a list.
my_mean_se1 <- by(data = df1[,-1], INDICES = df1$days_incubated, FUN= f.colMeanSE); my_mean_se1
#df1$days_incubated: 0
#means se
#i.x33.031 24.182 13.91245823
#i.x35.034 0.036 0.02874022
#i.x36.017 0.178 0.08668333
#i.x39.959 0.020 0.01549193
#i.x40.023 0.182 0.09779571
#------------------------------------------------------------
# df1$days_incubated: 10
#means se
#i.x33.031 3.294 0.932119091
#i.x35.034 0.014 0.007483315
#i.x36.017 0.000 0.000000000
#i.x39.959 0.000 0.000000000
#i.x40.023 0.174 0.094106323
#------------------------------------------------------------
# Bind the columns.
my_mean_se2 <- do.call(cbind, my_mean_se1); my_mean_se2
# means se means se means se means se means se means se means se means
#i.x33.031 24.182 13.91245823 3.294 0.932119091 0.036 0.036000000 26.628 19.46476571 113.340 68.5767486 45.140 17.49416646 10.144 4.665935 13.036
#i.x35.034 0.036 0.02874022 0.014 0.007483315 0.008 0.004898979 0.060 0.04289522 0.198 0.1212601 0.048 0.01157584 0.016 0.006000 0.010
#i.x36.017 0.178 0.08668333 0.000 0.000000000 0.028 0.023323808 0.000 0.00000000 0.010 0.0100000 0.000 0.00000000 0.000 0.000000 0.000
#i.x39.959 0.020 0.01549193 0.000 0.000000000 0.006 0.004000000 0.000 0.00000000 0.008 0.0080000 0.000 0.00000000 0.002 0.002000 0.006
#i.x40.023 0.182 0.09779571 0.174 0.094106323 0.018 0.015620499 0.158 0.11939012 0.378 0.2103188 0.294 0.23972067 0.006 0.004000 0.018
# se means se
#i.x33.031 6.002108463 0.094 0.07304793
#i.x35.034 0.007745967 0.002 0.00200000
#i.x36.017 0.000000000 0.136 0.07194442
#i.x39.959 0.004000000 0.024 0.01122497
#i.x40.023 0.011135529 0.016 0.00600000
# Bind the names of the columns to the names of days_incubated
names_days <- unique(df1$days_incubated)
names_meanse <- rbind(paste0("mean day ", names_days), paste0("se day ", names_days))
colnames(my_mean_se2) <- names_meanse
# Round the numbers to 2 decimal places and convert the numeric table to a data frame.
mean_se <- as.data.frame(round(my_mean_se2, digits = 2)); mean_se
# mean day 0 se day 0 mean day 4 se day 4 mean day 10 se day 10 mean day 17
#i.x33.031 24.18 13.91 3.29 0.93 0.04 0.04 26.63
#i.x35.034 0.04 0.03 0.01 0.01 0.01 0.00 0.06
#i.x36.017 0.18 0.09 0.00 0.00 0.03 0.02 0.00
#i.x39.959 0.02 0.02 0.00 0.00 0.01 0.00 0.00
#i.x40.023 0.18 0.10 0.17 0.09 0.02 0.02 0.16
# se day 17 mean day 24 se day 24 mean day 66 se day 66 mean day 81 se day 81
#i.x33.031 19.46 113.34 68.58 45.14 17.49 10.14 4.67
#i.x35.034 0.04 0.20 0.12 0.05 0.01 0.02 0.01
#i.x36.017 0.00 0.01 0.01 0.00 0.00 0.00 0.00
#i.x39.959 0.00 0.01 0.01 0.00 0.00 0.00 0.00
#i.x40.023 0.12 0.38 0.21 0.29 0.24 0.01 0.00
# mean day 94 se day 94 mean day 116 se day 116
#i.x33.031 13.04 6.00 0.09 0.07
#i.x35.034 0.01 0.01 0.00 0.00
#i.x36.017 0.00 0.00 0.14 0.07
#i.x39.959 0.01 0.00 0.02 0.01
#i.x40.023 0.02 0.01 0.02 0.01
# Convert the data frame to a tibble with rowname set as a new column
as_tibble(mean_se, rownames = "rowname")
# A tibble: 5 x 19
# rowname `mean day 0` `se day 0` `mean day 4` `se day 4` `mean day 10` `se day 10` `mean day 17` `se day 17` `mean day 24` `se day 24`
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 i.x33.~ 24.2 13.9 3.29 0.93 0.04 0.04 26.6 19.5 113. 68.6
#2 i.x35.~ 0.04 0.03 0.01 0.01 0.01 0 0.06 0.04 0.2 0.12
#3 i.x36.~ 0.18 0.09 0 0 0.03 0.02 0 0 0.01 0.01
#4 i.x39.~ 0.02 0.02 0 0 0.01 0 0 0 0.01 0.01
#5 i.x40.~ 0.18 0.1 0.17 0.09 0.02 0.02 0.16 0.12 0.38 0.21
# ... with 8 more variables: `mean day 66` <dbl>, `se day 66` <dbl>, `mean day 81` <dbl>, `se day 81` <dbl>, `mean day 94` <dbl>, `se day
# 94` <dbl>, `mean day 116` <dbl>, `se day 116` <dbl>