[英]Writing multiple data frames into .csv files using R
我使用 lapply 將函數應用於多個數據幀:
data.cleaned <- lapply(data.list, shooter_cleaning)
這是理查德評論中的一個自包含示例,但使用列表中數據幀的名稱作為 CSV 文件的文件名:
# Create a list of n data frames
n <- 10
my_list <- lapply(1:n, function(i) data.frame(x = rnorm(10), y = rnorm(10)) )
# name the data frames
names(my_list) <- letters[1:n]
# save each new data frame as an individual .csv file based on its name
lapply(1:length(my_list), function(i) write.csv(my_list[[i]],
file = paste0(names(my_list[i]), ".csv"),
row.names = FALSE))
這是一種常見的操作。 您需要將數據幀拆分為數據幀list
,然后將它們寫入許多單獨的 csv。 我將演示 2 種使用基礎 R 的方法,以及 2 種使用 tidyverse 的方法。
for
循環使迭代非常明確。
# example data.frame
df <- data.frame(x = 1:4, y = c("a", "a", "b", "b"))
# split the dataframe into a list by the y column
l <- split(df, df$y)
# make filepaths from list names, which are unique values of the y column
file_out <- paste0(names(l), ".csv")
# iterate over the list and the vector of list names to write csvs
for(i in 1:length(l)) {
write_csv(l[[i]], file_out[i])
}
或者使用mapply()
:
mapply(
function(x, y) write_csv(x, y),
l,
file_out
)
library(tidyverse)
# we pass walk2 two inputs: a list of dataframes (.x) and filepaths (.y)
# `walk` is a silent `map` that doesn't print output to the console
walk2(l, file_out, ~write_csv(.x, .y))
或者,避免中間變量:
df %>%
group_split(y) %>%
walk(~write_csv(.x, paste0(.x$y[1], ".csv")))
如果這有幫助:我有一個包含多個數據框的環境,並且只有這些數據框,我想將每個數據框作為單獨的 CSV 文件輸出。 在 Ben 的回答的幫助下,並發現了mget
,我能夠使用以下代碼做到這一點:
for(i in 1:length(ls())) {
write.table(
mget(ls()[[i]]),
file = paste0(ls()[[i]], ".csv"),
sep = ";",
qmethod = "double",
row.names = FALSE)
}
這是使用
purrr<\/code>在
tibble<\/a>中寫入多個數據幀的表示:
# use functions from the
# tidyr, stringr and purrr packages
library(tidyverse)
# create fake dataset
expand_grid(cat = c("a","b"),
lev = "c",
num = 1:2) %>%
# group and nest by `cat`
group_by(cat) %>%
nest() %>%
ungroup() %>%
# create different file names with
# the `cat` variable as reference
# (you can also specify folder paths)
mutate(file = map_chr(.x = cat,
.f = str_replace,
"(.+)","\\1.csv")) %>%
# write each nested dataset
# in a separate file
mutate(write = pmap(.l = select(.,x = data, file),
.f = write_csv))
#> # A tibble: 2 x 4
#> cat data file write
#> <chr> <list> <chr> <list>
#> 1 a <tibble [2 x 2]> a.csv <tibble [2 x 2]>
#> 2 b <tibble [2 x 2]> b.csv <tibble [2 x 2]>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.