R-将excel读入源结构中的数据框,并获取读取的行数
我正在尝试使用R将excel读入数据框R-将excel读入源结构中的数据框,并获取读取的行数,r,excel,R,Excel,我正在尝试使用R将excel读入数据框 dat = lapply(file.list, function(i){ print(i); x = read_xlsx(i,sheet=NULL, range=cell_cols("A:AE"), col_names=TRUE, skip=1, trim_ws=TRUE, guess_max=1000) x$file=i print(x$file) # Return data x }) 如何查找从每个excel读取的行数。我希望获得此数字,以确保我
dat = lapply(file.list, function(i){
print(i);
x = read_xlsx(i,sheet=NULL, range=cell_cols("A:AE"), col_names=TRUE, skip=1, trim_ws=TRUE, guess_max=1000)
x$file=i
print(x$file)
# Return data
x
})
如何查找从每个excel读取的行数。我希望获得此数字,以确保我可以使用excel中的实际计数验证读取的记录计数。如果在调用Lappy之前初始化行计数向量,则可以将每张工作表的行数(或完整尺寸)存储在该表中。比如:
row_counts <- vector()
dat = lapply(file.list, function(i){
print(i);
x = read_xlsx(i,sheet=NULL, range=cell_cols("A:AE"),
col_names=TRUE, skip=1,
trim_ws=TRUE, guess_max=1000)
row_counts[i] <- nrow(x)
x$file=i
print(x$file)
# Return data
x
})
row\u counts另一种方法是创建一个list()
对象作为apply函数的结果,其中输出列表包括一个数据对象和计数
由于我从kaggle.com上的Alberto Barradas的数据中获得了一组可用的csv
格式的示例文件,因此我将使用read.csv()
而不是read\u xlsx()
,但该过程将适用于任一函数
download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
"pokemonData.zip",
method="curl",mode="wb")
unzip("pokemonData.zip")
thePokemonFiles <- list.files("./pokemonData",
full.names=TRUE)
fileList <- lapply(thePokemonFiles,function(x) {
# read data and generate a list object including the data and row count
data <- read.csv(x)
list(data = data,rows=nrow(data))
})
# extract counts from the list
unlist(lapply(fileList,function(x){x["rows"]}))
> # extract counts from the list
> unlist(lapply(fileList,function(x){x["rows"]}))
rows rows rows rows rows rows
165 106 160 121 165 82
>