函数计算数据中的循环数并将其分组为r [英] function to count the number of loops in data and group them in r

查看:44
本文介绍了函数计算数据中的循环数并将其分组为r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有循环数据,该循环数据在数据中形成循环,如示例数据帧 df 中所提供:

I have cyclic data that forms loops in the data, as provided in the sample dataframe df:

> dput(df)
structure(list(Actuator.Force = c(-4853.5854, -4566.9771, -4198.7612, 
-3774.5527, -3317.6958, -2847.5229, -2364.7585, -1880.9485, -1405.4272, 
-930.289, -467.04822, -18.867363, 421.17499, 838.86719, 1239.9121, 
1626.0669, 1990.6389, 2334.0852, 2655.344, 2962.0227, 3243.7817, 
3506.2249, 3744.2622, 3959.8271, 4156.7061, 4324.9048, 4469.229, 
4591.6689, 4687.4194, 4764.0801, 4814.6167, 4840.313, 4846.0181, 
4826.3135, 4777.6553, 4696.0791, 4583.854, 4442.457, 4272.5254, 
4076.7224, 3851.1211, 3603.1853, 3330.7456, 3038.3157, 2724.115, 
2386.5476, 2032.5809, 1660.0547, 1268.0084, 859.16675, 432.4075, 
-14.131592, -479.29309, -955.67108, -1444.614, -1937.2562, -2437.0085, 
-2941.8914, -3450.9009, -3959.9597, -4468.9795, -4981.2549, -5492.6997, 
-6002.334, -6510.5425, -7016.2432, -7517.8286, -8013.1348, -8500.4199, 
-8974.8867, -9439.5479, -9890.5938, -10326.367, -10744.421, -11147.754, 
-11534.83, -11902.651, -12248.997, -12577.919, -12885.458, -13172.309, 
-13441.554, -13691.502, -13922.634, -14127.116, -14305.272, -14458.267, 
-14582.934, -14685.274, -14758.539, -14806.058, -14830.719, -14836.625, 
-14822.204, -14773.916, -14700.484, -14597.968, -14469.834, -14312.099, 
-14126.422, -13915.136, -13676.505, -13412.388, -13120.703, -12807.961, 
-12473.883, -12115.751, -11740.082, -11342.633, -10929.945, -10502.158, 
-10062.869, -9611.8271, -9146.6006, -8673.3545, -8191.7417, -7700.769, 
-7200.9346, -6695.8809, -6185.2378, -5670.8711, -5154.9995, -4643.4414, 
-4135.0015, -3629.2859, -3125.657, -2626.541, -2134.0662, -1646.4242, 
-1168.816, -699.63068, -245.34488, 192.7984, 618.76703, 1033.223, 
1428.922, 1807.2645, 2165.6274, 2507.6655, 2826.2754, 3120.4724, 
3395.2593, 3647.6946, 3879.4983, 4086.3855, 4265.1323, 4421.6831, 
4554.3594, 4657.8184, 4736.9561, 4792.6724, 4822.3784, 4830.3091, 
4815.9038, 4773.9692, 4706.4736, 4614.8379, 4491.3198, 4337.8892, 
4158.002, 3949.3147, 3713.4622, 3453.9114, 3167.8179, 2861.2598, 
2536.3259, 2187.3623, 1822.752, 1437.5449, 1034.8208, 617.23962, 
183.35637, -270.79733, -738.95618, -1220.1345, -1710.7787, -2206.1941, 
-2706.4871, -3210.8625, -3721.0002, -4233.6387, -4747.7271, -5258.7578, 
-5771.3071, -6280.7759, -6791.0166, -7295.0229, -7794.4199, -8287.4189, 
-8771.6377, -9243.3457, -9702.2559, -10146.865, -10577.053, -10989.863, 
-11385.981, -11760.477, -12116.938, -12456.351, -12772.688, -13071.995, 
-13349.56, -13611.837, -13852.025, -14066.192, -14254.534, -14420.322, 
-14556.846, -14668.379, -14753.795, -14815.698, -14857.507, -14872.669, 
-14865.908, -14836.117, -14774.959, -14686.203, -14569.189, -14426.374, 
-14253.839, -14054.1, -13825.626, -13570.092, -13290.482, -12987.772, 
-12664.012, -12316.427, -11948.471, -11560.391, -11154.085, -10733.564, 
-10299.443, -9853.6875, -9396.6436, -8925.8535, -8443.9336, -7956.8374, 
-7458.6333, -6954.7061, -6445.8086, -5929.9316, -5419.6182, -4908.4502, 
-4398.2227, -3885.9087, -3379.0095, -2874.7212, -2376.6907, -1884.6035, 
-1400.1414, -926.74658, -463.98688, -15.092632, 421.02869, 838.7132, 
1241.4564, 1630.713, 1999.5801, 2349.6597, 2678.7043, 2986.6536, 
3271.0425, 3535.8906, 3775.0059, 3991.6484, 4185.4834, 4354.6094, 
4495.666, 4610.7051, 4702.4521, 4770.9893, 4813.6733, 4832.2402, 
4830.335, 4802.6479, 4745.7451, 4660.7607, 4547.5942, 4406.2202, 
4236.4312, 4036.448, 3812.7585, 3562.6196, 3288.6797, 2993.6304, 
2673.9656, 2336.5691, 1977.1057, 1605.335, 1214.9467, 806.66827, 
380.42252, -62.247063, -522.36572, -996.21539, -1477.8905, -1969.7305, 
-2469.0476, -2971.5452, -3479.3577, -3992.8628, -4506.252, -5025.1992, 
-5545.3721, -6064.1323, -6576.875, -7086.855, -7590.6704, -8085.0815, 
-8567.9775, -9043.2002, -9507.4824, -9958.1904, -10392.594, -10814.161, 
-11215.28, -11600.177, -11967.055, -12312.813, -12642.992, -12948.593, 
-13238.12, -13509.343, -13760.307, -13987.689, -14191.591, -14364.724, 
-14514.282, -14638.64, -14738.083, -14811.377, -14862.124, -14888.534, 
-14893.003, -14876.485, -14828.384, -14752.178, -14650.495, -14518.901, 
-14358.499, -14171.098, -13956.202, -13714.101, -13446.276, -13151.266, 
-12836.099, -12495.902, -12137.276, -11756.888, -11357.116, -10942.688, 
-10514.518, -10073.99, -9619.6875, -9154.2783, -8679.9385, -8195.3867, 
-7699.6299, -7196.3784, -6689.6313, -6176.3032, -5664.7334),
Rotation = c(-0.005985651, -0.00565783616666667, -0.00522075016666667, 
-0.0046743925, -0.00406732866666667, -0.00343598223333333, 
-0.00286534205, -0.00219757165, -0.00156622503333333, -0.000934878566666667, 
-0.000267108158333333, 0.000303531998333333, 0.00084988955, 
0.0013962471, 0.00193046351666667, 0.00242825596666667, 0.00288962463333333, 
0.0033995583, 0.0038366445, 0.00424944783333333, 0.004637969, 
0.0050507725, 0.005378587, 0.00565783616666667, 0.00594922716666667, 
0.00620419383333333, 0.006410596, 0.0065684325, 0.00670198666666667, 
0.00683554116666667, 0.0069205295, 0.00699337683333333, 0.0070055185, 
0.006993377, 0.00696909483333333, 0.00688410516666667, 0.006774834, 
0.00659271483333333, 0.006386313, 0.00613134633333333, 0.0058399555, 
0.0055364235, 0.00518432633333333, 0.00483222916666667, 0.0044072845, 
0.00403090483333333, 0.00353311216666667, 0.0029988961, 0.00251324506666667, 
0.0020275938, 0.00144481233333333, 0.00086203085, 0.000303531998333333, 
-0.000315673273333333, -0.000983443666666667, -0.00162693151666667, 
-0.00233112578333333, -0.0029988961, -0.00366666666666667, 
-0.00433443683333333, -0.00496578316666667, -0.00563355366666667, 
-0.0062770415, -0.0069690945, -0.0076611475, -0.00836534183333333, 
-0.00902097083333333, -0.00968874116666667, -0.0103443703333333, 
-0.0109514346666667, -0.011594922, -0.012177704, -0.0127969093333333, 
-0.0133918318333333, -0.0139746131666667, -0.0145209698333333, 
-0.014982339, -0.0154437081666667, -0.0159050765, -0.0163178798333333, 
-0.0167185421666667, -0.0170706398333333, -0.0174105943333333, 
-0.0177505506666667, -0.018017659, -0.0182483433333333, -0.0184547455, 
-0.0186490056666667, -0.0187704183333333, -0.01887969, -0.0189525386666667, 
-0.018988962, -0.0190011033333333, -0.018988962, -0.0189768206666667, 
-0.0189282553333333, -0.0188189838333333, -0.018673289, -0.018442604, 
-0.0182240605, -0.017993377, -0.0176534206666667, -0.0173256068333333, 
-0.0169492263333333, -0.0165485635, -0.0161357608333333, 
-0.0156622516666667, -0.0152373058333333, -0.0147152313333333, 
-0.0141931568333333, -0.0136103748333333, -0.0130275935, 
-0.0123962468333333, -0.0118013235, -0.0112064015, -0.0105507718333333, 
-0.00993156683333333, -0.0092637965, -0.008620309, -0.00791611466666667, 
-0.00719977883333333, -0.0065441495, -0.00591280316666667, 
-0.00526931516666667, -0.00461368616666667, -0.00395805733333333, 
-0.00329028698333333, -0.00263465763333333, -0.00193046343333333, 
-0.00126269308333333, -0.00063134655, -1.21412798333333e-05, 
0.0005463576, 0.00111699776666667, 0.00169977918333333, 0.00220971298333333, 
0.00269536395, 0.00316887395, 0.003642384, 0.00409161133333333, 
0.0044437085, 0.0048443705, 0.005172185, 0.00548785833333333, 
0.0057549665, 0.00598565083333333, 0.00624061783333333, 0.00639845466666667, 
0.00653200816666667, 0.00664128016666667, 0.00671412783333333, 
0.0067626925, 0.00678697583333333, 0.00678697583333333, 0.00676269316666667, 
0.00670198616666667, 0.00660485583333333, 0.00645916083333333, 
0.00630132416666667, 0.00604635716666667, 0.00577924883333333, 
0.0054999995, 0.005172185, 0.00478366416666667, 0.004383002, 
0.0040187635, 0.0035331125, 0.00303531981666667, 0.00253752743333333, 
0.00205187631666667, 0.00150551863333333, 0.000947019833333333, 
0.000376379673333333, -0.000230684318333333, -0.0008984547, 
-0.00154194248333333, -0.00219757163333333, -0.00287748321666667, 
-0.00353311226666667, -0.00421302416666667, -0.00486865333333333, 
-0.00552428216666667, -0.0062163355, -0.0068598235, -0.00753973483333333, 
-0.00824392883333333, -0.00891169966666667, -0.00951876366666667, 
-0.010186534, -0.010805739, -0.0114370858333333, -0.0120320083333333, 
-0.012639073, -0.0132218531666667, -0.0137803523333333, -0.0143388515, 
-0.0148245028333333, -0.0152980126666667, -0.0157108165, 
-0.0161479028333333, -0.0165485635, -0.0169249445, -0.0172770415, 
-0.0176048558333333, -0.0179083875, -0.0181147896666667, 
-0.0183576146666667, -0.0185397348333333, -0.018697571, -0.0188189831666667, 
-0.0188918313333333, -0.0189525373333333, -0.0190011033333333, 
-0.0189768206666667, -0.0189768206666667, -0.0189525386666667, 
-0.0188432665, -0.0187461363333333, -0.0185761588333333, 
-0.0183211913333333, -0.0180783656666667, -0.0178112581666667, 
-0.0174713018333333, -0.0171070633333333, -0.0167306835, 
-0.0163178798333333, -0.0158565118333333, -0.0154072848333333, 
-0.0149459155, -0.0144359823333333, -0.0138653411666667, 
-0.0132825598333333, -0.0126876383333333, -0.0120320083333333, 
-0.0114613678333333, -0.0107935976666667, -0.0101986748333333, 
-0.00953090416666667, -0.00892384083333333, -0.00824392883333333, 
-0.00755187583333333, -0.0068476815, -0.00622847633333333, 
-0.00551214116666667, -0.00488079433333333, -0.00423730666666667, 
-0.0035452535, -0.00291390716666667, -0.00223399545, -0.00156622503333333, 
-0.000934878566666667, -0.000267108155, 0.000291390715, 0.00084988955, 
0.00142052971666667, 0.00197902856666667, 0.00244039716666667, 
0.00295033088333333, 0.003435982, 0.0038852095, 0.00428587166666667, 
0.0046622515, 0.00502648983333333, 0.005342163, 0.0056335535, 
0.0058885205, 0.00608278116666667, 0.00630132416666667, 0.0064470195, 
0.00656843216666667, 0.00667770383333333, 0.00671412766666667, 
0.0067626925, 0.00676269316666667, 0.00677483416666667, 0.00672626866666667, 
0.00664128016666667, 0.00653200816666667, 0.0063620305, 0.00611920466666667, 
0.00587637933333333, 0.00560927116666667, 0.00528145666666667, 
0.004953642, 0.00454083866666667, 0.00415231766666667, 0.0037030905, 
0.00324172168333333, 0.00274392916666667, 0.00224613676666667, 
0.00173620305, 0.00118984546666667, 0.0006192053, 1.214128e-05, 
-0.000582781433333333, -0.00123841056666667, -0.0019061809, 
-0.00258609256666667, -0.00322958058333333, -0.0039216335, 
-0.00457726266666667, -0.005257174, -0.00591280316666667, 
-0.0065320085, -0.00727262683333333, -0.00796467966666667, 
-0.00864459116666667, -0.00928807916666667, -0.00990728416666667, 
-0.0105629131666667, -0.0111942596666667, -0.0118013241666667, 
-0.0123841055, -0.0130033101666667, -0.0135496686666667, 
-0.0140838846666667, -0.0146181005, -0.0150916111666667, 
-0.0155165561666667, -0.0159657828333333, -0.0163785865, 
-0.0167792476666667, -0.0171192046666667, -0.0174591603333333, 
-0.017774834, -0.018066225, -0.0182604846666667, -0.0184668873333333, 
-0.0186490056666667, -0.0187704185, -0.0188675491666667, 
-0.0189525373333333, -0.0190011033333333, -0.0190253848333333, 
-0.019013244, -0.0189889613333333, -0.0189161133333333, -0.0188189838333333, 
-0.0186732876666667, -0.0184547455, -0.0182119198333333, 
-0.0179690948333333, -0.017629138, -0.0172891835, -0.0169128038333333, 
-0.0165364226666667, -0.0160750548333333, -0.0156258271666667, 
-0.0151765998333333, -0.0146666665, -0.01413245, -0.0135375268333333, 
-0.0129668866666667, -0.0123355401666667, -0.0117284768333333, 
-0.0111092713333333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 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), 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", "301", "302", "303", "304", "305", "306", "307", 
"308", "309", "310", "311", "312", "313", "314", "315", "316", 
"317", "318", "319", "320", "321", "322", "323", "324", "325", 
"326", "327", "328", "329", "330", "331", "332", "333", "334", 
"335", "336", "337", "338", "339", "340", "341", "342", "343", 
"344", "345", "346", "347", "348", "349", "350", "351", "352", 
"353", "354", "355", "356", "357", "358", "359", "360", "361", 
"362"), class = "data.frame")

我想编写一个函数,该函数首先按行号按循环对数据进行分组.

I want to write a function that first groups the data by loops by the row numbers.

为此示例数据帧提供了三个数据循环,如下所示:

There are three loops of data provided for this sample data frame as shown:

ggplot(data = df, aes(Rotation, Actuator.Force)) + 
  geom_path(aes(group = as.factor(loop), colour = as.factor(loop))) +
  labs(colour = "Loops")

原始数据没有 loop 列,我需要一个可以按循环将数据分组的函数.

The original data does not have the loop column and I need a function that would group the data by loops.

循环的定义是指从数据开始一直到最大x,再回到最小x以及恰好在到达循环的起始x点之前的所有点.

The definition of a loop is all the points from the beginning of the data all the way to the maximum x, back to the minimum x and right before it reaches the starting x point of the loop.

所需的输出是一个函数,该函数将基于 Rotation Actuator.Force 列生成 loop 列.

The desired output is a function that would produce the loop column based on the Rotation and Actuator.Force columns.

推荐答案

这与绘图本身并没有任何关系.从此处汲取灵感,您可以计算数据的周期并尝试使用该周期来估计循环.在下面的示例中,我仅使用 Rotation 数据,并且在估计的循环和实际的循环之间得到了1个不匹配,我可以接受.

This doesn't really have anything to do with plotting per se. Taking inspiration from here, you can calculate the period of your data and try to use that to estimate the loop. In the example below, I only use the Rotation data and get 1 mismatch between the estimated loop and the actual loop, which I'm OK with.

# Use spectrum methods to estimate period
ssp <- spectrum(df$Rotation, plot = FALSE, method = "ar", n.freq = 1e4)
period <- 1 / with(ssp, freq[spec == max(spec)])

# How many full periods can I fit in my rows (+ 1)
estimate <- (seq_len(nrow(df)) %/% period) + 1

table(estimate, loop = df$loop)
#>         loop
#> estimate   1   2   3
#>        1 121   0   0
#>        2   1 120   0
#>        3   0   0 120

我不知道这对其他任何周期性数据的推广程度如何.

I don't know how well this generalises to whatever other periodic data.

这篇关于函数计算数据中的循环数并将其分组为r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆