如何將相應檔案名的名稱附加到代碼的串列輸出中。目標是能夠將串列中的輸出跟蹤到輸入 csv 檔案。目前,串列輸出回傳一個串列索引[[1]], [[2]], ...([[5]]見下面的快照)。我想要包含相應的檔案名,像這樣CA_three, FL_three, ....,NY_two

@Akrun,我希望每個頁面都有一個對應的檔案名

下面有兩個代碼
代碼 1:回圈 5 個 csv 檔案并回傳輸出串列的代碼
[[]](我需要幫助)代碼 2:生成 5 個 csv 檔案的代碼
Code 1
# Code 1
library(multcompView) # for tukeyHD
# list of 20 csv files in a folder
files <- list.files("C:\\mypath\\", pattern="*.csv", full.names = T)
out <- lapply(1:length(files), function(x) { # use lapply to loop over all files
this_data <- read.csv(files[x], header = TRUE)
aov_mod <- aov(revenue ~ dept, data = this_data)
tuk<-TukeyHSD(x= aov_mod)
tuk
})
out
# Code 2
# In order to generate 5 csv files, copy and paste the code below and save in a new folder
#
state <-c("NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY")
dept <- c("energy","energy","energy","energy","works",'works','works','works','fin','fin','fin','fin','parks','parks','parks','parks','trans','trans','trans','trans')
year <- c("two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two")
revenue <-c(1212.9,1253,1244.4,5123.5,1312,3134,515.8,2449.9,3221.6,3132.5,2235.09,2239.01,3235.01,5223.01,4235.6,2204.5,2315.5,6114,4512,3514.2)
NY_two <-data.frame(state,dept,year,revenue)
# dataset 2
state <-c("NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY","NY")
dept <- c("energy","energy","energy","energy","works",'works','works','works','fin','fin','fin','fin','parks','parks','parks','parks','trans','trans','trans','trans')
year <- rep("five",20)
revenue <-c(1212.9,1253,1244.4,5123.5,1312,3134,515.8,2449.9,3221.6,3132.5,2235.09,2239.01,3235.01,5223.01,4235.6,2204.5,2315.5,6114,4512,3514.2)
NY_five <-data.frame(state,dept,year,revenue)
state <- rep("FL",20)
dept <- c("energy","energy","energy","energy","works",'works','works','works','fin','fin','fin','fin','parks','parks','parks','parks','trans','trans','trans','trans')
year <- rep("three",20)
revenue <-c(112.9,123,124,523.5,112,334,55,449,221.6,332,235,239,235,223,235.6,204,315.5,614,512,514.2)
FL_three <- data.frame(state,dept,year,revenue)
state <- rep("CA",20)
dept <- c("energy","energy","energy","energy","works",'works','works','works','fin','fin','fin','fin','parks','parks','parks','parks','trans','trans','trans','trans')
year <- rep("three",20)
revenue <-c(1102.9,1023,1024,5203.5,1012,3034,505,4049,2021.6,3032,2035,2039,2035,2023,2035.6,2004,3015.5,6014,5012,5014.2)
CA_three <- data.frame(state,dept,year,revenue)
state <- rep("KY",20)
dept <- c("energy","energy","energy","energy","works",'works','works','works','fin','fin','fin','fin','parks','parks','parks','parks','trans','trans','trans','trans')
year <- rep("one",20)
revenue <-c(1102.9,1023,1024,5203.5,1012,3034,505,4049,2021.6,3032,2035,2039,2035,2023,2035.6,2004,3015.5,6014,5012,5014.2)
KY_one <- data.frame(state,dept,year,revenue)
setwd("C:\\define your path\\")
# writing out the file to a newly created folder
write.csv(NY_two,"NY_two.csv",row.names = FALSE)
write.csv(CA_three,"CA_three.csv",row.names = FALSE)
write.csv(FL_three,"FL_three.csv",row.names = F)
write.csv(NY_five,"NY_five.csv",row.names = FALSE)
write.csv(KY_one,"KY_one.csv",row.names = F)
請提前分享您的代碼,謝謝!
uj5u.com熱心網友回復:
'out'list沒有任何名稱,因為它沒有被命名。如果名稱應該來自files零件,我們可以用檔案名的子字串命名輸出('out')
# returns all the file paths for csv
files <- list.files("C:\\mypath\\", pattern="\\.csv$", full.names = TRUE)
# get the substring of file names without the .csv part
filenms <- sub("\\.csv$", "", basename(files))
現在,我們使用與 OP 帖子中相同的代碼,或者直接回圈檔案
out <- lapply(files, function(x) { # use lapply to loop over all files
this_data <- read.csv(x, header = TRUE)
aov_mod <- aov(revenue ~ dept, data = this_data)
tuk <- TukeyHSD(x= aov_mod)
# output of TukeyHSD is a list which can be summarised into tibble
# with tidy from broom
broom::tidy(tuk)
})
# set the names with filenms
names(out) <- filenms
如果我們想撰寫輸出使用imap/iwalkfrom purrrwhich is concise as.y回傳名稱并.x回傳list
purrr::iwalk(out, ~ write.csv(.x, paste0(.y, ".csv"), row.names = FALSE))
如果我們想要pdf檔案,一個選項是使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/443920.html
上一篇:運行宏時Excel隨機崩潰
