r - Changing values within a function -


i have following function works fine:

d16<-function(x) {   delay<-difftime(tail(x$date.time[x$station == "l4"],1),               x$date.time[x$station == "l4"][1],units="mins")    return (delay) } 

i have many functions same "l4" changes different value e.g. "l5" , be:

d17<-function(x) {   delay<-difftime(tail(x$date.time[x$station == "l5"],1),               x$date.time[x$station == "l5"][1],units="mins")    return (delay) } 

is possible write code changes automatically, rather writing function on , on again

some sample data:

structure(list(date.time = structure(c(1365923863, 1365923895,  1365923931, 1365923950, 1365923965, 1368143290, 1368143310, 1368143370,  1368164838, 1368165029, 1368165346, 1368165376, 1368165474, 1368165497,  1368165536, 1368165574, 1368165608, 1368165626, 1368165661, 1368165719,  1368165736, 1368165858, 1368165923, 1368165952, 1368165991, 1368175156,  1368175173, 1368175193), class = c("posixct", "posixt"), tzone = ""),  station = c("l4", "l4", "l4", "l4", "l4", "l5", "l5",  "l5", "l5", "l5", "l5", "l5", "l5", "l5", "l5", "l5",  "l5", "l5", "l5", "l5", "l5", "l5", "r05", "l5", "l5",  "l5", "l5", "l5"), code = c(10897, 10897, 10897, 10897,  10897, 10897, 10897, 10897, 10897, 10897, 10897, 10897, 10897,  10897, 10897, 10897, 10897, 10897, 10897, 10897, 10897, 10897,  10897, 10897, 10897, 10897, 10897, 10897)), .names = c("date.time",  "station", "code"), row.names = c(26l, 27l, 28l, 29l, 30l, 3038l,  3039l, 3040l, 3059l, 3060l, 3061l, 3062l, 3063l, 3064l, 3065l,  3066l, 3067l, 3068l, 3069l, 3070l, 3071l, 3072l, 3073l, 3074l,  3075l, 3076l, 3077l, 3078l), class = "data.frame") 

i think might useful you, since looks taking difftimes each station. need run install.packages("data.table") first.

require(data.table) dt <- data.table(x) dt[,difftime(date.time[1],date.time[.n],units="mins")[[1]],by=station] 

the result data.frame/data.table listing stations , delays. default, delay column named "v1". can alter last line set custom name:

dt[,list(     delay=difftime(date.time[1],date.time[.n],units="mins")[[1]] ),by=station] 

here's example. data...

set.seed(1) x <- data.frame(     date.time=sample(seq.date(as.date("2013-05-18"),as.date("2013-06-02"),1),10),     station=rep(c("a","b"),5),     stringsasfactors=false ) 

i result:

   station  delay 1:       -12960 2:       b   7200 

probably data sorted delays have same sign, if not, can set keys data table , columns sorted them: setkey(dt,station,date.time).

to matrix of difftimes, can use base r function outer:

firsts <- dt[,date.time[1],by=station][,{names(v1)<-station;v1}] lasts <- dt[,date.time[.n],by=station][,{names(v1)<-station;v1}] outer(firsts,lasts,difftime,units="mins") 

which gives

          b -12960 5760 b -11520 7200 

unfortunately, difftime gives weird output, information in well-behaved data.frame, we'll need roll new function:

my_difftime <- vectorize(function(x,y)difftime(x,y,units="mins")[[1]]) diffs <- as.data.frame(outer(firsts,lasts,my_difftime)) 

with (newly posted) sample data, get

          l4          l5         r05 l4     -1.70 -37522.1667 -37367.6667 l5  36988.75   -531.7167   -377.2167 r05 37365.97   -154.5000      0.0000 

each entry in matrix shows difftime between first observation of row station , last observation of column station.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -