R:使用foreach读取csv数据,并在数据上应用函数,然后导出回csv
我有3个csv文件,即R:使用foreach读取csv数据,并在数据上应用函数,然后导出回csv,r,foreach,parallel-foreach,R,Foreach,Parallel Foreach,我有3个csv文件,即file1.csv,file2.csv和file3.csv 现在,对于文件的每个,我想导入csv并对其执行一些功能,然后导出转换后的csv。因此,3个csv输入和3个转换csv输出。只有3个独立的任务。所以我想我可以尝试使用foreach%dopar%。请不要告诉我我正在使用窗口机器 然而,我无法让它工作 library(foreach) library(doParallel) library(xts) library(zoo) numCores <- detectC
file1.csv
,file2.csv
和file3.csv
现在,对于文件的每个,我想导入csv并对其执行一些功能,然后导出转换后的csv。因此,3个csv输入和3个转换csv输出。只有3个独立的任务。所以我想我可以尝试使用foreach
%dopar%
。请不要告诉我我正在使用窗口机器
然而,我无法让它工作
library(foreach)
library(doParallel)
library(xts)
library(zoo)
numCores <- detectCores()
cl <- parallel::makeCluster(numCores)
doParallel::registerDoParallel(cl)
filenames <- c("file1.csv","file2.csv","file3.csv")
foreach(i = 1:3, .packages = c("xts","zoo")) %dopar%{
df_xts <- data_processing_IMPORT(filenames[i])
ddates <- unique(date(df_xts))
}
它仍然不起作用。我想了解我的逻辑出了什么问题,我应该如何解决这个问题?我只是尝试在数据上应用简单的函数,我仍然没有转换数据并将它们单独导出到csv。但我已经被卡住了
有趣的是,我已经编写了下面的简单代码,效果很好。在foreach
中,a
就像上面的df_xts
一样,存储在变量中并传递到Fun2
进行处理。下面的代码运行良好。但上面没有。我不明白为什么
numCores <- detectCores()
cl <- parallel::makeCluster(numCores)
doParallel::registerDoParallel(cl)
# Define the function
Fun1=function(x){
a=2*x
b=3*x
c=a+b
return(c)
}
Fun2=function(x){
a=2*x
b=3*x
c=a+b
return(c)
}
foreach(i = 1:10)%dopar%{
x <- rnorm(5)
a <- Fun1(x)
tst <- Fun2(a)
return(tst)
}
### Output: No error
parallel::stopCluster(cl)
numCores使用foreach()
是正确的。您在ddates中使用了date()
,我遇到了关于读取、修改和写入几个CSV文件的相同问题。我试图找到一个tidyverse
解决方案,虽然它并没有真正解决上面的date
问题,但这里是--如何使用purr
中的map
读取、修改和写入几个csv文件
library(tidyverse)
# There are some sample csv file in the "sample" dir.
# First get the paths of those.
datapath <- fs::dir_ls("./sample", regexp = ("csv"))
datapath
# Then read in the data, such as it is a list of data frames
# It seems simpler to write them back to disk as separate files.
# Another way to read them would be:
# newsampledata <- vroom::vroom(datapath, ";", id = "path")
# but this will return a DF and separating it to different files
# may be more complicated.
sampledata <- map(datapath, ~ read_delim(.x, ";"))
# Do some transformation of the data.
# Here I just alter the column names.
transformeddata <- sampledata %>%
map(rename_all, tolower)
# Then prepare to write new files
names(transformeddata) <- paste0("new-", basename(names(transformeddata)))
# Write the csv files and check if they are there
map2(transformeddata, names(transformeddata), ~ write.csv(.x, file = .y))
dir(pattern = "new-")
库(tidyverse)
#“示例”目录中有一些示例csv文件。
#首先要知道这些的路径。
数据路径功能data\u processing\u IMPORT
做什么?我在您给定的包中找不到它,它所做的只是读取.csv并将其转换为xts对象。仅此而已。您使用的date()
错误,您想将用作.date()
date()
返回当前系统时间。
ddates <- unique(as.Date(df_xts))
library(tidyverse)
# There are some sample csv file in the "sample" dir.
# First get the paths of those.
datapath <- fs::dir_ls("./sample", regexp = ("csv"))
datapath
# Then read in the data, such as it is a list of data frames
# It seems simpler to write them back to disk as separate files.
# Another way to read them would be:
# newsampledata <- vroom::vroom(datapath, ";", id = "path")
# but this will return a DF and separating it to different files
# may be more complicated.
sampledata <- map(datapath, ~ read_delim(.x, ";"))
# Do some transformation of the data.
# Here I just alter the column names.
transformeddata <- sampledata %>%
map(rename_all, tolower)
# Then prepare to write new files
names(transformeddata) <- paste0("new-", basename(names(transformeddata)))
# Write the csv files and check if they are there
map2(transformeddata, names(transformeddata), ~ write.csv(.x, file = .y))
dir(pattern = "new-")