我的数据框如下:
test <- structure(list(datetime = structure(c(1325394000, 1325394360,
1325394720, 1325395080, 1325395440, 1325395800, 1325396160, 1325396520,
1325396880, 1325397240, 1325397600, 1325397960, 1325398320, 1325398680,
1325399040, 1325399400, 1325399760, 1325400120, 1325400480, 1325400840,
1325401200, 1325401560, 1325401920, 1325402280, 1325402640, 1325403000,
1325403360, 1325403720, 1325404080, 1325404440, 1325404800, 1325405160,
1325405520, 1325405880, 1325406240, 1325406600, 1325406960, 1325407320,
1325407680, 1325408040, 1325408400, 1325408760, 1325409120, 1325409480,
1325409840, 1325410200, 1325410560, 1325410920, 1325411280, 1325411640,
1325412000, 1325412360, 1325412720, 1325413080, 1325413440, 1325413800,
1325414160, 1325414520, 1325414880, 1325415240, 1325415600, 1325415960,
1325416320, 1325416680, 1325417040, 1325417400, 1325417760, 1325418120,
1325418480, 1325418840, 1325419200, 1325419560, 1325419920, 1325420280,
1325420640, 1325421000, 1325421360, 1325421720, 1325422080, 1325422440,
1325422800, 1325423160, 1325423520, 1325423880, 1325424240, 1325424600,
1325424960, 1325425320, 1325425680, 1325426040, 1325426400, 1325426760,
1325427120, 1325427480, 1325427840, 1325428200, 1325428560, 1325428920,
1325429280, 1325429640, 1325430000, 1325430360, 1325430720, 1325431080,
1325431440, 1325431800, 1325432160, 1325432520, 1325432880, 1325433240,
1325433600, 1325433960, 1325434320, 1325434680, 1325435040, 1325435400,
1325435760, 1325436120, 1325436480, 1325436840, 1325437200, 1325437560,
1325437920, 1325438280, 1325438640, 1325439000, 1325439360, 1325439720,
1325440080, 1325440440, 1325440800, 1325441160, 1325441520, 1325441880,
1325442240, 1325442600, 1325442960, 1325443320, 1325443680, 1325444040,
1325444400, 1325444760, 1325445120, 1325445480, 1325445840, 1325446200,
1325446560, 1325446920, 1325447280, 1325447640, 1325448000, 1325448360,
1325448720, 1325449080, 1325449440, 1325449800, 1325450160, 1325450520,
1325450880, 1325451240, 1325451600, 1325451960, 1325452320, 1325452680,
1325453040, 1325453400, 1325453760, 1325454120, 1325454480, 1325454840,
1325455200, 1325455560, 1325455920, 1325456280, 1325456640, 1325457000,
1325457360, 1325457720, 1325458080, 1325458440, 1325458800, 1325459160,
1325459520, 1325459880, 1325460240, 1325460600, 1325460960, 1325461320,
1325461680, 1325462040, 1325462400, 1325462760, 1325463120, 1325463480,
1325463840, 1325464200, 1325464560, 1325464920, 1325465280, 1325465640,
1325466000, 1325466360, 1325466720, 1325467080, 1325467440, 1325467800,
1325468160, 1325468520, 1325468880, 1325469240, 1325469600, 1325469960,
1325470320, 1325470680, 1325471040, 1325471400, 1325471760, 1325472120,
1325472480, 1325472840, 1325473200, 1325473560, 1325473920, 1325474280,
1325474640, 1325475000, 1325475360, 1325475720, 1325476080, 1325476440,
1325476800, 1325477160, 1325477520, 1325477880, 1325478240, 1325478600,
1325478960, 1325479320, 1325479680, 1325480040, 1325480400, 1325480760,
1325481120, 1325481480, 1325481840, 1325482200, 1325482560, 1325482920,
1325483280, 1325483640, 1325484000, 1325484360, 1325484720, 1325485080,
1325485440, 1325485800, 1325486160, 1325486520, 1325486880, 1325487240,
1325487600, 1325487960, 1325488320, 1325488680, 1325489040, 1325489400,
1325489760, 1325490120, 1325490480, 1325490840, 1325491200, 1325491560,
1325491920, 1325492280, 1325492640, 1325493000, 1325493360, 1325493720,
1325494080, 1325494440, 1325494800, 1325495160, 1325495520, 1325495880,
1325496240, 1325496600, 1325496960, 1325497320, 1325497680, 1325498040,
1325498400, 1325498760, 1325499120, 1325499480, 1325499840, 1325500200,
1325500560, 1325500920, 1325501280, 1325501640),
class = c("POSIXct", "POSIXt"), tzone = "America/New_York"),
predicted = c(3.448,
3.508, 3.566, 3.622, 3.675, 3.726, 3.775, 3.82, 3.863, 3.902,
3.938, 3.97, 3.999, 4.023, 4.044, 4.06, 4.072, 4.079, 4.081,
4.078, 4.07, 4.057, 4.038, 4.015, 3.985, 3.951, 3.91, 3.865,
3.815, 3.759, 3.699, 3.634, 3.565, 3.493, 3.417, 3.338, 3.256,
3.173, 3.089, 3.003, 2.917, 2.832, 2.747, 2.663, 2.581, 2.502,
2.424, 2.349, 2.277, 2.207, 2.141, 2.078, 2.017, 1.96, 1.905,
1.852, 1.802, 1.754, 1.707, 1.661, 1.616, 1.572, 1.528, 1.485,
1.442, 1.398, 1.355, 1.311, 1.268, 1.225, 1.182, 1.14, 1.099,
1.06, 1.023, 0.988, 0.957, 0.928, 0.904, 0.884, 0.868, 0.858,
0.853, 0.854, 0.86, 0.873, 0.892, 0.916, 0.947, 0.983, 1.024,
1.071, 1.123, 1.179, 1.239, 1.302, 1.369, 1.438, 1.51, 1.583,
1.658, 1.734, 1.81, 1.887, 1.963, 2.04, 2.115, 2.19, 2.265, 2.338,
2.41, 2.481, 2.551, 2.62, 2.688, 2.754, 2.819, 2.883, 2.945,
3.006, 3.066, 3.124, 3.18, 3.235, 3.288, 3.339, 3.388, 3.435,
3.48, 3.522, 3.561, 3.598, 3.632, 3.662, 3.689, 3.713, 3.733,
3.749, 3.761, 3.769, 3.772, 3.77, 3.764, 3.753, 3.737, 3.716,
3.69, 3.658, 3.621, 3.578, 3.531, 3.478, 3.42, 3.358, 3.29, 3.219,
3.143, 3.064, 2.982, 2.897, 2.809, 2.72, 2.63, 2.539, 2.448,
2.357, 2.267, 2.179, 2.093, 2.008, 1.927, 1.848, 1.773, 1.701,
1.632, 1.566, 1.504, 1.445, 1.389, 1.337, 1.286, 1.238, 1.192,
1.148, 1.106, 1.064, 1.024, 0.984, 0.945, 0.906, 0.868, 0.83,
0.793, 0.756, 0.721, 0.686, 0.654, 0.623, 0.595, 0.57, 0.548,
0.53, 0.516, 0.507, 0.503, 0.504, 0.512, 0.525, 0.545, 0.571,
0.603, 0.641, 0.686, 0.737, 0.793, 0.855, 0.921, 0.993, 1.068,
1.147, 1.23, 1.315, 1.402, 1.491, 1.581, 1.672, 1.763, 1.854,
1.945, 2.036, 2.125, 2.213, 2.3, 2.386, 2.47, 2.553, 2.634, 2.713,
2.79, 2.866, 2.94, 3.012, 3.082, 3.15, 3.217, 3.281, 3.344, 3.405,
3.463, 3.52, 3.574, 3.626, 3.676, 3.723, 3.767, 3.809, 3.847,
3.883, 3.916, 3.945, 3.971, 3.993, 4.012, 4.026, 4.037, 4.044,
4.046, 4.044, 4.037, 4.026, 4.01, 3.989, 3.963, 3.933, 3.897,
3.857, 3.813, 3.763, 3.71, 3.652, 3.591, 3.526, 3.458, 3.387,
3.314, 3.239, 3.163, 3.086, 3.008, 2.93, 2.853, 2.777, 2.702,
2.628, 2.556, 2.487, 2.42, 2.355, 2.293, 2.233),
type = structure(c(NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("", "high", "low"), class = "factor"),
datehell = c(NA, "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "8400", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "9900", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "8400", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "9900", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "8400", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "")),
.Names = c("datetime", "predicted", "type", "datehell"),
row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92",
"93", "94", "95", "96", "97", "98", "99", "100", "101", "102",
"103", "104", "105", "106", "107", "108", "109", "110", "111",
"112", "113", "114", "115", "116", "117", "118", "119", "120",
"121", "122", "123", "124", "125", "126", "127", "128", "129",
"130", "131", "132", "133", "134", "135", "136", "137", "138",
"139", "140", "141", "142", "143", "144", "145", "146", "147",
"148", "149", "150", "151", "152", "153", "154", "155", "156",
"157", "158", "159", "160", "161", "162", "163", "164", "165",
"166", "167", "168", "169", "170", "171", "172", "173", "174",
"175", "176", "177", "178", "179", "180", "181", "182", "183",
"184", "185", "186", "187", "188", "189", "190", "191", "192",
"193", "194", "195", "196", "197", "198", "199", "200", "201",
"202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "213", "214", "215", "216", "217", "218", "219",
"220", "221", "222", "223", "224", "225", "226", "227", "228",
"229", "230", "231", "232", "233", "234", "235", "236", "237",
"238", "239", "240", "241", "242", "243", "244", "245", "246",
"247", "248", "249", "250", "251", "252", "253", "254", "255",
"256", "257", "258", "259", "260", "261", "262", "263", "264",
"265", "266", "267", "268", "269", "270", "271", "272", "273",
"274", "275", "276", "277", "278", "279", "280", "281", "282",
"283", "284", "285", "286", "287", "288", "289", "290", "291",
"292", "293", "294", "295", "296", "297", "298", "299", "300")
, class = "data.frame")
数据集的头部如下所示:
> head(test)
datetime predicted type datehell
1 2012-01-01 00:00:00 3.448 <NA> <NA>
2 2012-01-01 00:06:00 3.508
3 2012-01-01 00:12:00 3.566
4 2012-01-01 00:18:00 3.622
5 2012-01-01 00:24:00 3.675
6 2012-01-01 00:30:00 3.726
type
列正在检查maxima
列的本地minima
和predict
。如果是minima
,则type
为“低”,当为“最大值”时,类型为“高”。
我需要转换datetime
,这样当type =“high”时,我需要添加140分钟,当type =“low”时,我需要添加165分钟。在日期地狱栏中,我有适当的分钟数,以便我可以添加到相应的日期。
对于既没有“高”也没有“低”datetime
的剩余type
,我需要在高潮和低潮之间进行线性插值以找到适当的时移。
例如,第19行是涨潮,datehell = 8400,第83行是退潮,datehell = 9900.因此,在第19行和第83行之间,我需要进行线性插值以填充空白的helltide列。对于其余的数据,我需要重复同样的事情。高低和低低之间的所有列都需要用线性插值填充。
如果不清楚,请告诉我。
从第19行到第83行,在添加列datetime和datehell之后,预期输出为
1/1/12 4:08 AM
1/1/12 4:14 AM
1/1/12 4:20 AM
1/1/12 4:27 AM
1/1/12 4:33 AM
1/1/12 4:39 AM
1/1/12 4:46 AM
1/1/12 4:52 AM
1/1/12 4:59 AM
1/1/12 5:05 AM
1/1/12 5:11 AM
1/1/12 5:18 AM
1/1/12 5:24 AM
1/1/12 5:31 AM
1/1/12 5:37 AM
1/1/12 5:43 AM
1/1/12 5:50 AM
1/1/12 5:56 AM
1/1/12 6:03 AM
1/1/12 6:09 AM
1/1/12 6:15 AM
1/1/12 6:22 AM
1/1/12 6:28 AM
1/1/12 6:34 AM
1/1/12 6:41 AM
1/1/12 6:47 AM
1/1/12 6:54 AM
1/1/12 7:00 AM
1/1/12 7:06 AM
1/1/12 7:13 AM
1/1/12 7:19 AM
1/1/12 7:26 AM
1/1/12 7:32 AM
1/1/12 7:38 AM
1/1/12 7:45 AM
1/1/12 7:51 AM
1/1/12 7:58 AM
1/1/12 8:04 AM
1/1/12 8:10 AM
1/1/12 8:17 AM
1/1/12 8:23 AM
1/1/12 8:30 AM
1/1/12 8:36 AM
1/1/12 8:42 AM
1/1/12 8:49 AM
1/1/12 8:55 AM
1/1/12 9:01 AM
1/1/12 9:08 AM
1/1/12 9:14 AM
1/1/12 9:21 AM
1/1/12 9:27 AM
1/1/12 9:33 AM
1/1/12 9:40 AM
1/1/12 9:46 AM
1/1/12 9:53 AM
1/1/12 9:59 AM
1/1/12 10:05 AM
1/1/12 10:12 AM
1/1/12 10:18 AM
1/1/12 10:25 AM
1/1/12 10:31 AM
1/1/12 10:37 AM
1/1/12 10:44 AM
1/1/12 10:50 AM
1/1/12 10:57 AM
答案 0 :(得分:1)
这是一个计算线性插值的循环:
BBB<CCC>
答案 1 :(得分:1)
#first set the first and last rows as appropriate
test[1,3]<-"low"
test[1,4]<-9900
test[300,3]<-"low"
test[300,4]<-9900
#find and save all the local extrema
highlows<-which(as.character(test$type) %in% c("high","low"))
#loop through the thresholds
for(Ind in highlows[1:(length(highlows)-1)]) {
nextpos <- highlows[which(highlows==Ind)+1]
#this sets the whole vector to the new values at once
test[(Ind+1):(nextpos-1),4]<-as.numeric(test[Ind,4])+((as.numeric(test[nextpos,4])-as.numeric(test[Ind,4]))/(nextpos-Ind))*1:(nextpos-Ind-1)
}
test$datetime<-test$datetime+as.numeric(test$datehell)