简体   繁体   English

R项目数据框

[英]R-project data frame

I'm using a function that should return a data frame (table?) with 2 columns. 我正在使用一个函数,该函数应返回一个包含两列的数据框(表?)。

here is the function: 这是函数:

complete <- function(directory,id = 1:332) {

    csvfiles <- sprintf("/Users/myname/Desktop/%s/%03d.csv", directory, id)

    nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
    data.frame(ID=sprintf('%03d', id), countrows=sapply(csvfiles,function(x) length(count.fields(x))))
    }

Sample output: 样本输出:

                                       ID countrows
/Users/myname/Desktop/specdata/100.csv 100      1097
/Users/myname/Desktop/specdata/101.csv 101       731

I need the output to show just the number in that file path. 我需要输出以仅显示该文件路径中的数字。 So the first one should read just 100, the second record 101 etc. 因此,第一个记录应只读取100,第二个记录应读取101等。

This does the job in the console 这在控制台中完成了工作

colID <- sprintf('%03d', id)

But I'm trying to integrate it into my function with no joy. 但是,我试图将其集成到我的函数中却没有任何乐趣。

I have tried: 我努力了:

nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
+ data.frame(ID=sprintf('%03d', id), countrows=sapply(csvfiles,function(x) length(count.fields(x))))

and I have tried: 我已经尝试过:

 complete <- function(directory,id = 1:332) {

    csvfiles <- sprintf("/Users/myname/Desktop/%s/%03d.csv", directory, id)

    colID <- sprintf('%03d', id)

    nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
    data.frame(ColID, countrows=sapply(csvfiles,function(x) length(count.fields(x))))
    }

I'm sure I'm just a step away? 我确定我只是一步之遥?

BASED ON FEEDBACK BELOW 基于以下反馈

I tried this 我试过了

complete <- function(directory,id = 1:332) {

    csvfiles <- sprintf("/Users/myname/Desktop/%s/%03d.csv", directory, id)

    colID <- sprintf('%03d', id)

    nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
    data.frame(ID=id, countrows=sapply(csvfiles,function(x) length(count.fields(x))))

    row.names(colID) <- basename(row.names(colID))
    }

That's giving back an error: "Error in basename(row.names(colID)) : a character vector argument expected " 这会返回一个错误:“ basename(row.names(colID))中的错误:预期为字符向量参数”

Do it one of two ways: A (inside the function): 使用以下两种方法之一:A(在函数内部):

complete <- function(directory,id = 1:332) {

   csvfiles <- sprintf("/Users/myname/Desktop/%s/%03d.csv", directory, id)

   nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
   data.frame(ID=sprintf('%03d', id), 
              countrows=sapply(csvfiles,function(x) length(count.fields(x))),
             row.names=id
           )
       }

B (outside the function): B(函数外):

 compdf <- complete()
row.names(compdf) <- 1:NROW(compdf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM