[R-es] Sumar positivos y negativos por separado

Manuel Mendoza mmendoz@ @end|ng |rom |u|br|ghtm@||@org
Lun Ene 2 13:45:26 CET 2023


Gracias Carlos, y aprovecho para felicitaros también el año nuevo..
Manuel

El lun, 2 ene 2023 a las 13:03, Carlos Ortega (<cof using qualityexcellence.es>)
escribió:

> Hola,
>
> Feliz Año!.
> Por no dejar pasar la oportunidad de usar algo más vectorizado... y
> rapidito...
> Incluyo tiempos comprativos.
>
> #-----------------
> > library(mlbench)
> > library(data.table)
> > library(dplyr)
> > library(tictoc)
> >
> > data(Ionosphere)
> > head(Ionosphere)
>   V1 V2      V3       V4       V5       V6       V7       V8      V9
>  V10     V11      V12     V13      V14
> 1  1  0 0.99539 -0.05889  0.85243  0.02306  0.83398 -0.37708 1.00000
>  0.03760 0.85243 -0.17755 0.59755 -0.44945
> 2  1  0 1.00000 -0.18829  0.93035 -0.36156 -0.10868 -0.93597 1.00000
> -0.04549 0.50874 -0.67743 0.34432 -0.69707
> 3  1  0 1.00000 -0.03365  1.00000  0.00485  1.00000 -0.12062 0.88965
>  0.01198 0.73082  0.05346 0.85443  0.00827
> 4  1  0 1.00000 -0.45161  1.00000  1.00000  0.71216 -1.00000 0.00000
>  0.00000 0.00000  0.00000 0.00000  0.00000
> 5  1  0 1.00000 -0.02401  0.94140  0.06531  0.92106 -0.23255 0.77152
> -0.16399 0.52798 -0.20275 0.56409 -0.00712
> 6  1  0 0.02337 -0.00592 -0.09924 -0.11949 -0.00763 -0.11824 0.14706
>  0.06637 0.03786 -0.06302 0.00000  0.00000
>        V15      V16      V17      V18      V19      V20      V21      V22
>      V23      V24      V25      V26
> 1  0.60536 -0.38223  0.84356 -0.38542  0.58212 -0.32192  0.56971 -0.29674
>  0.36946 -0.47357  0.56811 -0.51171
> 2 -0.51685 -0.97515  0.05499 -0.62237  0.33109 -1.00000 -0.13151 -0.45300
> -0.18056 -0.35734 -0.20332 -0.26569
> 3  0.54591  0.00299  0.83775 -0.13644  0.75535 -0.08540  0.70887 -0.27502
>  0.43385 -0.12062  0.57528 -0.40220
> 4 -1.00000  0.14516  0.54094 -0.39330 -1.00000 -0.54467 -0.69975  1.00000
>  0.00000  0.00000  1.00000  0.90695
> 5  0.34395 -0.27457  0.52940 -0.21780  0.45107 -0.17813  0.05982 -0.35575
>  0.02309 -0.52879  0.03286 -0.65158
> 6 -0.04572 -0.15540 -0.00343 -0.10196 -0.11575 -0.05414  0.01838  0.03669
>  0.01519  0.00888  0.03513 -0.01535
>        V27      V28      V29      V30      V31      V32      V33      V34
> Class
> 1  0.41078 -0.46168  0.21266 -0.34090  0.42267 -0.54487  0.18641 -0.45300
>  good
> 2 -0.20468 -0.18401 -0.19040 -0.11593 -0.16626 -0.06288 -0.13738 -0.02447
>   bad
> 3  0.58984 -0.22145  0.43100 -0.17365  0.60436 -0.24180  0.56045 -0.38238
>  good
> 4  0.51613  1.00000  1.00000 -0.20099  0.25682  1.00000 -0.32382  1.00000
>   bad
> 5  0.13290 -0.53206  0.02431 -0.62197 -0.05707 -0.59573 -0.04608 -0.65697
>  good
> 6 -0.03240  0.09223 -0.07859  0.00732  0.00000  0.00000 -0.00039  0.12011
>   bad
> >
> > data <- Ionosphere %>% select(where(is.numeric)) %>% as.data.frame()
> >
> *> #------ Vectorizado*
> > tic()
> > myfun <- function(x) {
> +
> +   pos <- sum(x[x > 0])
> +   neg <- sum(x[x < 0])
> +
> +   return(list(pos = pos, neg = neg))
> + }
> >
> > resout <- apply(data, 1, myfun)
> > resend <- rbindlist(resout)
> > toc()
> *0.012 sec elapsed*
> > head(resend)
>         pos      neg
>       <num>    <num>
> 1:  9.96328 -5.23501
> 2:  4.16949 -8.80629
> 3: 11.59911 -2.19323
> 4: 12.07816 -5.61414
> 5:  6.38876 -5.34692
> 6:  0.60859 -1.01667
> >
> >
> *> #-------- Bucle*
> > tic()
> > pos <-c(rep(0, nrow(data)))
> > neg <- c(rep(0, nrow(data)))
> >
> > for (i in 1:nrow(data)) {
> +   x<-data[i,]
> +   pos[i]<-sum(x[x>0])
> +   neg[i]<-sum(x[x<0])
> + }
> > toc()
> *0.609 sec elapsed*
> > head(pos)
> [1]  9.96328  4.16949 11.59911 12.07816  6.38876  0.60859
> > head(neg)
> [1] -5.23501 -8.80629 -2.19323 -5.61414 -5.34692 -1.01667
>
>
> #-----------------
>
>
>
> Gracias,
> Carlos Ortega
> www.qualityexcellence.es
>
>
>
> El lun, 2 ene 2023 a las 11:58, Manuel Mendoza (<
> mmendoza using fulbrightmail.org>) escribió:
>
>> Bueno, funcionó con:
>>
>> pos<-c(0)
>> for (i in 1:nrow(data)) {
>>     x<-data[i,]
>>     pos[i]<-sum(x[x>0])
>> }
>>
>> y x<0 para los negativos
>>
>> Gracias nuevamente,
>> Manuel
>>
>>
>> El lun, 2 ene 2023 a las 10:23, Proyecto R-UCA (<r-uca using uca.es>) escribió:
>>
>> > Buenas,
>> >
>> > Sin usar dplyr,
>> >
>> > Se puede hacer un bucle en las columnas y para cada columna
>> >
>> > sum(x[x>0])
>> >
>> > sum(x[x < 0])
>> >
>> > Un saludo.
>> >
>> > El lun, 02-01-2023 a las 09:02 +0100, Emilio L. Cano escribió:
>> > > Hola,
>> > >
>> > > Este sería un ejemplo reproducible rápido. A mí para esto me gusta
>> > rowise() de {dplyr}. En c_across() se pueden seleccionar las columnas a
>> > > conveniencia. Seguramente haya una forma de evitar crear las
>> funciones.
>> > Si la suma la quieres en valor absoluto multiplicas por -1 en
>> > > suma_neg y listo.
>> > >
>> > > Un saludo,
>> > > Emilio
>> > >
>> > >
>> > > > set.seed(2023)
>> > > > d <- data.frame(a = round(rnorm(10), 1),
>> > > +                 b = round(rnorm(10), 1),
>> > > +                 c = round(rnorm(10), 1))
>> > > > d
>> > >       a    b    c
>> > > 1  -0.1  0.3 -0.4
>> > > 2  -1.0 -0.4 -0.3
>> > > 3  -1.9  0.6  1.2
>> > > 4  -0.2  0.7  0.2
>> > > 5  -0.6 -0.6 -0.4
>> > > 6   1.1  0.7 -1.8
>> > > 7  -0.9  0.6 -0.6
>> > > 8   1.0  0.5 -0.9
>> > > 9  -0.4  0.9  1.5
>> > > 10 -0.5  0.6  2.7
>> > > >
>> > > > library(dplyr)
>> > > >
>> > > > suma_pos <- function(x){
>> > > +   sum((x>0)*x)
>> > > + }
>> > > > suma_neg <- function(x){
>> > > +   sum((x<0)*x)
>> > > + }
>> > > >
>> > > > d |>
>> > > +   rowwise() |>
>> > > +   mutate(positivos = suma_pos(c_across()),
>> > > +          negativos = suma_neg(c_across()))
>> > > # A tibble: 10 × 5
>> > > # Rowwise:
>> > >        a     b     c positivos negativos
>> > >    <dbl> <dbl> <dbl>     <dbl>     <dbl>
>> > >  1  -0.1   0.3  -0.4       0.3      -0.5
>> > >  2  -1    -0.4  -0.3       0        -1.7
>> > >  3  -1.9   0.6   1.2       1.8      -1.9
>> > >  4  -0.2   0.7   0.2       0.9      -0.2
>> > >  5  -0.6  -0.6  -0.4       0        -1.6
>> > >  6   1.1   0.7  -1.8       1.8      -1.8
>> > >  7  -0.9   0.6  -0.6       0.6      -1.5
>> > >  8   1     0.5  -0.9       1.5      -0.9
>> > >  9  -0.4   0.9   1.5       2.4      -0.4
>> > > 10  -0.5   0.6   2.7       3.3      -0.5
>> > >
>> > >
>> > >
>> > > > El 2 ene 2023, a las 6:31, Manuel Mendoza <
>> mmendoza using fulbrightmail.org>
>> > escribió:
>> > > >
>> > > > Buenos días, de un conjunto de variables, quiero obtener la suma de
>> los
>> > > > valores positivos de cada fila, por una parte, y la de los negativos
>> > por
>> > > > otra. Qué variables toman valores positivos y negativos varía de una
>> > fila a
>> > > > otra, claro.
>> > > > Gracias por vuestra ayuda,
>> > > > Manuel
>> > > >
>> > > >         [[alternative HTML version deleted]]
>> > > >
>> > > > _______________________________________________
>> > > > R-help-es mailing list
>> > > > R-help-es using r-project.org
>> > > >
>> >
>> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help-es__;!!D9dNQwwGXtA!XPbjkJgKEEP5dmap64bNfzfAmIe4ArK_nN4H9-QIBPAihE4aO-RS6q6YAaM3IT-sP9qA0PqeGoqQF7gIlec$
>> > > >
>> > >
>> > >
>> > >         [[alternative HTML version deleted]]
>> > >
>> > > _______________________________________________
>> > > R-help-es mailing list
>> > > R-help-es using r-project.org
>> > >
>> >
>> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help-es__;!!D9dNQwwGXtA!XPbjkJgKEEP5dmap64bNfzfAmIe4ArK_nN4H9-QIBPAihE4aO-RS6q6YAaM3IT-sP9qA0PqeGoqQF7gIlec$
>> > >
>> > _______________________________________________
>> > R-help-es mailing list
>> > R-help-es using r-project.org
>> > https://stat.ethz.ch/mailman/listinfo/r-help-es
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-help-es mailing list
>> R-help-es using r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-help-es
>>
>
>
> --
> Saludos,
> Carlos Ortega
> www.qualityexcellence.es
>

	[[alternative HTML version deleted]]



Más información sobre la lista de distribución R-help-es