r - Creating a read() command in a custom function -



r - Creating a read() command in a custom function -

i'm still rookie r world, in accelerated class limited/no guidance. assignment build custom function reads in specific .csv, , take specific columns out analyzed. please offer advice? "sample code" given looks 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 finish , run it, should able say:

annualleksurvey(gsg_lek,2006)

where "gsg_lek" name of file want import, , 2006 values "year" column want subset. "complex" , "tot_male" variable analyzed "year", i'm not worried code right now.

what i'm confused is; how tell r gsg_lek .csv file, , tell in proper directory when run custom function?

i saw 1 other vaguely similar illustration on here, , had utilize if() , paste() commands build string of file name - seems much arbitrary work, unless i'm beingness lazy...

any help appreciated.

you can create function 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 function basename of csv file. in particular example, has in c:/ folder. if file in other folder, have alter "c:/" in function folder csv file located.

running function:

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

note arguments has within quotes. 'data' contain columns year, complex , tot_male of gsg_lek.csv corresponding year 2006

r function csv command-line-arguments

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -