我有以下工作正常的功能:
d16<-function(x) {
delay<-difftime(tail(x$date.time[x$station == "L4"],1),
x$date.time[x$station == "L4"][1],units="mins")
return (delay)
}
我有许多与此完全相同的功能,但是“L4”更改为不同的值,例如“L5”,因此将是:
d17<-function(x) {
delay<-difftime(tail(x$date.time[x$station == "L5"],1),
x$date.time[x$station == "L5"][1],units="mins")
return (delay)
}
是否可以将其写入代码以便它自动更改,而不是一遍又一遍地编写函数
一些样本数据:
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")