function - select a row of matrix based on Date in R -


i'm new in r.i have small question. want select 1 row based on date. write function doesn't work.here example.

here data sample:

 example      date b    1 01/01/2000 0.6765442 1    2 01/01/2001 0.1374231 10    3 01/01/2002 0.6917205 0    4 01/01/2003 0.1440152 5    5 01/01/2004 0.1143759 8    6 01/01/2005 0.3379550 7    7 01/01/2006 0.4477631 9    8 01/01/2007 0.6164849 6    9 01/01/2008 0.1232872 8   10 01/01/2009 0.3511471 6  

i tried using code:

 test=function(data,text)  {           ( in (1:col(data)))                {                if (data[i,1]=="text")                   { l=i                     r1=data[l,]                     return(r1)  }                    }  }  

you should use r-vectorized feature subset data. no need use for-loop here. example:

data[data$date == "01/01/2001",] ##         date          b ## 2 01/01/2001 0.1374231 10 

of course can wrap in function:

subset_by_date <-    function(date_txt,data)  data[data$date == date_txt,] 

ps : in function have typo error , should use text argument , not "text".

update check if col in data.frame before filtering

subset_by_date <-    function(date_txt,data,col="date") {           if (col  %in% colnames(data))              data[data[,col] == date_txt,]    } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -