Reputation: 21
Thanks for all your advice. My remaining question is this: Can I replace column name 'sulphate' in the following statement ... dataclean <- datatable$sulfate[!datanas] .... with a reference to a parameter 'pollutant', which may or may not have a value of 'sulfate'?
Upvotes: 2
Views: 90
Reputation: 70653
When you attach values to arguments, they appear as they would be objects in your workspace. But the environment is not workspace but that of the function.
So in your case, directory
would be a character string and it would work. For the first time. Your working directory is now changed and you need to revert back to the previous one for the function to work again. This can get pretty messy so what I like to do is just refer to raw files by full path. See ?list.files
for more info.
For your second question, your best bet is to refer to a certain level within the variable, is to do
x[, pollutant]
It is convenient to add drop = FALSE
argument there, in order to keep the what I'm assuming is a data.frame.
You could improve your function by also implementing the datatable
argument. That way you have all the objects bundled together nicely.
The most important thing to note here would be "debugging". You should learn to use at least browser()
. This function will stop the execution of your function at the very step where it was called. This enables you, in the R console, to inspect elements in the function and run code to see what's going. This way you can speed up the development of code, at least initially when you usually haven't internalized all the data structures and paradigms yet.
Upvotes: 1