大家。
在R。
中创建一个空的ff data.frame我想在R中读取多个csv文件,将它们绑定在一起并创建一个大数据框架。由于数据非常庞大,我使用的是ff包。
这是我的代码。
file_list = list.files(pattern="*.csv")
library(ff)
for(i in seq_along(length(file_list)){
ffdf <- read.csv.ffdf(x=ffdf, file=file_list[i], header=T)
}
但是,我收到了以下错误。
Error in `rownames<-`(x, value) :
attempt to set 'rownames' on an object with no dimensions
我搜索了Google和Stackoverflow中的错误消息,但没有得到有用的结果。 有谁知道如何处理这个问题?
以下代码有效。
library(ffbase)
library(ff)
file_list = list.files(pattern="*.csv")
lst <- lapply(file_list, function(x) read.csv.ffdf(file=x,header=TRUE))
ff1 <- Reduce(function(x,y) ffdfappend(x,y, adjustvmode=F), lst)
起初,lappy行是这样的,它没有用。
lst <- lapply(file_list, read.csv.ffdf, header=TRUE)
关键是写file=
。似乎 ff
函数需要明确指示属性。
(参考:Import text file using ff package)
感谢大家!
答案 0 :(得分:0)
如果打算从多个文件创建一个大的ffdf对象,
library(ffbase)
library(ff)
ff1 <- Reduce(function(x,y) ffdfappend(x,y, adjustvmode=F), lst)
dim(ff1) #from the example
#[1] 78 7
,其中
lst <- lapply(file_list, read.csv.ffdf, header=TRUE)
使用?read.csv.ffdf
x <- data.frame(log=rep(c(FALSE, TRUE), length.out=26),
int=1:26, dbl=1:26 + 0.1
, fac=factor(letters), ord=ordered(LETTERS)
, dct=Sys.time()+1:26, dat=seq(as.Date("1910/1/1"), length.out=26, by=1))
x <- x[c(13:1, 13:1),]
csvfile <- tempPathFile(path=getOption("fftempdir"), extension="csv")
write.csv(x, file=csvfile, row.names=FALSE)
y <- read.csv(file=csvfile, header=TRUE)
y
cat("Read csv with header\n")
ffx <- read.csv.ffdf(file=csvfile, header=TRUE)
lst <- lapply(1:3, function(x) read.csv.ffdf(file=csvfile,
header=TRUE))
dim(lst[[1]])
#[1] 26 7