【发布时间】:2014-02-26 08:26:51
【问题描述】:
我有一个文件夹,里面装满了包含相同时间序列数据的文件。这是我的数据示例:
Date Time Letter
2009-09-25 23:56:00 a
2009-09-25 23:57:00 a
2009-09-25 23:58:00 b
2009-09-25 23:59:00 c
2009-09-26 00:00:00 c
2009-09-26 00:00:00 c
2009-09-26 00:00:00 c
2009-09-26 00:00:00 b
2009-09-26 00:00:00 a
我希望将我的功能应用于文件夹中的所有文件。在此,我将使用按日期拆分将每个文件分成不同的日期,并对这些文件应用一个函数。然后,我使用表格返回日期,然后是当天输入的每个字母的编号。所以对于上面的数据我会回来的:
$2009-09-25
a 2
b 1
c 1
$2009-09-26
a 1
b 1
c 3
我的问题是,当我尝试使用 sink 将输出定向到文件时,我收到错误“sink stack is full”。
setwd("C:\\User1\\Documents\\Files")
path <- "C:\\User1\\Documents\\Files"
files <- list.files(path = path)
lapply(files,function(files, path){
path <- "C:\\User1\\Documents\\Files"
fp <- file.path(path, files)
df <- read.csv(fp)
#A few more calculations with data here
# Separating data frame into large list of separate days
eachday <- split(df, df$Date)
myfunction <- function(df){
# More calculations with data within function
#Sink output to file
sink(file="testing121.csv", append=TRUE, type = "output", split=FALSE)
return(table(night$Activity))
}
# Apply function over list of days
lapply(eachday, myfunction)
})
如果我在函数中关闭接收器,它也不起作用。有没有更好的方法将我的数据保存到具有每个特定日期及其字母列频率的文件中?为什么这个水槽是错误的?
【问题讨论】:
-
sink用于将控制台输出写入文件。您想使用write.csv构建数据的 CSV 文件。
标签: r file-io dataframe time-series