Reputation: 21302
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.
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 "
Upvotes: 0
Views: 5100
Reputation: 263471
Do it one of two ways: A (inside 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))),
row.names=id
)
}
B (outside the function):
compdf <- complete()
row.names(compdf) <- 1:NROW(compdf)
Upvotes: 1