如何对 tibbles 进行 运行 操作并在 tibbles 中进行 columns/values 操作,其中 tibbles 在列表中?
How to run operations on tibbles and the columns/values in the tibbles, with the tibbles being in a list?
我是 R 的新手,因此很抱歉,如果 awnser 很明显。
我正在尝试对 tibbles 及其 values/columns 执行操作,而此 tibbles 是列表的一部分。以前我会手动上传每个现在的小标题作为 data.frame(csv 数据)并在 data.frame 上手动执行操作。不幸的是,这很烦人,所以我试图同时为我的所有 data.frame 完成脚本中的所有操作。
例如,到目前为止对我有用的是将 0.7 添加到列表中每个小标题中名称为 'Temperature' 的每一列中的每个元素。我是这样做的:
for(i in seq_along(Data_List)) {Data_List[[i]]$Temperature <- Data_List[[i]]$Temperature + 0.7}
但是我现在想执行不同的任务:主要是我需要将我的 tibbles 分成序列。当我一次使用一个 data.frame 时,这就是我所做的:
df_Sitting <- df[1:12, ]
df_Standing <- df[13:26, ]
df_LigEx <- df[27:35, ]
df_VigEx <- df[36:42, ]
df_After <- df[43:54, ]
如何针对我现在拥有的所有 tibbles/data.frames 列表正确调整它?
其次,我想进行描述性统计,Pearson Correlation和Lin Correlation。此外,我创建了一个 ggplot 和一个 Bland-Altman-Plot。我是这样做的:
describe(df$Temperature)
describe(df$Temp_core)
cor.test(df)
library(epiR)
epi.ccc(df$Temp_core, df$Temperature, ci = "z-transform",
conf.level = 0.95, rep.measure = FALSE, subjectid)
mdata <- melt(df, id="Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group= variable, color = variable))+
geom_line(aes(group= variable, color = variable))
library(BlandAltmanLeh)
BlandAltman_df <- bland.altman.plot(df$Temp_core, df$Temperature, graph.sys = "ggplot2")
print(BlandAltman_df +theme(plot.title=element_text(hjust = 0.5)))
我现在想 运行 一次为整个小标题和小标题中的变量列表使用上面的所有函数,并获取所有相应的统计数据和图表,以便稍后创建 Markdown。我试过 lapply 但不知何故不起作用。
我希望我正确地提出了问题,感谢您的帮助!!
PS,这是 dput(head(df, 20))
的输出
structure(list(Time = structure(c(52465, 52525, 52585, 52645,
52705, 52765, 52825, 52885, 52945, 53005, 53065, 53125, 53185,
53245, 53305, 53365, 53425, 53485, 53545, 53605), class = c("hms",
"difftime"), units = "secs"), Temp_core = c(35.565, 36.097, 36.38,
36.591, 36.782, 36.927, 37.067, 37.149, 37.208, 37.249, 37.276,
37.296, 37.327, 37.349, 37.356, 37.376, 37.393, 37.397, 37.409,
37.432), Temperature = c(33.87, 34.52, 34.85, 35.12, 35.37, 35.59,
35.74, 35.82, 35.95, 3600, 36.06, 36.17, 36.23, 36.18, 36.16,
36.18, 36.19, 36.19, 36.37, 36.37)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
使用一些其他类型的列表在 R 中是完全可行的。首先,我建议将 seq_along
替换为 lapply
,或者因为您已经在使用 tidyverse,purrr::map
:
for(i in seq_along(Data_List)) {
Data_List[[i]]$Temperature <- Data_List[[i]]$Temperature + 0.7
}
变为:
modified_data_list <- purrr::map(Data_List, function(df){
dplyr::mutate(df, Temperature = Temperature + 0.7)
})
您可以对上述功能应用相同的原则。请注意,我在这里使用 purrr:walk
而不是 map
,因为您没有在函数中返回修改后的数据框,而是将其称为“副作用”,如情节:
library(epiR)
library(BlandAltmanLeh)
modified_data_list <- purrr::walk(Data_List, function(df){
describe(df$Temperature)
describe(df$Temp_core)
cor.test(df)
epi.ccc(df$Temp_core, df$Temperature, ci = "z-transform",
conf.level = 0.95, rep.measure = FALSE, subjectid)
mdata <- melt(df, id="Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group= variable, color = variable))+
geom_line(aes(group= variable, color = variable))
BlandAltman_df <- bland.altman.plot(df$Temp_core, df$Temperature, graph.sys = "ggplot2")
print(BlandAltman_df +theme(plot.title=element_text(hjust = 0.5)))
})
您可以 lapply
列表成员的测试和绘图代码以及 return 测试结果和绘图列表。类似于以下内容。
library(ggplot2)
library(epiR)
library(BlandAltmanLeh)
Data_List <- lapply(Data_List, \(X){
X[["Temperature"]] <- X[["Temperature"]] + 0.7
X
})
cor_test_list <- lapply(Data_List, \(X) cor.test(formula = ~ Temperature + Temp_core, data = X))
lin_test_list <- lapply(Data_List, \(X){
epi.ccc(
X[["Temp_core"]],
X[["Temperature"]],
ci = "z-transform",
conf.level = 0.95,
rep.measure = FALSE
)
})
gg_plot_list <- lapply(Data_List, \(X){
mdata <- reshape2::melt(X, id = "Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group = variable, color = variable))+
geom_line(aes(group= variable, color = variable))
})
BlandAltman_List <- lapply(Data_List, \(X){
BlandAltman_df <- bland.altman.plot(X$Temp_core, X$Temperature, graph.sys = "ggplot2")
BlandAltman_df +
theme(plot.title = element_text(hjust = 0.5))
})
测试
要访问测试结果,请再次使用 *apply
循环和提取函数。
sapply(cor_test_list, "[[", "estimate")
# df_a.cor df_b.cor df_c.cor
#0.7425467 0.5259107 0.4572278
sapply(cor_test_list, "[[", "statistic")
# df_a.t df_b.t df_c.t
#7.680738 4.283887 3.561892
sapply(cor_test_list, "[[", "p.value")
# df_a df_b df_c
#6.709843e-10 8.771860e-05 8.434625e-04
sapply(lin_test_list, "[[", "rho.c")
sapply(lin_test_list, "[[", "sblalt")
地块
剧情可以一一绘制:
gg_plot_list[[1]]
BlandAltman_List[[1]]
或在 print
循环中。
for(i in seq_along(gg_plot_list))
print(gg_plot_list[[i]])
或者到图形设备(到磁盘文件)。
for(i in seq_along(gg_plot_list)) {
filename <- sprintf("Rplot%03d.png", i)
png(filename = filename)
print(gg_plot_list[[i]])
dev.off()
}
测试数据
Data_List <- iris[1:2]
names(Data_List) <- c("Temp_core", "Temperature")
Data_List$Time <- rep(1:50, 3)
Data_List <- split(Data_List, iris$Species)
names(Data_List) <- paste("df", letters[1:3], sep = "_")
Data_List <- lapply(Data_List, \(x){row.names(x) <- NULL; x})
我是 R 的新手,因此很抱歉,如果 awnser 很明显。 我正在尝试对 tibbles 及其 values/columns 执行操作,而此 tibbles 是列表的一部分。以前我会手动上传每个现在的小标题作为 data.frame(csv 数据)并在 data.frame 上手动执行操作。不幸的是,这很烦人,所以我试图同时为我的所有 data.frame 完成脚本中的所有操作。 例如,到目前为止对我有用的是将 0.7 添加到列表中每个小标题中名称为 'Temperature' 的每一列中的每个元素。我是这样做的:
for(i in seq_along(Data_List)) {Data_List[[i]]$Temperature <- Data_List[[i]]$Temperature + 0.7}
但是我现在想执行不同的任务:主要是我需要将我的 tibbles 分成序列。当我一次使用一个 data.frame 时,这就是我所做的:
df_Sitting <- df[1:12, ]
df_Standing <- df[13:26, ]
df_LigEx <- df[27:35, ]
df_VigEx <- df[36:42, ]
df_After <- df[43:54, ]
如何针对我现在拥有的所有 tibbles/data.frames 列表正确调整它? 其次,我想进行描述性统计,Pearson Correlation和Lin Correlation。此外,我创建了一个 ggplot 和一个 Bland-Altman-Plot。我是这样做的:
describe(df$Temperature)
describe(df$Temp_core)
cor.test(df)
library(epiR)
epi.ccc(df$Temp_core, df$Temperature, ci = "z-transform",
conf.level = 0.95, rep.measure = FALSE, subjectid)
mdata <- melt(df, id="Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group= variable, color = variable))+
geom_line(aes(group= variable, color = variable))
library(BlandAltmanLeh)
BlandAltman_df <- bland.altman.plot(df$Temp_core, df$Temperature, graph.sys = "ggplot2")
print(BlandAltman_df +theme(plot.title=element_text(hjust = 0.5)))
我现在想 运行 一次为整个小标题和小标题中的变量列表使用上面的所有函数,并获取所有相应的统计数据和图表,以便稍后创建 Markdown。我试过 lapply 但不知何故不起作用。 我希望我正确地提出了问题,感谢您的帮助!!
PS,这是 dput(head(df, 20))
的输出structure(list(Time = structure(c(52465, 52525, 52585, 52645,
52705, 52765, 52825, 52885, 52945, 53005, 53065, 53125, 53185,
53245, 53305, 53365, 53425, 53485, 53545, 53605), class = c("hms",
"difftime"), units = "secs"), Temp_core = c(35.565, 36.097, 36.38,
36.591, 36.782, 36.927, 37.067, 37.149, 37.208, 37.249, 37.276,
37.296, 37.327, 37.349, 37.356, 37.376, 37.393, 37.397, 37.409,
37.432), Temperature = c(33.87, 34.52, 34.85, 35.12, 35.37, 35.59,
35.74, 35.82, 35.95, 3600, 36.06, 36.17, 36.23, 36.18, 36.16,
36.18, 36.19, 36.19, 36.37, 36.37)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
使用一些其他类型的列表在 R 中是完全可行的。首先,我建议将 seq_along
替换为 lapply
,或者因为您已经在使用 tidyverse,purrr::map
:
for(i in seq_along(Data_List)) {
Data_List[[i]]$Temperature <- Data_List[[i]]$Temperature + 0.7
}
变为:
modified_data_list <- purrr::map(Data_List, function(df){
dplyr::mutate(df, Temperature = Temperature + 0.7)
})
您可以对上述功能应用相同的原则。请注意,我在这里使用 purrr:walk
而不是 map
,因为您没有在函数中返回修改后的数据框,而是将其称为“副作用”,如情节:
library(epiR)
library(BlandAltmanLeh)
modified_data_list <- purrr::walk(Data_List, function(df){
describe(df$Temperature)
describe(df$Temp_core)
cor.test(df)
epi.ccc(df$Temp_core, df$Temperature, ci = "z-transform",
conf.level = 0.95, rep.measure = FALSE, subjectid)
mdata <- melt(df, id="Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group= variable, color = variable))+
geom_line(aes(group= variable, color = variable))
BlandAltman_df <- bland.altman.plot(df$Temp_core, df$Temperature, graph.sys = "ggplot2")
print(BlandAltman_df +theme(plot.title=element_text(hjust = 0.5)))
})
您可以 lapply
列表成员的测试和绘图代码以及 return 测试结果和绘图列表。类似于以下内容。
library(ggplot2)
library(epiR)
library(BlandAltmanLeh)
Data_List <- lapply(Data_List, \(X){
X[["Temperature"]] <- X[["Temperature"]] + 0.7
X
})
cor_test_list <- lapply(Data_List, \(X) cor.test(formula = ~ Temperature + Temp_core, data = X))
lin_test_list <- lapply(Data_List, \(X){
epi.ccc(
X[["Temp_core"]],
X[["Temperature"]],
ci = "z-transform",
conf.level = 0.95,
rep.measure = FALSE
)
})
gg_plot_list <- lapply(Data_List, \(X){
mdata <- reshape2::melt(X, id = "Time")
ggplot(data = mdata, aes(x = Time, y = value))+
geom_point(aes(group = variable, color = variable))+
geom_line(aes(group= variable, color = variable))
})
BlandAltman_List <- lapply(Data_List, \(X){
BlandAltman_df <- bland.altman.plot(X$Temp_core, X$Temperature, graph.sys = "ggplot2")
BlandAltman_df +
theme(plot.title = element_text(hjust = 0.5))
})
测试
要访问测试结果,请再次使用 *apply
循环和提取函数。
sapply(cor_test_list, "[[", "estimate")
# df_a.cor df_b.cor df_c.cor
#0.7425467 0.5259107 0.4572278
sapply(cor_test_list, "[[", "statistic")
# df_a.t df_b.t df_c.t
#7.680738 4.283887 3.561892
sapply(cor_test_list, "[[", "p.value")
# df_a df_b df_c
#6.709843e-10 8.771860e-05 8.434625e-04
sapply(lin_test_list, "[[", "rho.c")
sapply(lin_test_list, "[[", "sblalt")
地块
剧情可以一一绘制:
gg_plot_list[[1]]
BlandAltman_List[[1]]
或在 print
循环中。
for(i in seq_along(gg_plot_list))
print(gg_plot_list[[i]])
或者到图形设备(到磁盘文件)。
for(i in seq_along(gg_plot_list)) {
filename <- sprintf("Rplot%03d.png", i)
png(filename = filename)
print(gg_plot_list[[i]])
dev.off()
}
测试数据
Data_List <- iris[1:2]
names(Data_List) <- c("Temp_core", "Temperature")
Data_List$Time <- rep(1:50, 3)
Data_List <- split(Data_List, iris$Species)
names(Data_List) <- paste("df", letters[1:3], sep = "_")
Data_List <- lapply(Data_List, \(x){row.names(x) <- NULL; x})