[英]R-project data frame returning too many columns
Working through an R tutorial that I'm having a hard time understanding. 通过R教程,我很难理解。
Directory is a folder with numerous csv files. 目录是包含大量csv文件的文件夹。 The function takes as id either one of more of the files and returns the number of records in each.
该函数将多个文件之一作为ID,并返回每个文件中的记录数。
My 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))),
row.names=id
)
}
Then complete("specdata", 100:105) Returns 然后complete(“ specdata”,100:105)返回
ID countrows
100 100 1097
101 101 731
102 102 1462
103 103 3653
104 104 2558
105 105 2192
What must I do so that the left most column is a sequence starting 1? 我该怎么做才能使最左列是从1开始的序列? So that, for example, the first record would be 1 100 & 1092, the second record 2 101 & 731
因此,例如,第一条记录将是1100和1092,第二条记录将是2101和731
The first apparent column is just the names of the rows (look at eg ncol(specdata)
). 第一明显的列只是行的名称(例如,查看
ncol(specdata)
)。 You can rename rows as follows: 您可以按以下方式重命名行:
row.names(specdata) <- 1:nrow(specdata)
在函数内部,在dataframe调用中使用以下代码:
row.names = 1: length(id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.