0

I'm still a rookie to the R world, in a very accelerated class with limited/no guidance. My assignment is to build a custom function that reads in a specific .csv, and take some specific columns out to be analyzed. Could anyone please offer some advice? The "sample code" I was given looks like this:

AnnualLekSurvey=function(data.in,stat.year){
d1=subset(data.in,year==stat.year)
d2=d1[c("year","complex","tot_male")]
      attach(d2)}

So when it's complete and I run it, I should be able to say:

AnnualLekSurvey(gsg_lek,2006)

where "gsg_lek" is the name of the file I want to import, and 2006 is the values from the "year" column that I want to subset. "complex" and "tot_male" will be the variable to be analyzed by "year", but I'm not worried about that code right now.

What I'm confused about is; how do I tell R that gsg_lek is a .csv file, and tell it to look in the proper directory for it when I run the custom function?

I saw one other vaguely similar example on here, and they had to use the if() and paste() commands to build the string of the file name - that seems like too much arbitrary work, unless I'm just being lazy...

Any help would be appreciated.

1
  • if data.in is the full path to your csv file including extension, the first line of your function should be d1 <- read.csv(data.in) or similar. Do not use attach; instead just return(d2). What do you mean "too much arbitrary work?" Commented Oct 4, 2014 at 0:52

1 Answer 1

1

You can make a function like this:

AnnualLekSurvey <- function(csvFile, stat.year)
{
  d1 <- read.csv(paste("C:/",csvFile,".csv", sep=""),header=T, sep=",")  
  d2 <- subset(d1, year==stat.year)
  d2 <- d2[, c("year","complex","tot_male")]
  return(d2)
}

The argument 'csvFile' in the function is the basename of your csv file. In this particular example, this has to be in your C:/ folder. If your file is in some other folder, you have to change the "C:/" in the function to the folder where your csv file is located.

Running the function:

data <- AnnualLekSurvey("gsg_lek", "2006")

Note that the arguments has to be within the quotes. 'data' will now contain the columns year, complex and tot_male of gsg_lek.csv corresponding to the year 2006

Sign up to request clarification or add additional context in comments.

4 Comments

What if they're on a linux machine? You might want to use file.path or path.expand instead of hard-coding a file path.
@technOslerphile the code looks like it ought to run, and I entered it exactly as you have it, except setting C:/ to where the file is (same as my working directory). But when I run: data <- AnnualLekSurvey(gsg_lek, "2006") I get this error: Error in paste("C:/...exercise_dat", : object 'gsg_lek' not found - Since it is saying OBJECT gsg_lek not found, does that imply that it is trying to make gsg_lek an object before it has been read in to R? Any idea what's going wrong?
Sorry for the run-on comment above. I tried "ctrl+k" and "5 spaces" to separate the code from the text, but neither worked...
Ah, my bad. Both the arguments has to be within the quotes. It is working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.