Using "by" with helper function error in r
I am trying to apply a helper function to a dataframe. My function works (I know because I've used it a lot already, and also works with a full dataframe) and spelling correctness. However, when I want to apply, I get an error:
pru<-by( data = cs_us[,3:length(cs_us)],
+ INDICES = cs_us$ensayo,
+ FUN =desplazar(cs_us$tiempo,cs_us[,3:length(cs_us)-1])
+ )
Error in FUN(X[[1L]], ...) : could not find function "FUN"
If I try to find it, it is in the global environment
getAnywhere(desplazar)
A single object matching โdesplazarโ was found
It was found in the following places
.GlobalEnv
with value
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 )))
}
Here is some of my data (just a critical part for you to have 2 levels of factors)
dput(cs_us[120:140,])
structure(list(estado = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L), tiempo = c(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, 3120.2, 3120.4, 3120.6,
3120.8, 3121), UT2x45.1 = c(13666L, 13665L, 13664L, 13662L, 13661L,
13655L, 13651L, 13648L, 13647L, 13643L, 13642L, 13640L, 13638L,
13637L, 13637L, 13636L, 11115L, 11115L, 11115L, 11115L, 11115L
), UT2x45.2 = c(15357L, 15357L, 15357L, 15357L, 15357L, 15357L,
15357L, 15357L, 15357L, 15357L, 15357L, 15357L, 15357L, 15357L,
15357L, 15357L, 15087L, 15087L, 15087L, 15087L, 15087L), TR2x45.1 = c(15761L,
15755L, 15742L, 15729L, 15727L, 15726L, 15724L, 15722L, 15720L,
15716L, 15717L, 15715L, 15711L, 15708L, 15706L, 15704L, 15628L,
15628L, 15628L, 15628L, 15628L), TR2x45.2 = c(15381L, 15363L,
15342L, 15324L, 15320L, 15314L, 15308L, 15298L, 15296L, 15294L,
15293L, 15293L, 15293L, 15294L, 15294L, 15294L, 14238L, 14238L,
14238L, 14238L, 14238L), UT2x45.3 = c(15649L, 15649L, 15649L,
15649L, 15649L, 15649L, 15649L, 15649L, 15649L, 15649L, 15649L,
15649L, 15649L, 15649L, 15649L, 15649L, 15532L, 15532L, 15532L,
15532L, 15532L), UT2x45.4 = c(16038L, 16038L, 16038L, 16038L,
16038L, 16038L, 16038L, 16038L, 16038L, 16038L, 16038L, 16038L,
16038L, 16038L, 16038L, 16038L, 16136L, 16136L, 16136L, 16136L,
16136L), TR2x45.3 = c(15662L, 15658L, 15658L, 15657L, 15657L,
15653L, 15649L, 15648L, 15647L, 15646L, 15645L, 15644L, 15644L,
15637L, 15633L, 15632L, 16123L, 16123L, 16123L, 16123L, 16123L
), TR2x45.4 = c(15780L, 15761L, 15743L, 15732L, 15729L, 15726L,
15720L, 15715L, 15713L, 15712L, 15709L, 15705L, 15704L, 15703L,
15702L, 15702L, 15233L, 15233L, 15232L, 15233L, 15233L), TR2x45.5 = c(16072L,
16075L, 16077L, 16078L, 16078L, 16080L, 16082L, 16083L, 16084L,
16086L, 16087L, 16088L, 16088L, 16089L, 16091L, 16093L, 16198L,
16198L, 16198L, 16198L, 16198L), TR2x45.6 = c(16100L, 16087L,
16072L, 16062L, 16055L, 16049L, 16044L, 16040L, 16035L, 16030L,
16024L, 16025L, 16026L, 16024L, 16022L, 16022L, 16306L, 16306L,
16306L, 16306L, 16306L), UT2x45.5 = c(15642L, 15642L, 15642L,
15642L, 15642L, 15642L, 15642L, 15642L, 15642L, 15642L, 15642L,
15642L, 15642L, 15642L, 15642L, 15642L, 16140L, 16140L, 16140L,
16140L, 16140L), UT2x45.6 = c(16546L, 16546L, 16546L, 16546L,
16546L, 16546L, 16546L, 16546L, 16546L, 16546L, 16546L, 16546L,
16546L, 16546L, 16546L, 16546L, 16428L, 16428L, 16428L, 16428L,
16428L), TR2x45.7 = c(19030L, 19035L, 19033L, 19029L, 19028L,
19027L, 19026L, 19025L, 19023L, 19022L, 19022L, 19022L, 19022L,
19022L, 19022L, 19022L, 20119L, 20119L, 20118L, 20119L, 20120L
), TR2x45.8 = c(20874L, 20883L, 20886L, 20887L, 20889L, 20891L,
20896L, 20898L, 20899L, 20901L, 20903L, 20907L, 20912L, 20916L,
20920L, 20923L, 29200L, 29200L, 29200L, 29200L, 29200L), UT2x45.7 = c(15731L,
15730L, 15729L, 15729L, 15726L, 15724L, 15722L, 15722L, 15722L,
15722L, 15722L, 15722L, 15722L, 15722L, 15722L, 15722L, 15567L,
15567L, 15567L, 15567L, 15567L), UT2x45.8 = c(14584L, 14584L,
14584L, 14584L, 14584L, 14584L, 14584L, 14584L, 14584L, 14584L,
14584L, 14584L, 14584L, 14584L, 14584L, 14584L, 14080L, 14080L,
14080L, 14080L, 14082L), UT2x45.9 = c(15900L, 15900L, 15901L,
15902L, 15902L, 15908L, 15911L, 15911L, 15912L, 15913L, 15913L,
15913L, 15913L, 15913L, 15913L, 15913L, 15941L, 15941L, 15941L,
15941L, 15941L), UT2x45.10 = c(14906L, 14906L, 14906L, 14906L,
14906L, 14906L, 14906L, 14906L, 14906L, 14906L, 14906L, 14906L,
14906L, 14906L, 14906L, 14906L, 13890L, 13890L, 13890L, 13890L,
13890L), TR2x45.9 = c(16072L, 16072L, 16072L, 16072L, 16072L,
16072L, 16072L, 16072L, 16072L, 16071L, 16071L, 16071L, 16071L,
16071L, 16071L, 16071L, 16240L, 16240L, 16240L, 16240L, 16240L
), TR2x45.10 = c(13995L, 13981L, 13948L, 13929L, 13916L, 13906L,
13900L, 13893L, 13881L, 13870L, 13863L, 13854L, 13846L, 13839L,
13834L, 13826L, 3363L, 3363L, 3363L, 3363L, 3363L), UT2x45.11 = c(16673L,
16673L, 16673L, 16673L, 16673L, 16673L, 16673L, 16673L, 16673L,
16673L, 16673L, 16673L, 16673L, 16673L, 16673L, 16673L, 17391L,
17391L, 17391L, 17391L, 17391L), UT2x45.12 = c(16197L, 16194L,
16194L, 16194L, 16192L, 16191L, 16191L, 16190L, 16188L, 16185L,
16185L, 16184L, 16184L, 16184L, 16183L, 16182L, 16493L, 16493L,
16493L, 16493L, 16493L), TR2x45.11 = c(15590L, 15588L, 15587L,
15587L, 15585L, 15585L, 15585L, 15585L, 15584L, 15584L, 15584L,
15584L, 15590L, 15590L, 15590L, 15589L, 15464L, 15464L, 15464L,
15464L, 15464L), TR2x45.12 = c(13279L, 13255L, 13221L, 13190L,
13182L, 13177L, 13174L, 13166L, 13159L, 13157L, 13154L, 13147L,
13141L, 13138L, 13138L, 13135L, 8705L, 8705L, 8705L, 8705L, 8705L
), TR2x45.13 = c(14342L, 14342L, 14342L, 14342L, 14342L, 14342L,
14342L, 14342L, 14342L, 14342L, 14342L, 14342L, 14343L, 14342L,
14341L, 14341L, 13276L, 13276L, 13276L, 13276L, 13276L), TR2x45.14 = c(17163L,
17163L, 17163L, 17163L, 17163L, 17163L, 17163L, 17163L, 17163L,
17163L, 17163L, 17163L, 17163L, 17163L, 17163L, 17163L, 17888L,
17888L, 17888L, 17888L, 17888L), UT2x45.13 = c(15769L, 15769L,
15769L, 15769L, 15769L, 15769L, 15769L, 15769L, 15769L, 15769L,
15769L, 15769L, 15769L, 15769L, 15769L, 15769L, 15702L, 15702L,
15702L, 15702L, 15702L), UT2x45.14 = c(18606L, 18606L, 18606L,
18606L, 18606L, 18606L, 18606L, 18606L, 18606L, 18606L, 18606L,
18606L, 18606L, 18606L, 18606L, 18606L, 22677L, 22677L, 22677L,
22677L, 22677L), TR2x45.15 = c(15986L, 15989L, 15990L, 15990L,
15990L, 15990L, 15990L, 15990L, 15989L, 15988L, 15988L, 15988L,
15988L, 15988L, 15988L, 15988L, 15998L, 15998L, 15998L, 15998L,
15998L), TR2x45.16 = c(16328L, 16347L, 16362L, 16367L, 16366L,
16365L, 16366L, 16366L, 16366L, 16366L, 16366L, 16366L, 16366L,
16366L, 16366L, 16366L, 17946L, 17946L, 17946L, 17946L, 17946L
), UT2x45.15 = c(15768L, 15768L, 15768L, 15768L, 15768L, 15768L,
15768L, 15768L, 15768L, 15768L, 15768L, 15768L, 15768L, 15768L,
15768L, 15768L, 16168L, 16168L, 16168L, 16168L, 16168L), UT2x45.16 = c(16040L,
16040L, 16040L, 16040L, 16040L, 16040L, 16040L, 16040L, 16040L,
16040L, 16040L, 16040L, 16040L, 16040L, 16040L, 16040L, 16037L,
16037L, 16037L, 16037L, 16037L), UT2x45.17 = c(15749L, 15749L,
15749L, 15749L, 15749L, 15749L, 15749L, 15749L, 15749L, 15749L,
15749L, 15749L, 15749L, 15749L, 15749L, 15749L, 15677L, 15677L,
15677L, 15677L, 15677L), UT2x45.18 = c(18179L, 18179L, 18179L,
18179L, 18179L, 18179L, 18179L, 18179L, 18179L, 18179L, 18179L,
18179L, 18179L, 18179L, 18179L, 18179L, 16152L, 16152L, 16152L,
16152L, 16152L), TR2x45.17 = c(16423L, 16426L, 16427L, 16428L,
16428L, 16428L, 16428L, 16428L, 16428L, 16428L, 16428L, 16428L,
16428L, 16428L, 16428L, 16428L, 17110L, 17110L, 17110L, 17110L,
17110L), TR2x45.18 = c(13624L, 13613L, 13586L, 13573L, 13566L,
13558L, 13554L, 13548L, 13546L, 13543L, 13540L, 13538L, 13533L,
13531L, 13527L, 13526L, 12369L, 12369L, 12369L, 12369L, 12369L
), UT2x45.19 = c(8263L, 8259L, 8257L, 8255L, 8254L, 8253L, 8252L,
8250L, 8246L, 8239L, 8235L, 8235L, 8234L, 8232L, 8229L, 8226L,
1579L, 1579L, 1579L, 1579L, 1579L), UT2x45.20 = c(17469L, 17469L,
17470L, 17471L, 17473L, 17475L, 17475L, 17475L, 17475L, 17476L,
17477L, 17479L, 17481L, 17482L, 17482L, 17483L, 19822L, 19822L,
19823L, 19823L, 19824L), TR2x45.19 = c(15723L, 15709L, 15671L,
15634L, 15617L, 15598L, 15582L, 15568L, 15557L, 15547L, 15537L,
15526L, 15517L, 15506L, 15501L, 15496L, 14338L, 14338L, 14338L,
14338L, 14338L), TR2x45.20 = c(15818L, 15822L, 15825L, 15829L,
15833L, 15837L, 15838L, 15839L, 15840L, 15841L, 15843L, 15845L,
15847L, 15849L, 15851L, 15854L, 14945L, 14945L, 14945L, 14945L,
14945L), ensayo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("E1",
"E2"), class = "factor")), .Names = c("estado", "tiempo", "UT2x45.1",
"UT2x45.2", "TR2x45.1", "TR2x45.2", "UT2x45.3", "UT2x45.4", "TR2x45.3",
"TR2x45.4", "TR2x45.5", "TR2x45.6", "UT2x45.5", "UT2x45.6", "TR2x45.7",
"TR2x45.8", "UT2x45.7", "UT2x45.8", "UT2x45.9", "UT2x45.10",
"TR2x45.9", "TR2x45.10", "UT2x45.11", "UT2x45.12", "TR2x45.11",
"TR2x45.12", "TR2x45.13", "TR2x45.14", "UT2x45.13", "UT2x45.14",
"TR2x45.15", "TR2x45.16", "UT2x45.15", "UT2x45.16", "UT2x45.17",
"UT2x45.18", "TR2x45.17", "TR2x45.18", "UT2x45.19", "UT2x45.20",
"TR2x45.19", "TR2x45.20", "ensayo"), row.names = c("3117", "3118",
"3119", "3120", "3121", "3122", "3123", "3124", "3125", "3126",
"3127", "3128", "3129", "3130", "3131", "3132", "15598", "15599",
"15600", "15601", "15602"), class = "data.frame")
+3
source to share
1 answer
You may try
by(cs_us[,(3:length(cs_us))-1], cs_us$ensayo,
FUN= function(x) desplazar(x$tiempo, x[,-1] ) )
or
Map(desplazar, split(cs_us$tiempo, cs_us$ensayo),
split(cs_us[3:(ncol(cs_us)-1)], cs_us$ensayo))
You can also modify desplazar
and remove the anonymous function call inby
desplazar1 <- function(data){
matriz <- as.matrix(data[-1])
matriz_dif <- abs(diff(matriz))
matriz_dif_inv <- diffinv(matriz_dif)
data.frame(tiempo=data['tiempo'], matriz_dif_inv)
}
by(cs_us[,(3:length(cs_us))-1], cs_us$ensayo, FUN=desplazar1)
0
source to share