Working through an R tutorial that I’m having a hard time understanding.
Directory is a folder with numerous csv files. The function takes as id either one of more of the files and returns the number of records in each.
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
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? So that, for example, the first record would be 1 100 & 1092, the second record 2 101 & 731
The first apparent column is just the names of the rows (look at e.g.
ncol(specdata)). You can rename rows as follows: