我无法正确设置需要传递给函数的行。我有"计算功能"和"行选择功能"。我用来计算的功能正在做我想要的。
该函数必须首先是子集estado=3
(该部分正常)然后将正确的行号子集化并将它们传递给"计算函数"。
正如我所说,我选择的代码应该选择特定的行号(在本例中为1:11,23:23,46:56和68:78)。问题是我试图为任何数据帧的长度自动进行此操作。我应该得到这样的模式:rep(11,12)我的意思是我把前两个dfs之间的第一个11放到我传递给计算函数并且在前两个和下一个块之间丢弃12并循环直到数据帧是结束(见照片以获得更好的不受欢迎)。
行选择基于时间(您可以看到' sa tiempo
列,因此根据时间(t_muestreo,t_pasaje,t__entre)计算行数.I&# 39;我试图根据这个时间设置行号,以防万一我们必须在实验中更改它们。
pendientes_ensayo <- function(df){
pendientes = data.frame()
t_muestreo = 0.2
t_pasaje = 2
t_entre = 2.5
nro_filas_salto = (t_pasaje / t_muestreo)
nro_filas_entre = (t_entre/t_muestreo)
df_a_medir_completo = df[df$estado == 3,] # Me quedo solo con la parte que mido
tamanodf = dim(df_a_medir_completo)[1]
df$tiempo = as.numeric( df$tiempo)
cant_animales = length(names(df)) - 2 # tiempo y estado
pos_en_filas = 1
for(nro_ensayo in 1:(tamanodf/ (nro_filas_salto + nro_filas_entre) )) {
print( pos_en_filas )
print( pos_en_filas + nro_filas_salto )
df_a_medir = df_a_medir_completo[pos_en_filas:(pos_en_filas+nro_filas_salto),]
pendientes = rbind(pendientes,obtener_pendientes(df_a_medir))
pos_en_filas = pos_en_filas + nro_filas_salto + nro_filas_entre - 0.5
}
return(pendientes)
}
我得到的回报(打印行)就是这个
[1] 1
[1] 11
[1] 23
[1] 33
[1] 45
[1] 55
[1] 67
[1] 77
请注意,前2个子集(1:11和23:33是正确的)但45:55和67:77是一行异相。
要运行pendientes_ensayo功能,您需要
iti = 153
t_adaptacion = 600
t_pre_estimulo = 18
t_muestreo = 0.2
t_pasaje = 2
t_entre = 2.5
defasaje = 1
nro_ensayos = 2
regresion <- function(formula){
regresion = lm( formula )
ss = summary(regresion)
return(c( coef(regresion)[2] , ss$adj.r.squared))
}
sacar_columnas <- function (dataframe,columnas){
return(dataframe[,!(names(dataframe) %in% columnas)])
}
desplazar<- function(tiempo,dataframe){
matriz = as.matrix(dataframe)
matriz_dif = abs(diff(matriz )) # derivar
matriz_dif_inv = diffinv(matriz_dif) # integrar
return(data.frame(cbind(tiempo, matriz_dif_inv )))
}
obtener_pendientes <- function(df_a_medir){
df_a_medir2 =sacar_columnas(df_a_medir, c("estado","tiempo"))
mat_desplazamiento = desplazar(df_a_medir$tiempo,df_a_medir2)
pend_muestreo = apply(mat_desplazamiento,2,function(x) regresion(x~df_a_medir$tiempo))
return(pend_muestreo)
}
这是一个已经由df $ estado = 3
提交的数据框的示例dput(d)
structure(list(estado = c(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, 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), tiempo = c(618.2,
618.4, 618.6, 618.8, 619, 619.2, 619.4, 619.6, 619.8, 620, 620.2,
620.4, 620.6, 620.8, 621, 621.2, 621.4, 621.6, 621.8, 622, 622.2,
622.4, 622.6, 622.8, 623, 623.2, 623.4, 623.6, 623.8, 624, 624.2,
624.4, 624.6, 624.8, 625, 625.2, 625.4, 625.6, 625.8, 626, 626.2,
626.4, 626.6, 626.8, 627, 3138.2, 3138.4, 3138.6, 3138.8, 3139,
3139.2, 3139.4, 3139.6, 3139.8, 3140, 3140.2, 3140.4, 3140.6,
3140.8, 3141, 3141.2, 3141.4, 3141.6, 3141.8, 3142, 3142.2, 3142.4,
3142.6, 3142.8, 3143, 3143.2, 3143.4, 3143.6, 3143.8, 3144, 3144.2,
3144.4, 3144.6, 3144.8, 3145, 3145.2, 3145.4, 3145.6, 3145.8,
3146, 3146.2, 3146.4, 3146.6, 3146.8, 3147), UT2x45 = c(14663,
14662, 14661, 14659, 14657, 14654, 14652, 14650, 14651, 14651,
14651, 14650, 14648, 14647, 14645, 14644, 14641, 14640, 14640,
14640, 14640, 14640, 14639, 14639, 14638, 14638, 14637, 14637,
14637, 14636, 14636, 14635, 14634, 14632, 14631, 14629, 14628,
14628, 14626, 14625, 14625, 14625, 14624, 14624, 14624, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888,
10888, 10888, 10888, 10888, 10888, 10888, 10888, 10888), TR2x45 = c(14663,
14663, 14655, 14634, 14632, 14634, 14634, 14632, 14633, 14605,
14573, 14555, 14548, 14540, 14525, 14523, 14517, 14510, 14506,
14499, 14497, 14494, 14492, 14487, 14453, 14426, 14414, 14416,
14413, 14413, 14416, 14409, 14387, 14380, 14367, 14364, 14356,
14347, 14347, 14343, 14337, 14332, 14330, 14329, 14327, 13432,
13432, 13427, 13424, 13426, 13427, 13427, 13427, 13428, 13428,
13426, 13424, 13423, 13423, 13423, 13423, 13423, 13422, 13421,
13421, 13420, 13419, 13418, 13417, 13410, 13410, 13410, 13411,
13411, 13411, 13411, 13411, 13410, 13410, 13410, 13410, 13410,
13411, 13411, 13411, 13411, 13411, 13411, 13411, 13411), UT2x45.2 = c(15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510,
15510, 15510, 15510, 15510, 15510, 15510, 15510, 15510, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282,
15282, 15282, 15282, 15282, 15282, 15282, 15282, 15282), TR2x45.2 = c(17295,
17295, 17293, 17266, 17248, 17250, 17268, 17277, 17252, 17195,
17141, 17115, 17108, 17105, 17102, 17099, 17098, 17097, 17093,
17089, 17086, 17084, 17084, 17083, 17080, 17036, 16975, 16919,
16884, 16869, 16836, 16788, 16738, 16700, 16675, 16661, 16653,
16648, 16643, 16638, 16634, 16633, 16631, 16629, 16626, 24047,
24047, 24047, 24047, 24047, 24062, 24085, 24119, 24163, 24191,
24181, 24182, 24181, 24183, 24183, 24183, 24183, 24183, 24183,
24183, 24183, 24184, 24184, 24184, 24182, 24137, 24070, 24011,
23978, 23965, 23920, 23868, 23826, 23806, 23799, 23796, 23788,
23782, 23776, 23774, 23772, 23769, 23766, 23764, 23761), TR2x45.3 = c(14702,
14702, 14702, 14702, 14702, 14702, 14702, 14704, 14720, 14733,
14739, 14741, 14743, 14744, 14746, 14750, 14753, 14754, 14755,
14756, 14756, 14758, 14759, 14760, 14760, 14760, 14763, 14767,
14770, 14774, 14773, 14774, 14775, 14775, 14776, 14776, 14776,
14776, 14776, 14776, 14776, 14776, 14776, 14776, 14776, 14818,
14818, 14818, 14818, 14818, 14820, 14819, 14818, 14819, 14819,
14819, 14819, 14819, 14819, 14819, 14819, 14819, 14819, 14819,
14819, 14819, 14819, 14819, 14819, 14817, 14816, 14816, 14816,
14816, 14816, 14817, 14817, 14817, 14817, 14817, 14817, 14817,
14817, 14817, 14817, 14817, 14817, 14817, 14817, 14817), UT2x45.3 = c(18521,
18518, 18517, 18518, 18517, 18515, 18513, 18512, 18510, 18507,
18505, 18503, 18501, 18499, 18499, 18497, 18496, 18495, 18493,
18492, 18490, 18487, 18483, 18481, 18479, 18476, 18474, 18472,
18471, 18470, 18467, 18464, 18461, 18457, 18455, 18453, 18451,
18449, 18447, 18445, 18442, 18438, 18435, 18432, 18430, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204,
18204, 18204, 18204, 18204, 18204, 18204, 18204, 18204), TR2x45.1 = c(14052,
14049, 14013, 13961, 13907, 13874, 13861, 13844, 13801, 13742,
13680, 13627, 13585, 13563, 13547, 13536, 13524, 13511, 13497,
13487, 13477, 13466, 13457, 13446, 13406, 13352, 13295, 13255,
13248, 13237, 13211, 13163, 13110, 13066, 13034, 13015, 13000,
12980, 12969, 12958, 12944, 12931, 12927, 12919, 12910, 11396,
11394, 11355, 11301, 11247, 11210, 11198, 11195, 11164, 11130,
11081, 11038, 11011, 11003, 10991, 10982, 10974, 10966, 10960,
10954, 10949, 10944, 10941, 10934, 10892, 10839, 10789, 10748,
10729, 10722, 10697, 10656, 10611, 10574, 10547, 10534, 10525,
10516, 10509, 10502, 10494, 10489, 10485, 10482, 10476), UT2x45.1 = c(17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050,
17050, 17050, 17050, 17050, 17050, 17050, 17050, 17050, 17077,
17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077,
17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077, 17077,
17077, 17077, 17077, 17078, 17078, 17080, 17083, 17084, 17084,
17084, 17085, 17087, 17089, 17090, 17093, 17095, 17095, 17096,
17098, 17100, 17101, 17103, 17106, 17107, 17109, 17111), UT2x45.4 = c(16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434,
16434, 16434, 16434, 16434, 16434, 16434, 16434, 16434, 16823,
16824, 16825, 16825, 16825, 16825, 16825, 16826, 16826, 16826,
16826, 16826, 16826, 16826, 16824, 16824, 16824, 16824, 16826,
16827, 16828, 16828, 16828, 16828, 16829, 16829, 16830, 16831,
16832, 16832, 16832, 16832, 16833, 16833, 16834, 16835, 16834,
16833, 16833, 16833, 16833, 16834, 16834, 16835, 16836), TR2x45.4 = c(12488,
12488, 12486, 12471, 12448, 12435, 12430, 12423, 12418, 12370,
12307, 12247, 12190, 12136, 12095, 12069, 12045, 12025, 12015,
12001, 11994, 11985, 11978, 11967, 11931, 11885, 11817, 11761,
11714, 11667, 11624, 11574, 11519, 11475, 11438, 11405, 11375,
11354, 11330, 11313, 11303, 11284, 11272, 11265, 11255, 9056,
9056, 9056, 9056, 9056, 9056, 9056, 9056, 9054, 9039, 9006, 8994,
8983, 8969, 8968, 8964, 8957, 8950, 8951, 8949, 8943, 8936, 8935,
8934, 8920, 8885, 8841, 8808, 8778, 8752, 8715, 8654, 8595, 8549,
8524, 8500, 8488, 8477, 8461, 8458, 8447, 8441, 8436, 8426, 8421
), UT2x45.1.1 = c(12004, 12004, 12004, 12004, 12004, 12004, 12004,
12004, 12003, 12003, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002, 12002,
12002, 12002, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015, 11015,
11015, 11015), TR2x45.1.1 = c(20832, 20832, 20828, 20818, 20822,
20855, 20873, 20895, 20930, 20915, 20904, 20904, 20904, 20904,
20904, 20904, 20904, 20904, 20905, 20906, 20907, 20908, 20911,
20916, 20918, 20887, 20857, 20861, 20879, 20892, 20910, 20897,
20893, 20894, 20893, 20893, 20893, 20893, 20893, 20893, 20893,
20893, 20893, 20893, 20893, 22371, 22371, 22370, 22369, 22383,
22410, 22440, 22459, 22470, 22426, 22414, 22406, 22404, 22404,
22404, 22403, 22402, 22402, 22402, 22402, 22402, 22402, 22402,
22402, 22405, 22387, 22371, 22373, 22387, 22390, 22364, 22327,
22303, 22295, 22283, 22272, 22264, 22253, 22250, 22240, 22234,
22232, 22230, 22223, 22214), TR2x45.5 = c(11994, 11992, 11991,
11992, 11994, 11994, 11992, 11989, 11991, 11988, 11981, 11978,
11978, 11977, 11974, 11971, 11971, 11969, 11962, 11955, 11952,
11948, 11945, 11943, 11942, 11942, 11942, 11942, 11941, 11939,
11938, 11938, 11931, 11928, 11927, 11925, 11920, 11917, 11915,
11914, 11909, 11906, 11904, 11901, 11897, 3997, 3997, 3997, 3997,
3997, 3997, 3997, 3997, 3997, 3997, 3998, 3998, 3998, 3998, 3998,
3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998, 3998,
3998, 3998, 3998, 3999, 3999, 3999, 3999, 3999, 3999, 3999, 3999,
3999, 3999, 3999, 3999, 3999, 3999, 3999, 3999), TR2x45.1.2 = c(12363,
12363, 12352, 12332, 12333, 12330, 12329, 12329, 12329, 12318,
12298, 12296, 12289, 12286, 12277, 12264, 12264, 12261, 12254,
12252, 12249, 12248, 12246, 12244, 12244, 12240, 12227, 12224,
12220, 12219, 12218, 12215, 12206, 12200, 12197, 12197, 12196,
12196, 12195, 12191, 12187, 12183, 12182, 12179, 12177, 11923,
11922, 11921, 11914, 11913, 11913, 11913, 11913, 11913, 11905,
11904, 11903, 11903, 11903, 11902, 11902, 11901, 11901, 11901,
11901, 11901, 11900, 11900, 11899, 11898, 11891, 11880, 11878,
11875, 11873, 11871, 11870, 11866, 11864, 11861, 11859, 11857,
11856, 11855, 11854, 11853, 11852, 11851, 11849, 11848), UT2x45.5 = c(12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040,
12040, 12040, 12040, 12040, 12040, 12040, 12040, 12040, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462,
10462, 10462, 10462, 10462, 10462, 10462, 10462, 10462), UT2x45.1.2 = c(12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349,
12349, 12349, 12349, 12349, 12349, 12349, 12349, 12349, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580,
9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580, 9580
), UT2x45.6 = c(15564, 15563, 15562, 15562, 15563, 15563, 15563,
15563, 15562, 15562, 15560, 15554, 15544, 15542, 15541, 15540,
15539, 15538, 15538, 15537, 15536, 15535, 15533, 15529, 15528,
15525, 15524, 15521, 15519, 15517, 15516, 15516, 15515, 15515,
15514, 15514, 15513, 15510, 15508, 15505, 15502, 15501, 15500,
15498, 15496, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438, 12438,
12438, 12438), UT2x45.1.3 = c(18966, 18966, 18966, 18966, 18966,
18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966, 18966,
18966, 18966, 18966, 18966, 18966, 18966, 18967, 18968, 18968,
18969, 18970, 18970, 18971, 18971, 18971, 18972, 18973, 18974,
18974, 18975, 18976, 18977, 18977, 18978, 18978, 18978, 18978,
18979, 18980, 18980, 18981, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189, 20189,
20189, 20189, 20189, 20189), TR2x45.6 = c(15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834, 15834,
15834, 15834, 15834, 15835, 15835, 15835, 15835, 15835, 15835,
15835, 15835, 15835, 15835, 15835, 15835, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775, 16775,
16775, 16775, 16775, 16775, 16775, 16775), TR2x45.1.3 = c(19716,
19716, 19716, 19716, 19716, 19718, 19718, 19718, 19720, 19722,
19723, 19723, 19723, 19723, 19723, 19723, 19722, 19722, 19722,
19723, 19723, 19723, 19724, 19724, 19725, 19726, 19728, 19730,
19732, 19733, 19736, 19737, 19737, 19737, 19737, 19738, 19738,
19738, 19738, 19738, 19738, 19738, 19738, 19738, 19738, 27720,
27720, 27720, 27720, 27720, 27720, 27720, 27720, 27720, 27720,
27720, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719,
27719, 27719, 27719, 27719, 27719, 27719, 27719, 27719)), .Names = c("estado",
"tiempo", "UT2x45", "TR2x45", "UT2x45.2", "TR2x45.2", "TR2x45.3",
"UT2x45.3", "TR2x45.1", "UT2x45.1", "UT2x45.4", "TR2x45.4", "UT2x45.1.1",
"TR2x45.1.1", "TR2x45.5", "TR2x45.1.2", "UT2x45.5", "UT2x45.1.2",
"UT2x45.6", "UT2x45.1.3", "TR2x45.6", "TR2x45.1.3"), row.names = c("3087",
"3088", "3089", "3090", "3091", "3092", "3093", "3094", "3095",
"3096", "3097", "3098", "3099", "3100", "3101", "3102", "3103",
"3104", "3105", "3106", "3107", "3108", "3109", "3110", "3111",
"3112", "3113", "3114", "3115", "3116", "3117", "3118", "3119",
"3120", "3121", "3122", "3123", "3124", "3125", "3126", "3127",
"3128", "3129", "3130", "3131", "15687", "15688", "15689", "15690",
"15691", "15692", "15693", "15694", "15695", "15696", "15697",
"15698", "15699", "15700", "15701", "15702", "15703", "15704",
"15705", "15706", "15707", "15708", "15709", "15710", "15711",
"15712", "15713", "15714", "15715", "15716", "15717", "15718",
"15719", "15720", "15721", "15722", "15723", "15724", "15725",
"15726", "15727", "15728", "15729", "15730", "15731"), class = "data.frame")
我希望我已经明确表示我认为必须有一种更简单的方法来做到这一点。谢谢。
答案 0 :(得分:0)
我认为for循环只是对函数ibtener_pendientes
进行子集化和应用。如果是这种情况,您可以使用:
## create list of dfs based on row idx
idx <- list(1:11, 23:33, 45:56, 67:78)
## or write a function to automate the rows
# idx <- lapply(seq(1, nrow(d), by = 10 + 12), function(x) seq(x, x + 11))
l <- lapply(idx, function(x) d[x, ])
do.call('rbind', Map('obtener_pendientes', l))
# tiempo V2 V3 V4 V5 V6
# df_a_medir$tiempo 1 8.2272727 37.9090909 0 95.5909091 16.6818182
# 1 0.9495273 0.7936207 NaN 0.8618870 0.5851504
# df_a_medir$tiempo 1 2.3181818 50.3181818 0 184.8636364 10.6363636
# 1 0.9436334 0.8228418 NaN 0.9712757 0.9353285
# df_a_medir$tiempo 1 0.0000000 4.4220779 0 51.0649351 1.4675325
# 1 NaN 0.8718034 NaN 0.7925802 0.6962431
# df_a_medir$tiempo 1 0.0000000 4.0000000 0 194.3636364 1.9090909
# 1 NaN 0.5621631 NaN 0.9699970 0.7561455
# V7 V8 V9 V10 V11
# df_a_medir$tiempo 8.4090909 182.0000000 0.0000000 0.0000000 77.0454545
# 0.9803865 0.9748662 NaN NaN 0.8338448
# df_a_medir$tiempo 10.4090909 169.5000000 0.0000000 0.0000000 242.1363636
# 0.9862105 0.9684383 NaN NaN 0.9918088
# df_a_medir$tiempo 0.0000000 120.2272727 0.0000000 1.6818182 36.2532468
# NaN 0.9357959 NaN 0.8381040 0.8932908
# df_a_medir$tiempo 0.0000000 165.5000000 5.8636364 2.6363636 171.0909091
# NaN 0.9730404 0.9418502 0.9271605 0.9658712
# V12 V13 V14 V15 V16 V17 V18
# df_a_medir$tiempo 0.7727273 86.1363636 9.8636364 27.8181818 0 0 2.2727273
# 0.5311111 0.9337161 0.8997295 0.8450071 NaN NaN 0.8739165
# df_a_medir$tiempo 0.0000000 77.2727273 4.9545455 20.1363636 0 0 9.0454545
# NaN 0.9727717 0.7449921 0.9362205 NaN NaN 0.9558411
# df_a_medir$tiempo 0.0000000 52.7532468 0.3571429 5.7337662 0 0 0.0000000
# NaN 0.8280456 0.7368421 0.8605300 NaN NaN NaN
# df_a_medir$tiempo 0.0000000 68.8181818 0.6363636 18.5454545 0 0 0.0000000
# NaN 0.9035839 0.6666667 0.9262417 NaN NaN NaN
# V19 V20 V21
# df_a_medir$tiempo 0.0000000 0 3.5000000
# NaN NaN 0.8115468
# df_a_medir$tiempo 2.8636364 0 7.6363636
# 0.9541063 NaN 0.9702682
# df_a_medir$tiempo 0.0000000 0 0.3571429
# NaN NaN 0.7368421
# df_a_medir$tiempo 0.0000000 0 0.0000000
# NaN NaN NaN
看起来类似于我从循环函数得到的输出
辅助函数:
(没有足够的空间在这里添加数据d
)
regresion <- function(formula){
regresion = lm( formula )
ss = summary(regresion)
return(c( coef(regresion)[2] , ss$adj.r.squared))
}
sacar_columnas <- function (dataframe,columnas){
return(dataframe[,!(names(dataframe) %in% columnas)])
}
desplazar<- function(tiempo,dataframe){
matriz = as.matrix(dataframe)
matriz_dif = abs(diff(matriz )) # derivar
matriz_dif_inv = diffinv(matriz_dif) # integrar
return(data.frame(cbind(tiempo, matriz_dif_inv )))
}
obtener_pendientes <- function(df_a_medir){
df_a_medir2 =sacar_columnas(df_a_medir, c("estado","tiempo"))
mat_desplazamiento = desplazar(df_a_medir$tiempo,df_a_medir2)
pend_muestreo = apply(mat_desplazamiento,2,function(x) regresion(x~df_a_medir$tiempo))
return(pend_muestreo)
}