trying to create a function that looks up a bunch of CSV files in a directory and then, taking the file ID as an argument, outputs a table (actually data frame – new to R language) where there are 2 columns, one titled ID for the corresponding id parameter and the second column will be the count of rows in that file.
The files are all titled 001.csv – 322.csv
e.g. output would look like column title: ID, first record: 001 (derived from 001.csv), second column: title "count of rows", first record
The function looks like so: myfunction(directory,id)
Directory is the folder where the csv files are and id can be a number (or vector?) e.g. simply 1 or 9 or 100 or it can be a vector like so 200:300.
In the case of the later, 200:300, the output would be a table with 100 rows where the 1st row would be 200 with say 10 rows of data within it.
So far:
complete <- function(directory,id = 1:332) {
# create an object to help read the appropriate csv files later int he function
csvfilespath <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)
colID <- sprintf('%03d', id)
# now, how do I tell R to create a table with 2 columns titled ID and countrows?
# Now, how would I take each instance of an ID and add to this table the id and count of rows in each?
}
I apologize if this seems really basic. The tutorial I’m on moves fast and I have watched each video lecture and done a fair amount of research too.
SO is by far my favourite resource and I learn better by using it. Perhaps because it’s personalised and directly applicable to my immediate tasks. I hope my questions also benefit others who are learning R.
BASED ON FEEDBACK BELOW
I now have the following script:
complete <- function(directory,id = 1:332) {
csvfiles <- sprintf("/Users/gcameron/Desktop/%s/%03d.csv", directory, id)
nrows <- sapply( csvfiles, function(f) nrow(read.csv(f)))
data.frame(ID=id, countrows=sapply(csvfiles,function(x) length(count.fields(x)))
}
Does this look like I’m on the right track?
I’m receiving an error "Error: unexpected ‘}’ in:
"data.frame(ID=id, countrows=sapply(csvfiles,function(x) length(count.fields(x)))
}"
I cannot see hwere the extra "}" is coming from?
1 Answer