我是 R 的新手,因此很抱歉,如果 awnser 很明顯。我正在嘗試對小標題及其值/列執行操作,而此小標題是串列的一部分。以前我會手動上傳每個現在的 tibbles 作為 data.frame(csv 資料)并在 data.frame 上手動執行操作。不幸的是,這很煩人,所以我試圖同時為我的所有 data.frames 完成我在腳本中的所有操作。例如,到目前為止對我有用的是將 0.7 添加到串列中每個 tibble 名稱為“Temperature”的每列中的每個元素。我是這樣做的:
for(i in seq_along(Data_List)) {Data_List[[i]]$Temperature <- Data_List[[i]]$Temperature 0.7}
但是我現在想執行不同的任務:主要是我需要將我的小標題分成序列。當我一次使用一個 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)))
我現在想一次為整個 tibbles 串列和 tibbles 中的變數串列運行上述所有函式,并獲取所有相應的統計資訊和繪圖,以便稍后創建 Markdown。我試過 lapply 但不知何故不起作用。我希望我正確地提出了這個問題,我很感激你的幫助!!
uj5u.com熱心網友回復:
與其他型別的串列,在作業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)))
})
uj5u.com熱心網友回復:
您可以lapply將測驗和繪圖代碼添加到串列成員并回傳測驗結果和繪圖串列。類似于以下內容。
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("Rplotd.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})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340007.html
上一篇:按r中的特定類別過濾
