我是來自 stata 的 R 新手,正在努力適應 R 語言的回圈。我需要遍歷一個串列,為串列中的每個元素創建一個表,并將每個表匯出到 .csv 檔案。這是我的資料的較短版本:
mydata <- structure(list(SampleID = c("R22 w0", "R24 w0", "R26 w0", "R29 w0",
"R22 w1", "R24 w1", "R26 w1", "R29 w1", "R22 w8", "R24 w8", "R26 w8",
"R29 w8", "R22 w24", "R24 w24", "R26 w24", "R29 w24", "R23 w0",
"R25 w0", "R27 w0", "R30 w0", "R23 w1", "R25 w1", "R27 w1", "R30 w1",
"R23 w8", "R25 w8", "R27 w8", "R30 w8", "R23 w24", "R25 w24",
"R27 w24", "R30 w24", "R1 w0", "R3 w0", "R5 w0", "R7 w0", "R9 w0",
"R11 w0", "R13 w0", "R15 w0", "R17 w0", "R19 w0", "R21 w0", "R1 w1",
"R3 w1", "R5 w1", "R7 w1", "R9 w1", "R11 w1", "R13 w1", "R15 w1",
"R17 w1", "R19 w1", "R21 w1", "R1 w8", "R3 w8", "R5 w8", "R7 w8",
"R9 w8", "R11 w8", "R13 w8", "R15 w8", "R17 w8", "R19 w8", "R21 w8",
"R1 w24", "R3 w24", "R5 w24", "R7 w12", "R9 w24", "R11 w24",
"R13 w24", "R15 w24", "R17 w24", "R19 w48", "R21 w24", "R2 w0",
"R4 w0", "R6 w0", "R8 w0", "R10 w0", "R12 w0", "R14 w0", "R16 w0",
"R18 w0", "R20 w0", "R2 w1", "R4 w1", "R6 w1", "R8 w1", "R10 w1",
"R12 w1", "R14 w1", "R16 w1", "R18 w1", "R20 w1", "R2 w8", "R4 w8",
"R6 w8", "R8 w4", "R10 w8", "R12 w8", "R14 w8", "R16 w8", "R18 w8",
"R20 w8", "R2 w24", "R4 w24", "R6 w24", "R8 w24", "R10 w24",
"R12 w24", "R14 w24", "R16 w24", "R18 w24", "R20 w48"), week = c(0,
0, 0, 0, 1, 1, 1, 1, 8, 8, 8, 8, 24, 24, 24, 24, 0, 0, 0, 0,
1, 1, 1, 1, 8, 8, 8, 8, 24, 24, 24, 24, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 24, 24, 24, 12, 24, 24, 24, 24, 24, 24, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8,
8, 4, 8, 8, 8, 8, 8, 8, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24
), Treatment = c("Placebo", "Active", "Placebo", "Active", "Placebo",
"Active", "Placebo", "Active", "Placebo", "Active", "Placebo",
"Active", "Placebo", "Active", "Placebo", "Active", "Active",
"Placebo", "Placebo", "Active", "Active", "Placebo", "Placebo",
"Active", "Active", "Placebo", "Placebo", "Active", "Active",
"Placebo", "Placebo", "Active", "Active", "Placebo", "Placebo",
"Active", "Placebo", "Active", "Placebo", "Placebo", "Active",
"Placebo", "Placebo", "Active", "Placebo", "Placebo", "Active",
"Placebo", "Active", "Placebo", "Placebo", "Active", "Placebo",
"Placebo", "Active", "Placebo", "Placebo", "Active", "Placebo",
"Active", "Placebo", "Placebo", "Active", "Placebo", "Placebo",
"Active", "Placebo", "Placebo", "Active", "Placebo", "Active",
"Placebo", "Placebo", "Active", "Placebo", "Placebo", "Active",
"Active", "Placebo", "Placebo", "Placebo", "Active", "Active",
"Placebo", "Active", "Active", "Active", "Active", "Placebo",
"Placebo", "Placebo", "Active", "Active", "Placebo", "Active",
"Active", "Active", "Active", "Placebo", "Placebo", "Placebo",
"Active", "Active", "Placebo", "Active", "Active", "Active",
"Active", "Placebo", "Placebo", "Placebo", "Active", "Active",
"Placebo", "Active", "Active")), row.names = c(NA, -116L), class = "data.frame")
我需要一個回圈來為“周”列(0、1、4、8、12、24 和 48)中的每個可能值應用以下代碼。
Data_week_0 <- mydata %>% filter(!stringr::str_detect(Treatment, 'Placebo'))
這樣我就得到了 5 個帶有 0、1、4、8、12、24 和 48 后綴的Data_week_表。然后,將每個物件匯出到 .csv 檔案
write.csv(Data_week_0,"~/Data_week_0.csv", row.names = FALSE)
我知道這是一個基本問題,但我將不勝感激。
uj5u.com熱心網友回復:
這是一個選項,用于回圈遍歷您的資料以匯出每個治療條件的單個周資料框:
library(tidyverse)
for (j in unique(mydata$Treatment)) {
df_treatment = mydata %>% filter(Treatment==j)
for (i in unique(df_treatment$week)) {
df_week <- df_treatment %>% filter(week==i)
write.csv(df_week, paste0("~/Data_week_", i, "_", j,".csv"), row.names = FALSE)
}
}
uj5u.com熱心網友回復:
不需要在tidyverse這里使用。您可以使用基礎 R:
# week values to filter by, assign object
weekVals <- c(0, 1, 4, 8, 12, 24, 48)
# treatment values to filter by, assign object
treatVals <- unique(mydata[["Treatment"]])
# combine values and iterate over each row..
combVals <- expand.grid(weekVals, treatVals, stringsAsFactors = FALSE)
# use for loop to iterate over rows...
for (i in seq_len(nrow(combVals))) {
wk <- combVals[i, 1]
treat <- combVals[i, 2]
# subset data
df <- mydata[mydata[["week"]] == wk & mydata[["Treatment"]] == treat, ]
# tell user which subsets return no match
if (nrow(df) == 0L) {cat("Data frame is empty for combination week", wk, ", Treatment:", treat, "\n\n");next}
# write it out
write.csv(df, paste0("Data_week_", wk, "_", treat, ".csv"))
}
也可以使用data.table以下功能過濾并保存到 csv 中fwrite:
# data.table and for loop, fwrite to write file into csv
for (i in seq_len(nrow(combVals))) {
wk <- combVals[i, 1]
treat <- combVals[i, 2]
# subset data
df <- mydata[week == wk & Treatment == treat, ]
# tell user which subsets return no match
if (nrow(df) == 0L) {cat("Data frame is empty for combination week", wk, ", Treatment:", treat, "\n\n");next}
# write it out
data.table::fwrite(df, paste0("Data_week_", wk, "_", treat, ".csv"))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504645.html
