Re: [R-es] aplicar codigo

2020-09-10 Thread David Mateos
Que bueno!
el famoso stringsAsFactors.
Muchas gracias Marcelino, un saludo


Enviado desde Outlook


De: Marcelino de la Cruz Rot 
Enviado: jueves, 10 de septiembre de 2020 21:20
Para: David Mateos 
Cc: r-help-es@r-project.org 
Asunto: Re: [R-es] aplicar codigo

Ya s� a qu� se debe:
En vuestro R, la funci�n data.frame transforma autom�ticamente los
vectores de tipo /character/ a tipo/factor/.
Con un factor, sapply devuelve el n�mero del nivel correspondiente:

 > col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
'x1','x2', 'x4')

 > sapply(col1, function(x) ifelse(x%in%c("x1","x2","x3"),
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
 x1 x2x11 x1x33 x1
x4 x5x35 x1 x2 x4
"prueba12" "prueba12"  "x11" "prueba12"  "x33" "prueba12"
"prueba2"  "prueba2"  "x35" "prueba12" "prueba12"  "prueba2"

 > sapply(factor(col1), function(x) ifelse(x%in%c("x1","x2","x3"),
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
  [1] "prueba12" "prueba12" "2""prueba12" "4" "prueba12"
"prueba2"  "prueba2"  "5""prueba12" "prueba12" "prueba2"
 >




El 10/09/2020 a las 20:35, David Mateos escribi�:
> Hola,
> a m� me sucede como a Samura y no consigo entender de donde salen esos
> "2", "4" y "5" para df1. �Alguien puede arrojar luz?
> Tanto con la versi�n 3.6 como con la 4.0.2
>
> Si me funciona con
> ```
> transforma <- function(df) {
>   apply(df, 1, function(x)
> ifelse(x%in%c("x1","x2","x3"),
>"prueba12",
>ifelse(x%in%c("x4","x5","x6"),
>   "prueba2",x)
>)
> )
> }
> df1$transformacion <- transforma(df1)
> ```
> Un saludo,
> David
>
> Enviado desde Outlook 
>
> 
> *De:* R-help-es  en nombre de
> Marcelino de la Cruz Rot 
> *Enviado:* jueves, 10 de septiembre de 2020 19:15
> *Para:* Samura . ; r-help-es@r-project.org
> 
> *Asunto:* Re: [R-es] aplicar codigo
> Yo copio y pego este c�digo y me sale correctamente. Se me ocurre que
> pueda deberse a la versi�n de R �cu�l usas?
>
>
> El 10/09/2020 a las 17:51, Samura . escribi�:
> > Gracias por las respuestas.
> >
> > Prob� lo de hacer la funci�n y no me sal�a. Pensaba que hac�a algo mal.
> > Ahora con el c�digo de Marcelino tampoco me sale.
> >
> > col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> > 'x1','x2', 'x4')
> > col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> > col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> > 'x35','x2','x2', 'x4','x6', 'x5')
> >
> > df1<-data.frame(col1)
> > df2<-data.frame(col2)
> > df3<-data.frame(col3)
> >
> > transforma <- function(df) sapply(df, function(x)
> > ifelse(x%in%c("x1","x2","x3"),
> >  "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
> >
> > transforma(df1)
> > col1
> > [1,] "prueba12"
> > [2,] "prueba12"
> > [3,] "2"
> > [4,] "prueba12"
> > [5,] "4"
> > [6,] "prueba12"
> > [7,] "prueba2"
> > [8,] "prueba2"
> > [9,] "5"
> > [10,] "prueba12"
> > [11,] "prueba12"
> > [12,] "prueba2"
> >
> > �Alguna idea de pq me sale 2, 4, 5?
> > 
> > *De:* R-help-es  en nombre de
> > Marcelino de la Cruz Rot 
> > *Enviado:* jueves, 10 de septiembre de 2020 9:36
> > *Para:* r-help-es@r-project.org 
> > *Asunto:* Re: [R-es] aplicar codigo
> > Hola:
> > Como dice Carlos, algo as�, por ejemplo:
> >
> > transforma <- function(df) sapply(df, function(x)
> > ifelse(x%in%c("x1","x2","x3"),
> > "prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
> >
> >
> >  > transforma(df1)
> >col1
> >   [1,] "prueba1"
> >   [2,] "prueba1"
> >   [3,] "x11"
> >   [4,] "prueba1"
> >   [5,] "x33"
> >   [6,] "prueba1"
> >   [7,] "prueba2"
> >   [8,] "prueba2"
> >   [9,] "x35"
> > [10,] "prueba1"
> > [11,] "prueba1"
> > [12,] "prueba2"
> >
> >
> >  > transforma(df2)
> >   col2
> > [1,] "x12"
> > [2,] "prueba2"
> > [3,] "prueba2"
> > [4,] "x771"
> > [5,] "prueba2"
> > [6,] "prueba1"
> >
> >
> >  > transforma(df3)
> >col3
> >   [1,] "x7"
> >   [2,] "prueba1"
> >   [3,] "prueba2"
> >   [4,] "prueba2"
> >   [5,] "x111"
> >   [6,] "prueba1"
> >   [7,] "prueba2"
> >   [8,] "prueba2"
> >   [9,] "x35"
> > [10,] "prueba1"
> > [11,] "prueba1"
> > [12,] "prueba2"
> > [13,] "prueba2"
> > [14,] "prueba2"
> >
> > Saludos,
> >
> > Marcelino
> >
> > El 10/09/2020 a las 9:41, Carlos Ortega escribi�:
> > > Hola,
> > > Si lo estoy entendiendo bien, lo que tienes que crearte es una
> funci�n.
> > > A la funci�n le pasar�as unos par�metros (las columnas y el
> dataframe al
> > > que a�adir la transformaci�n), y en el cuerpo de la funci�n haces esos
> > > c�lculos.
> > >
> > > Gracias,
> > > Carlos.
> > >
> > >
> > >
> > > Gracias,
> > > Carlos.
> > >
> > > El jue., 10 sept. 2020 a las 3:55, Samura . ()
> > > escribi�:
> > >
> > >> Hola,

Re: [R-es] aplicar codigo

2020-09-10 Thread Samura .
Solucionado entonces,
muchas gracias

De: R-help-es  en nombre de Marcelino de la 
Cruz Rot 
Enviado: jueves, 10 de septiembre de 2020 19:20
Para: David Mateos 
Cc: r-help-es@r-project.org 
Asunto: Re: [R-es] aplicar codigo

Ya s� a qu� se debe:
En vuestro R, la funci�n data.frame transforma autom�ticamente los
vectores de tipo /character/ a tipo/factor/.
Con un factor, sapply devuelve el n�mero del nivel correspondiente:

 > col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
'x1','x2', 'x4')

 > sapply(col1, function(x) ifelse(x%in%c("x1","x2","x3"),
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
 x1 x2x11 x1x33 x1
x4 x5x35 x1 x2 x4
"prueba12" "prueba12"  "x11" "prueba12"  "x33" "prueba12"
"prueba2"  "prueba2"  "x35" "prueba12" "prueba12"  "prueba2"

 > sapply(factor(col1), function(x) ifelse(x%in%c("x1","x2","x3"),
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
  [1] "prueba12" "prueba12" "2""prueba12" "4" "prueba12"
"prueba2"  "prueba2"  "5""prueba12" "prueba12" "prueba2"
 >




El 10/09/2020 a las 20:35, David Mateos escribi�:
> Hola,
> a m� me sucede como a Samura y no consigo entender de donde salen esos
> "2", "4" y "5" para df1. �Alguien puede arrojar luz?
> Tanto con la versi�n 3.6 como con la 4.0.2
>
> Si me funciona con
> ```
> transforma <- function(df) {
>   apply(df, 1, function(x)
> ifelse(x%in%c("x1","x2","x3"),
>"prueba12",
>ifelse(x%in%c("x4","x5","x6"),
>   "prueba2",x)
>)
> )
> }
> df1$transformacion <- transforma(df1)
> ```
> Un saludo,
> David
>
> Enviado desde Outlook 
>
> 
> *De:* R-help-es  en nombre de
> Marcelino de la Cruz Rot 
> *Enviado:* jueves, 10 de septiembre de 2020 19:15
> *Para:* Samura . ; r-help-es@r-project.org
> 
> *Asunto:* Re: [R-es] aplicar codigo
> Yo copio y pego este c�digo y me sale correctamente. Se me ocurre que
> pueda deberse a la versi�n de R �cu�l usas?
>
>
> El 10/09/2020 a las 17:51, Samura . escribi�:
> > Gracias por las respuestas.
> >
> > Prob� lo de hacer la funci�n y no me sal�a. Pensaba que hac�a algo mal.
> > Ahora con el c�digo de Marcelino tampoco me sale.
> >
> > col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> > 'x1','x2', 'x4')
> > col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> > col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> > 'x35','x2','x2', 'x4','x6', 'x5')
> >
> > df1<-data.frame(col1)
> > df2<-data.frame(col2)
> > df3<-data.frame(col3)
> >
> > transforma <- function(df) sapply(df, function(x)
> > ifelse(x%in%c("x1","x2","x3"),
> >  "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
> >
> > transforma(df1)
> > col1
> > [1,] "prueba12"
> > [2,] "prueba12"
> > [3,] "2"
> > [4,] "prueba12"
> > [5,] "4"
> > [6,] "prueba12"
> > [7,] "prueba2"
> > [8,] "prueba2"
> > [9,] "5"
> > [10,] "prueba12"
> > [11,] "prueba12"
> > [12,] "prueba2"
> >
> > �Alguna idea de pq me sale 2, 4, 5?
> > 
> > *De:* R-help-es  en nombre de
> > Marcelino de la Cruz Rot 
> > *Enviado:* jueves, 10 de septiembre de 2020 9:36
> > *Para:* r-help-es@r-project.org 
> > *Asunto:* Re: [R-es] aplicar codigo
> > Hola:
> > Como dice Carlos, algo as�, por ejemplo:
> >
> > transforma <- function(df) sapply(df, function(x)
> > ifelse(x%in%c("x1","x2","x3"),
> > "prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
> >
> >
> >  > transforma(df1)
> >col1
> >   [1,] "prueba1"
> >   [2,] "prueba1"
> >   [3,] "x11"
> >   [4,] "prueba1"
> >   [5,] "x33"
> >   [6,] "prueba1"
> >   [7,] "prueba2"
> >   [8,] "prueba2"
> >   [9,] "x35"
> > [10,] "prueba1"
> > [11,] "prueba1"
> > [12,] "prueba2"
> >
> >
> >  > transforma(df2)
> >   col2
> > [1,] "x12"
> > [2,] "prueba2"
> > [3,] "prueba2"
> > [4,] "x771"
> > [5,] "prueba2"
> > [6,] "prueba1"
> >
> >
> >  > transforma(df3)
> >col3
> >   [1,] "x7"
> >   [2,] "prueba1"
> >   [3,] "prueba2"
> >   [4,] "prueba2"
> >   [5,] "x111"
> >   [6,] "prueba1"
> >   [7,] "prueba2"
> >   [8,] "prueba2"
> >   [9,] "x35"
> > [10,] "prueba1"
> > [11,] "prueba1"
> > [12,] "prueba2"
> > [13,] "prueba2"
> > [14,] "prueba2"
> >
> > Saludos,
> >
> > Marcelino
> >
> > El 10/09/2020 a las 9:41, Carlos Ortega escribi�:
> > > Hola,
> > > Si lo estoy entendiendo bien, lo que tienes que crearte es una
> funci�n.
> > > A la funci�n le pasar�as unos par�metros (las columnas y el
> dataframe al
> > > que a�adir la transformaci�n), y en el cuerpo de la funci�n haces esos
> > > c�lculos.
> > >
> > > Gracias,
> > > Carlos.
> > >
> > >
> > >
> > > Gracias,
> > > Carlos.
> > >
> > > El jue., 10 sept. 2020 a las 3:55, Samura . ()
> > > escribi�:
> > >
> > >> Hola,
> > >> me gustar�a hacer algo como en el siguiente ejemplo
> 

[R-es] Cálculo de la "d" de Cohen para mas de dos grupos

2020-09-10 Thread Gerardo Gold
¡Buenas tardes! (en el continente americano):

Tengo los resultados de cuatro muestreos de campo (cuatro
cruceros oceanográficos) y quiero calcular la "d"de Cohen para todas las
combinaciones posibles de los cuatro cruceros. Los paquetes de R que he
encontrado solo permiten hacer el cálculo para un par de grupos.

¿Hay algo que pueda hacer para hacer el cálculo de las seis combinaciones
posibles? Espero no estar preguntando una tontería.

Saludos desde México,

Gerardo

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] aplicar codigo

2020-09-10 Thread Marcelino de la Cruz Rot

Ya sé a qué se debe:
En vuestro R, la función data.frame transforma automáticamente los 
vectores de tipo /character/ a tipo/factor/.

Con un factor, sapply devuelve el número del nivel correspondiente:

> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35', 
'x1','x2', 'x4')


> sapply(col1, function(x) ifelse(x%in%c("x1","x2","x3"), 
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
    x1 x2    x11 x1    x33 x1 
x4 x5    x35 x1 x2 x4
"prueba12" "prueba12"  "x11" "prueba12"  "x33" "prueba12" 
"prueba2"  "prueba2"  "x35" "prueba12" "prueba12"  "prueba2"


> sapply(factor(col1), function(x) ifelse(x%in%c("x1","x2","x3"), 
"prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
 [1] "prueba12" "prueba12" "2"    "prueba12" "4" "prueba12" 
"prueba2"  "prueba2"  "5"    "prueba12" "prueba12" "prueba2"

>




El 10/09/2020 a las 20:35, David Mateos escribió:

Hola,
a mí me sucede como a Samura y no consigo entender de donde salen esos 
"2", "4" y "5" para df1. ¿Alguien puede arrojar luz?

Tanto con la versión 3.6 como con la 4.0.2

Si me funciona con
```
transforma <- function(df) {
  apply(df, 1, function(x)
    ifelse(x%in%c("x1","x2","x3"),
           "prueba12",
           ifelse(x%in%c("x4","x5","x6"),
                  "prueba2",x)
           )
    )
}
df1$transformacion <- transforma(df1)
```
Un saludo,
David

Enviado desde Outlook 


*De:* R-help-es  en nombre de 
Marcelino de la Cruz Rot 

*Enviado:* jueves, 10 de septiembre de 2020 19:15
*Para:* Samura . ; r-help-es@r-project.org 


*Asunto:* Re: [R-es] aplicar codigo
Yo copio y pego este código y me sale correctamente. Se me ocurre que
pueda deberse a la versión de R ¿cuál usas?


El 10/09/2020 a las 17:51, Samura . escribió:
> Gracias por las respuestas.
>
> Probé lo de hacer la función y no me salía. Pensaba que hacía algo mal.
> Ahora con el código de Marcelino tampoco me sale.
>
> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> 'x1','x2', 'x4')
> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> 'x35','x2','x2', 'x4','x6', 'x5')
>
> df1<-data.frame(col1)
> df2<-data.frame(col2)
> df3<-data.frame(col3)
>
> transforma <- function(df) sapply(df, function(x)
>     ifelse(x%in%c("x1","x2","x3"),
>  "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
> transforma(df1)
> col1
> [1,] "prueba12"
> [2,] "prueba12"
> [3,] "2"
> [4,] "prueba12"
> [5,] "4"
> [6,] "prueba12"
> [7,] "prueba2"
> [8,] "prueba2"
> [9,] "5"
> [10,] "prueba12"
> [11,] "prueba12"
> [12,] "prueba2"
>
> ¿Alguna idea de pq me sale 2, 4, 5?
> 
> *De:* R-help-es  en nombre de
> Marcelino de la Cruz Rot 
> *Enviado:* jueves, 10 de septiembre de 2020 9:36
> *Para:* r-help-es@r-project.org 
> *Asunto:* Re: [R-es] aplicar codigo
> Hola:
> Como dice Carlos, algo así, por ejemplo:
>
> transforma <- function(df) sapply(df, function(x)
> ifelse(x%in%c("x1","x2","x3"),
> "prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
>
>  > transforma(df1)
>    col1
>   [1,] "prueba1"
>   [2,] "prueba1"
>   [3,] "x11"
>   [4,] "prueba1"
>   [5,] "x33"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
>
>
>  > transforma(df2)
>   col2
> [1,] "x12"
> [2,] "prueba2"
> [3,] "prueba2"
> [4,] "x771"
> [5,] "prueba2"
> [6,] "prueba1"
>
>
>  > transforma(df3)
>    col3
>   [1,] "x7"
>   [2,] "prueba1"
>   [3,] "prueba2"
>   [4,] "prueba2"
>   [5,] "x111"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
> [13,] "prueba2"
> [14,] "prueba2"
>
> Saludos,
>
> Marcelino
>
> El 10/09/2020 a las 9:41, Carlos Ortega escribió:
> > Hola,
> > Si lo estoy entendiendo bien, lo que tienes que crearte es una 
función.
> > A la función le pasarías unos parámetros (las columnas y el 
dataframe al

> > que añadir la transformación), y en el cuerpo de la función haces esos
> > cálculos.
> >
> > Gracias,
> > Carlos.
> >
> >
> >
> > Gracias,
> > Carlos.
> >
> > El jue., 10 sept. 2020 a las 3:55, Samura . ()
> > escribió:
> >
> >> Hola,
> >> me gustaría hacer algo como en el siguiente ejemplo
> >>
> >> A un df añadirle una columna que es la transformación de otra,
> >> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
> >> todo lo que sea x4,x5,x6 lo llamo prueba 2
> >> el resto de x las dejo como están.
> >>
> >> Sería algo así
> >>
> >> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> >> 'x1','x2', 'x4')
> >> df1<-data.frame(col1)
> >> attach(df1)
> >>
> >> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
> >> "prueba1",
> >> ifelse(col1 == "x4"|col1 

Re: [R-es] aplicar codigo

2020-09-10 Thread Marcelino de la Cruz Rot

OK. Yo lo he hecho con R version 4.0.0 (2020-04-24) -- "Arbor Day".
Ya nos cuentas si funciona.

El 10/09/2020 a las 20:19, Samura . escribió:

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Rstudio Version 1.2.1335

Voy a actualizarlo todo a ver que pasa.

*De:* Marcelino de la Cruz Rot 
*Enviado:* jueves, 10 de septiembre de 2020 17:15
*Para:* Samura . ; r-help-es@r-project.org 


*Asunto:* Re: [R-es] aplicar codigo
Yo copio y pego este código y me sale correctamente. Se me ocurre que
pueda deberse a la versión de R ¿cuál usas?


El 10/09/2020 a las 17:51, Samura . escribió:
> Gracias por las respuestas.
>
> Probé lo de hacer la función y no me salía. Pensaba que hacía algo mal.
> Ahora con el código de Marcelino tampoco me sale.
>
> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> 'x1','x2', 'x4')
> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> 'x35','x2','x2', 'x4','x6', 'x5')
>
> df1<-data.frame(col1)
> df2<-data.frame(col2)
> df3<-data.frame(col3)
>
> transforma <- function(df) sapply(df, function(x)
>     ifelse(x%in%c("x1","x2","x3"),
>  "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
> transforma(df1)
> col1
> [1,] "prueba12"
> [2,] "prueba12"
> [3,] "2"
> [4,] "prueba12"
> [5,] "4"
> [6,] "prueba12"
> [7,] "prueba2"
> [8,] "prueba2"
> [9,] "5"
> [10,] "prueba12"
> [11,] "prueba12"
> [12,] "prueba2"
>
> ¿Alguna idea de pq me sale 2, 4, 5?
> 
> *De:* R-help-es  en nombre de
> Marcelino de la Cruz Rot 
> *Enviado:* jueves, 10 de septiembre de 2020 9:36
> *Para:* r-help-es@r-project.org 
> *Asunto:* Re: [R-es] aplicar codigo
> Hola:
> Como dice Carlos, algo así, por ejemplo:
>
> transforma <- function(df) sapply(df, function(x)
> ifelse(x%in%c("x1","x2","x3"),
> "prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
>
>  > transforma(df1)
>    col1
>   [1,] "prueba1"
>   [2,] "prueba1"
>   [3,] "x11"
>   [4,] "prueba1"
>   [5,] "x33"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
>
>
>  > transforma(df2)
>   col2
> [1,] "x12"
> [2,] "prueba2"
> [3,] "prueba2"
> [4,] "x771"
> [5,] "prueba2"
> [6,] "prueba1"
>
>
>  > transforma(df3)
>    col3
>   [1,] "x7"
>   [2,] "prueba1"
>   [3,] "prueba2"
>   [4,] "prueba2"
>   [5,] "x111"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
> [13,] "prueba2"
> [14,] "prueba2"
>
> Saludos,
>
> Marcelino
>
> El 10/09/2020 a las 9:41, Carlos Ortega escribió:
> > Hola,
> > Si lo estoy entendiendo bien, lo que tienes que crearte es una 
función.
> > A la función le pasarías unos parámetros (las columnas y el 
dataframe al

> > que añadir la transformación), y en el cuerpo de la función haces esos
> > cálculos.
> >
> > Gracias,
> > Carlos.
> >
> >
> >
> > Gracias,
> > Carlos.
> >
> > El jue., 10 sept. 2020 a las 3:55, Samura . ()
> > escribió:
> >
> >> Hola,
> >> me gustaría hacer algo como en el siguiente ejemplo
> >>
> >> A un df añadirle una columna que es la transformación de otra,
> >> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
> >> todo lo que sea x4,x5,x6 lo llamo prueba 2
> >> el resto de x las dejo como están.
> >>
> >> Sería algo así
> >>
> >> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> >> 'x1','x2', 'x4')
> >> df1<-data.frame(col1)
> >> attach(df1)
> >>
> >> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
> >> "prueba1",
> >> ifelse(col1 == "x4"|col1 == "x5"| col1
> == "x6",
> >> "prueba2", col1))
> >>
> >> detach(df1)
> >> df1
> >>
> >> pero ahora en vez de un df tengo varios
> >>
> >> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> >> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> 'x35','x2','x2',
> >> 'x4','x6', 'x5')
> >> df2<-data.frame(col2)
> >> df3<-data.frame(col3)
> >>
> >> ¿cómo puedo aplicar el código al resto de los df sin tener que
> repetirlo?
> >>
> >>
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> ___
> >> R-help-es mailing list
> >> R-help-es@r-project.org
> >> https://stat.ethz.ch/mailman/listinfo/r-help-es 

> >

> >>
> >
>
> --
> Marcelino de la Cruz Rot
> Depto. de Biología y Geología
> Física y Química Inorgánica
> Universidad Rey Juan Carlos
> Móstoles España
>
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es 

> 

Re: [R-es] aplicar codigo

2020-09-10 Thread Samura .
R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Rstudio Version 1.2.1335

Voy a actualizarlo todo a ver que pasa.

De: Marcelino de la Cruz Rot 
Enviado: jueves, 10 de septiembre de 2020 17:15
Para: Samura . ; r-help-es@r-project.org 

Asunto: Re: [R-es] aplicar codigo

Yo copio y pego este c�digo y me sale correctamente. Se me ocurre que
pueda deberse a la versi�n de R �cu�l usas?


El 10/09/2020 a las 17:51, Samura . escribi�:
> Gracias por las respuestas.
>
> Prob� lo de hacer la funci�n y no me sal�a. Pensaba que hac�a algo mal.
> Ahora con el c�digo de Marcelino tampoco me sale.
>
> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> 'x1','x2', 'x4')
> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> 'x35','x2','x2', 'x4','x6', 'x5')
>
> df1<-data.frame(col1)
> df2<-data.frame(col2)
> df3<-data.frame(col3)
>
> transforma <- function(df) sapply(df, function(x)
> ifelse(x%in%c("x1","x2","x3"),
>  "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
> transforma(df1)
> col1
> [1,] "prueba12"
> [2,] "prueba12"
> [3,] "2"
> [4,] "prueba12"
> [5,] "4"
> [6,] "prueba12"
> [7,] "prueba2"
> [8,] "prueba2"
> [9,] "5"
> [10,] "prueba12"
> [11,] "prueba12"
> [12,] "prueba2"
>
> �Alguna idea de pq me sale 2, 4, 5?
> 
> *De:* R-help-es  en nombre de
> Marcelino de la Cruz Rot 
> *Enviado:* jueves, 10 de septiembre de 2020 9:36
> *Para:* r-help-es@r-project.org 
> *Asunto:* Re: [R-es] aplicar codigo
> Hola:
> Como dice Carlos, algo as�, por ejemplo:
>
> transforma <- function(df) sapply(df, function(x)
> ifelse(x%in%c("x1","x2","x3"),
> "prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))
>
>
>  > transforma(df1)
>col1
>   [1,] "prueba1"
>   [2,] "prueba1"
>   [3,] "x11"
>   [4,] "prueba1"
>   [5,] "x33"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
>
>
>  > transforma(df2)
>   col2
> [1,] "x12"
> [2,] "prueba2"
> [3,] "prueba2"
> [4,] "x771"
> [5,] "prueba2"
> [6,] "prueba1"
>
>
>  > transforma(df3)
>col3
>   [1,] "x7"
>   [2,] "prueba1"
>   [3,] "prueba2"
>   [4,] "prueba2"
>   [5,] "x111"
>   [6,] "prueba1"
>   [7,] "prueba2"
>   [8,] "prueba2"
>   [9,] "x35"
> [10,] "prueba1"
> [11,] "prueba1"
> [12,] "prueba2"
> [13,] "prueba2"
> [14,] "prueba2"
>
> Saludos,
>
> Marcelino
>
> El 10/09/2020 a las 9:41, Carlos Ortega escribi�:
> > Hola,
> > Si lo estoy entendiendo bien, lo que tienes que crearte es una funci�n.
> > A la funci�n le pasar�as unos par�metros (las columnas y el dataframe al
> > que a�adir la transformaci�n), y en el cuerpo de la funci�n haces esos
> > c�lculos.
> >
> > Gracias,
> > Carlos.
> >
> >
> >
> > Gracias,
> > Carlos.
> >
> > El jue., 10 sept. 2020 a las 3:55, Samura . ()
> > escribi�:
> >
> >> Hola,
> >> me gustar�a hacer algo como en el siguiente ejemplo
> >>
> >> A un df a�adirle una columna que es la transformaci�n de otra,
> >> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
> >> todo lo que sea x4,x5,x6 lo llamo prueba 2
> >> el resto de x las dejo como est�n.
> >>
> >> Ser�a algo as�
> >>
> >> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> >> 'x1','x2', 'x4')
> >> df1<-data.frame(col1)
> >> attach(df1)
> >>
> >> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
> >> "prueba1",
> >> ifelse(col1 == "x4"|col1 == "x5"| col1
> == "x6",
> >> "prueba2", col1))
> >>
> >> detach(df1)
> >> df1
> >>
> >> pero ahora en vez de un df tengo varios
> >>
> >> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> >> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5',
> 'x35','x2','x2',
> >> 'x4','x6', 'x5')
> >> df2<-data.frame(col2)
> >> df3<-data.frame(col3)
> >>
> >> �c�mo puedo aplicar el c�digo al resto de los df sin tener que
> repetirlo?
> >>
> >>
> >>
> >>  [[alternative HTML version deleted]]
> >>
> >> ___
> >> R-help-es mailing list
> >> R-help-es@r-project.org
> >> https://stat.ethz.ch/mailman/listinfo/r-help-es
> 
> >>
> >
>
> --
> Marcelino de la Cruz Rot
> Depto. de Biolog�a y Geolog�a
> F�sica y Qu�mica Inorg�nica
> Universidad Rey Juan Carlos
> M�stoles Espa�a
>
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es
> 


--
Marcelino de la Cruz Rot
Depto. de Biolog�a y Geolog�a
F�sica y Qu�mica Inorg�nica
Universidad Rey Juan Carlos
M�stoles Espa�a


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] aplicar codigo

2020-09-10 Thread Marcelino de la Cruz Rot
Yo copio y pego este código y me sale correctamente. Se me ocurre que 
pueda deberse a la versión de R ¿cuál usas?



El 10/09/2020 a las 17:51, Samura . escribió:

Gracias por las respuestas.

Probé lo de hacer la función y no me salía. Pensaba que hacía algo mal.
Ahora con el código de Marcelino tampoco me sale.

col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35', 
'x1','x2', 'x4')

col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 
'x35','x2','x2', 'x4','x6', 'x5')


df1<-data.frame(col1)
df2<-data.frame(col2)
df3<-data.frame(col3)

transforma <- function(df) sapply(df, function(x)
    ifelse(x%in%c("x1","x2","x3"),
 "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))

transforma(df1)
col1
[1,] "prueba12"
[2,] "prueba12"
[3,] "2"
[4,] "prueba12"
[5,] "4"
[6,] "prueba12"
[7,] "prueba2"
[8,] "prueba2"
[9,] "5"
[10,] "prueba12"
[11,] "prueba12"
[12,] "prueba2"

¿Alguna idea de pq me sale 2, 4, 5?

*De:* R-help-es  en nombre de 
Marcelino de la Cruz Rot 

*Enviado:* jueves, 10 de septiembre de 2020 9:36
*Para:* r-help-es@r-project.org 
*Asunto:* Re: [R-es] aplicar codigo
Hola:
Como dice Carlos, algo así, por ejemplo:

transforma <- function(df) sapply(df, function(x)
ifelse(x%in%c("x1","x2","x3"),
"prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))


 > transforma(df1)
   col1
  [1,] "prueba1"
  [2,] "prueba1"
  [3,] "x11"
  [4,] "prueba1"
  [5,] "x33"
  [6,] "prueba1"
  [7,] "prueba2"
  [8,] "prueba2"
  [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"


 > transforma(df2)
  col2
[1,] "x12"
[2,] "prueba2"
[3,] "prueba2"
[4,] "x771"
[5,] "prueba2"
[6,] "prueba1"


 > transforma(df3)
   col3
  [1,] "x7"
  [2,] "prueba1"
  [3,] "prueba2"
  [4,] "prueba2"
  [5,] "x111"
  [6,] "prueba1"
  [7,] "prueba2"
  [8,] "prueba2"
  [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"
[13,] "prueba2"
[14,] "prueba2"

Saludos,

Marcelino

El 10/09/2020 a las 9:41, Carlos Ortega escribió:
> Hola,
> Si lo estoy entendiendo bien, lo que tienes que crearte es una función.
> A la función le pasarías unos parámetros (las columnas y el dataframe al
> que añadir la transformación), y en el cuerpo de la función haces esos
> cálculos.
>
> Gracias,
> Carlos.
>
>
>
> Gracias,
> Carlos.
>
> El jue., 10 sept. 2020 a las 3:55, Samura . ()
> escribió:
>
>> Hola,
>> me gustaría hacer algo como en el siguiente ejemplo
>>
>> A un df añadirle una columna que es la transformación de otra,
>> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
>> todo lo que sea x4,x5,x6 lo llamo prueba 2
>> el resto de x las dejo como están.
>>
>> Sería algo así
>>
>> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
>> 'x1','x2', 'x4')
>> df1<-data.frame(col1)
>> attach(df1)
>>
>> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
>> "prueba1",
>> ifelse(col1 == "x4"|col1 == "x5"| col1 
== "x6",

>> "prueba2", col1))
>>
>> detach(df1)
>> df1
>>
>> pero ahora en vez de un df tengo varios
>>
>> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
>> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 
'x35','x2','x2',

>> 'x4','x6', 'x5')
>> df2<-data.frame(col2)
>> df3<-data.frame(col3)
>>
>> ¿cómo puedo aplicar el código al resto de los df sin tener que 
repetirlo?

>>
>>
>>
>>  [[alternative HTML version deleted]]
>>
>> ___
>> R-help-es mailing list
>> R-help-es@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-help-es 


>>
>

--
Marcelino de la Cruz Rot
Depto. de Biología y Geología
Física y Química Inorgánica
Universidad Rey Juan Carlos
Móstoles España

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es 




--
Marcelino de la Cruz Rot
Depto. de Biología y Geología
Física y Química Inorgánica
Universidad Rey Juan Carlos
Móstoles España

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] aplicar codigo

2020-09-10 Thread Samura .
Gracias por las respuestas.

Prob� lo de hacer la funci�n y no me sal�a. Pensaba que hac�a algo mal.
Ahora con el c�digo de Marcelino tampoco me sale.

col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35', 'x1','x2', 
'x4')
col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2', 
'x4','x6', 'x5')

df1<-data.frame(col1)
df2<-data.frame(col2)
df3<-data.frame(col3)

transforma <- function(df) sapply(df, function(x)
ifelse(x%in%c("x1","x2","x3"),
   "prueba12",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))

transforma(df1)
col1
[1,] "prueba12"
[2,] "prueba12"
[3,] "2"
[4,] "prueba12"
[5,] "4"
[6,] "prueba12"
[7,] "prueba2"
[8,] "prueba2"
[9,] "5"
[10,] "prueba12"
[11,] "prueba12"
[12,] "prueba2"

�Alguna idea de pq me sale 2, 4, 5?

De: R-help-es  en nombre de Marcelino de la 
Cruz Rot 
Enviado: jueves, 10 de septiembre de 2020 9:36
Para: r-help-es@r-project.org 
Asunto: Re: [R-es] aplicar codigo

Hola:
Como dice Carlos, algo as�, por ejemplo:

transforma <- function(df) sapply(df, function(x)
ifelse(x%in%c("x1","x2","x3"),
"prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))


 > transforma(df1)
   col1
  [1,] "prueba1"
  [2,] "prueba1"
  [3,] "x11"
  [4,] "prueba1"
  [5,] "x33"
  [6,] "prueba1"
  [7,] "prueba2"
  [8,] "prueba2"
  [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"


 > transforma(df2)
  col2
[1,] "x12"
[2,] "prueba2"
[3,] "prueba2"
[4,] "x771"
[5,] "prueba2"
[6,] "prueba1"


 > transforma(df3)
   col3
  [1,] "x7"
  [2,] "prueba1"
  [3,] "prueba2"
  [4,] "prueba2"
  [5,] "x111"
  [6,] "prueba1"
  [7,] "prueba2"
  [8,] "prueba2"
  [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"
[13,] "prueba2"
[14,] "prueba2"

Saludos,

Marcelino

El 10/09/2020 a las 9:41, Carlos Ortega escribi�:
> Hola,
> Si lo estoy entendiendo bien, lo que tienes que crearte es una funci�n.
> A la funci�n le pasar�as unos par�metros (las columnas y el dataframe al
> que a�adir la transformaci�n), y en el cuerpo de la funci�n haces esos
> c�lculos.
>
> Gracias,
> Carlos.
>
>
>
> Gracias,
> Carlos.
>
> El jue., 10 sept. 2020 a las 3:55, Samura . ()
> escribi�:
>
>> Hola,
>> me gustar�a hacer algo como en el siguiente ejemplo
>>
>> A un df a�adirle una columna que es la transformaci�n de otra,
>> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
>> todo lo que sea x4,x5,x6 lo llamo prueba 2
>> el resto de x las dejo como est�n.
>>
>> Ser�a algo as�
>>
>> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
>> 'x1','x2', 'x4')
>> df1<-data.frame(col1)
>> attach(df1)
>>
>> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
>> "prueba1",
>> ifelse(col1 == "x4"|col1 == "x5"| col1 == "x6",
>> "prueba2", col1))
>>
>> detach(df1)
>> df1
>>
>> pero ahora en vez de un df tengo varios
>>
>> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
>> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2',
>> 'x4','x6', 'x5')
>> df2<-data.frame(col2)
>> df3<-data.frame(col3)
>>
>> �c�mo puedo aplicar el c�digo al resto de los df sin tener que repetirlo?
>>
>>
>>
>>  [[alternative HTML version deleted]]
>>
>> ___
>> R-help-es mailing list
>> R-help-es@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-help-es
>>
>

--
Marcelino de la Cruz Rot
Depto. de Biolog�a y Geolog�a
F�sica y Qu�mica Inorg�nica
Universidad Rey Juan Carlos
M�stoles Espa�a

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Luigi Marongiu
Update:
I also added the confidence interval for the Shannon index:
```
#! Hutcheson's t-test for Shannon diversity equality
# thanks to Karl Schilling and Rui Barradas
hutcheson = function(A, B){
  # compute Shannon index, variance and sum of elements
  A_index <- Shannon(A)
  B_index <- Shannon(B)
  A_var <- ShannonVar(A)
  B_var <- ShannonVar(B)
  A_sum <- sum(A)
  B_sum <- sum(B)
  # compute Hutcheson
  DF <- (A_var + B_var)^2 /(A_var^2/A_sum + B_var^2/B_sum)
  test <- (A_index-B_index) /sqrt(A_var + B_var)
  p <- 2*pt(-abs(test),df= DF)
  if (p < 0.001) {
P = "<0.001"
  } else {
P = round(p, 3)
  }
  if (p < 0.001) {
S = "***"
  } else if (p < 0.01) {
S = "**"
  } else if (p < 0.05) {
S = "*"
  } else {
S = ""
  }
  # closure
  cat("Hutcheson's t-test for Shannon diversity equality\n\tShannon
index first group: \t",
  round(A_index, 3), " (", round((A_index-2*A_var),3), "-",
round((A_index+2*A_var),3),
  ")\n\tShannon index second group: \t",
  round(B_index, 3), " (", round((B_index-2*B_var),3), "-",
round((B_index+2*B_var),3),
  ")\n\tp-value: ", P, " ", S, "\n", sep = "")
  return(p)
}
```

On Thu, Sep 10, 2020 at 2:10 PM Luigi Marongiu  wrote:
>
> Hello,
> thank you for the code. To explain better, when I used vegan, I did
> not count the species directly but simply prepared a dataframe where,
> for each species, I counted the number of samples bearing such
> species:
> ```
>
> > str(new_df)
> 'data.frame': 3 obs. of  46 variables:
>  $ NC_001416 Enterobacteria phage lambda   : int  5 4 5
>  $ NC_001623 Autographa californica nucl...: int  7 7 7
>  $ NC_001895 Enterobacteria phage P2   : int  1 0 0
>  $ NC_004745 Yersinia phage L-413C : int  1 0 0
> ```
> here the triplettes refer to healthy, tumor and metastasis. The outcome is:
> ```
> # Shannon index
> diversity(new_df)
> #> Normal Tumour Metastasis
> #> 2.520139   3.109512   1.890404
> ```
> Using iNext, I provided a list of all the species counted in a samples
> ```
> > new_list
> $Healthy
>  [1] 5 7 1 1 1 8 1 1 2 1 2 1 1 1 1 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0 0
>
> $Tumour
>  [1] 4 7 0 0 0 7 0 0 1 0 1 0 0 0 0 2 0 0 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1
> 1 2 1 1 1 1 1 1 1 0 0 0 0
>
> $Metastasis
>  [1] 5 7 0 0 0 9 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 1 0 0 1 1 1 1
> ```
> From this I get:
> ```
> mod = iNEXT(new_list, q=0, datatype="abundance")
> mod$AsyEst
> #Site Diversity Observed Estimator   s.e.LCL UCL
> #1 Normal  Species richness   18.00041.368 19.683 23.563 116.155
> #2 Normal Shannon diversity   12.43021.343  5.183 12.430  31.501
> #4 Tumour  Species richness   30.00094.776 42.936 49.848 241.396
> #5 Tumour Shannon diversity   22.41053.135 14.486 24.743  81.526
> #7 Metastasis  Species richness   10.00027.379 22.821 12.443 133.640
> #8 Metastasis Shannon diversity6.622 9.980  3.102  6.622  16.059
> ```
> So here the Shannon index is 12 instead of 2.5...
> Using Karl's function, I get:
> ```
> # compute Shannon
> norm_sIdx <- Shannon(array(as.numeric(unlist(new_list[1]
> canc_sIdx <- Shannon(array(as.numeric(unlist(new_list[2]
> meta_sIdx <- Shannon(array(as.numeric(unlist(new_list[3]
> norm_var <- ShannonVar(array(as.numeric(unlist(new_list[1]
> canc_var <- ShannonVar(array(as.numeric(unlist(new_list[2]
> meta_var <- ShannonVar(array(as.numeric(unlist(new_list[3]
> norm_sum <- sum(array(as.numeric(unlist(new_list[1]
> canc_sum <- sum(array(as.numeric(unlist(new_list[2]
> meta_sum <- sum(array(as.numeric(unlist(new_list[3]
> # compute Hutcheson
> degfree <- (norm_var + canc_var)**2 /(norm_var**2/norm_sum +
> canc_var**2 /canc_sum)
> test <- (norm_sIdx-canc_sIdx) /sqrt(norm_var + canc_var)
> (p <- 2*pt(-abs(test),df= degree))
> > [1] 0.01825784
> ```
> remarkably, the indices are the same as obtained by vegan:
> ```
> > norm_sIdx
> [1] 2.520139
> > canc_sIdx
> [1] 3.109512
> > meta_sIdx
> [1] 1.890404
> ```
>
> I tried Rui's function but I got an error, so I wrote it as
> ```
> hutcheson = function(A, B){
>   # compute Shannon index, variance and sum of elements
>   A_index <- Shannon(A)
>   B_index <- Shannon(B)
>   A_var <- ShannonVar(A)
>   B_var <- ShannonVar(B)
>   A_sum <- sum(A)
>   B_sum <- sum(B)
>   # compute Hutcheson
>   DF <- (A_var + B_var)^2 /(A_var^2/A_sum + B_var^2/B_sum)
>   test <- (A_index-B_index) /sqrt(A_var + B_var)
>   p <- 2*pt(-abs(test),df= DF)
>   # closure
>   cat("Hutcheson's t-test for Shannon diversity equality\n\tShannon
> index first group: ",
>   round(A_index, 4), "\n\tShannon index second group: ", round(B_index, 
> 4),
>   "\n\tp-value : ", round(p, 4), "\n", sep = "")
>   return(p)
> }
> ```
> and I got:
> ```
>
> > n_t = hutcheson(array(as.numeric(unlist(new_list[1]))), 
> > array(as.numeric(unlist(new_list[2]
> Hutcheson's t-test for Shannon diversity equality
> Shannon index 

Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Luigi Marongiu
Hello,
thank you for the code. To explain better, when I used vegan, I did
not count the species directly but simply prepared a dataframe where,
for each species, I counted the number of samples bearing such
species:
```

> str(new_df)
'data.frame': 3 obs. of  46 variables:
 $ NC_001416 Enterobacteria phage lambda   : int  5 4 5
 $ NC_001623 Autographa californica nucl...: int  7 7 7
 $ NC_001895 Enterobacteria phage P2   : int  1 0 0
 $ NC_004745 Yersinia phage L-413C : int  1 0 0
```
here the triplettes refer to healthy, tumor and metastasis. The outcome is:
```
# Shannon index
diversity(new_df)
#> Normal Tumour Metastasis
#> 2.520139   3.109512   1.890404
```
Using iNext, I provided a list of all the species counted in a samples
```
> new_list
$Healthy
 [1] 5 7 1 1 1 8 1 1 2 1 2 1 1 1 1 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0

$Tumour
 [1] 4 7 0 0 0 7 0 0 1 0 1 0 0 0 0 2 0 0 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1 0 0 0 0

$Metastasis
 [1] 5 7 0 0 0 9 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 1 1 1 1
```
>From this I get:
```
mod = iNEXT(new_list, q=0, datatype="abundance")
mod$AsyEst
#Site Diversity Observed Estimator   s.e.LCL UCL
#1 Normal  Species richness   18.00041.368 19.683 23.563 116.155
#2 Normal Shannon diversity   12.43021.343  5.183 12.430  31.501
#4 Tumour  Species richness   30.00094.776 42.936 49.848 241.396
#5 Tumour Shannon diversity   22.41053.135 14.486 24.743  81.526
#7 Metastasis  Species richness   10.00027.379 22.821 12.443 133.640
#8 Metastasis Shannon diversity6.622 9.980  3.102  6.622  16.059
```
So here the Shannon index is 12 instead of 2.5...
Using Karl's function, I get:
```
# compute Shannon
norm_sIdx <- Shannon(array(as.numeric(unlist(new_list[1]
canc_sIdx <- Shannon(array(as.numeric(unlist(new_list[2]
meta_sIdx <- Shannon(array(as.numeric(unlist(new_list[3]
norm_var <- ShannonVar(array(as.numeric(unlist(new_list[1]
canc_var <- ShannonVar(array(as.numeric(unlist(new_list[2]
meta_var <- ShannonVar(array(as.numeric(unlist(new_list[3]
norm_sum <- sum(array(as.numeric(unlist(new_list[1]
canc_sum <- sum(array(as.numeric(unlist(new_list[2]
meta_sum <- sum(array(as.numeric(unlist(new_list[3]
# compute Hutcheson
degfree <- (norm_var + canc_var)**2 /(norm_var**2/norm_sum +
canc_var**2 /canc_sum)
test <- (norm_sIdx-canc_sIdx) /sqrt(norm_var + canc_var)
(p <- 2*pt(-abs(test),df= degree))
> [1] 0.01825784
```
remarkably, the indices are the same as obtained by vegan:
```
> norm_sIdx
[1] 2.520139
> canc_sIdx
[1] 3.109512
> meta_sIdx
[1] 1.890404
```

I tried Rui's function but I got an error, so I wrote it as
```
hutcheson = function(A, B){
  # compute Shannon index, variance and sum of elements
  A_index <- Shannon(A)
  B_index <- Shannon(B)
  A_var <- ShannonVar(A)
  B_var <- ShannonVar(B)
  A_sum <- sum(A)
  B_sum <- sum(B)
  # compute Hutcheson
  DF <- (A_var + B_var)^2 /(A_var^2/A_sum + B_var^2/B_sum)
  test <- (A_index-B_index) /sqrt(A_var + B_var)
  p <- 2*pt(-abs(test),df= DF)
  # closure
  cat("Hutcheson's t-test for Shannon diversity equality\n\tShannon
index first group: ",
  round(A_index, 4), "\n\tShannon index second group: ", round(B_index, 4),
  "\n\tp-value : ", round(p, 4), "\n", sep = "")
  return(p)
}
```
and I got:
```

> n_t = hutcheson(array(as.numeric(unlist(new_list[1]))), 
> array(as.numeric(unlist(new_list[2]
Hutcheson's t-test for Shannon diversity equality
Shannon index first group: 2.5201
Shannon index second group: 3.1095
p-value : 0.0183
> n_m = hutcheson(array(as.numeric(unlist(new_list[1]))), 
> array(as.numeric(unlist(new_list[3]
Hutcheson's t-test for Shannon diversity equality
Shannon index first group: 2.5201
Shannon index second group: 1.8904
p-value : 0.0371
> t_m = hutcheson(array(as.numeric(unlist(new_list[2]))), 
> array(as.numeric(unlist(new_list[3]
Hutcheson's t-test for Shannon diversity equality
Shannon index first group: 3.1095
Shannon index second group: 1.8904
p-value : 0
```
new_list[1]|[2]|[3] refer to healthy, tumor and metastasis. applied to
the original Hutcheson data:
```
> bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
> bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
> hutcheson(bird_1956, bird_1957)
Hutcheson's t-test for Shannon diversity equality
Shannon index first group: 1.8429
Shannon index second group: 1.0689
p-value : 0

```
This is to compare two groups at the time. I'll probably have to
compensate for multiple testing...
But if this all OK, then the case is closed.
Thank you

On Thu, Sep 10, 2020 at 1:04 PM Rui Barradas  wrote:
>
> Hello,
>
> Sorry, there's an instruction missing. See inline.
>
> Às 11:44 de 10/09/20, Rui Barradas escreveu:
> > If you want a function automating Karl's code, here it is. It returns an
> > object of S3 class "htest", R's standard for hypothesis tests functions.
> > The 

Re: [R] facet_wrap(nrow) ignored

2020-09-10 Thread Ivan Calandra
Thank you Ulrik for the suggestions.

I was thinking of a similar approach using a nested patchwork (which
would be easier for me).

It's just a shame that it is not possible to force a number of rows.
It's good that ggplot2 tries to do things in the most "appropriate" way,
but at some point, when the user decides it needs to have 5 rows, then
ggplot2 should listen, potentially issuing a warning like "the number of
rows specified is not appropriate, consider other values instead (e.g. 2)".

Best,
Ivan

PS: our email server is having troubles today, so I have not received
any other R-related emails. Are there few today or is it just me? There
might even be more answers to my question...

--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 10/09/2020 11:54, Ulrik Stervbo wrote:
> Dear Ivan,
>
> I don't think it is possible to force a number of rows - but I'm
> honestly just guessing.
>
> What you can do is to add an empty plot. Here I use cowplot, but
> gridExtra should also work well.
>
> I add an indication of the row number for the plot to the initial
> data.frame, and loop over these.
>
> In the first variant, I add an unused factor to the grp which creates
> an empty facet. I personally think this looks a little confusing, so
> in the second variant, I add a number of empty plots.
>
> HTH
> Ulrik
>
> ```
> mydf <- data.frame(
>   grp = rep(letters[1:6], each = 15),
>   cat = rep(1:3, 30),
>   var = rnorm(90),
>   row_num = rep(c(1, 1, 2, 3, 4, 5), each = 15)
> )
>
> s_mydf <- split(mydf, mydf$row_num)
>
> plots_mydf <- lapply(s_mydf, function(x){
>   # Ensure no unused factors
>   x$grp <- droplevels.factor(x$grp)
>   if(length(unique(x$grp)) == 1){
>     x$grp <- factor(x$grp, levels = c(unique(x$grp), ""))
>   }
>   ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
>     facet_wrap(~grp, drop=FALSE)
> })
>
> cowplot::plot_grid(plotlist = plots_mydf, nrow = 5)
>
> # Maybe more elegant output
> plots_mydf <- lapply(s_mydf, function(x, ncol = 2){
>   # Ensure no unused factors
>   x$grp <- droplevels.factor(x$grp)
>   x <- split(x, x$grp)
>
>   p <- lapply(x, function(x){
>     ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
>   facet_wrap(~grp)
>   })
>
>   if(length(p) < ncol){
>     pe <- rep(list(ggplot() + theme_void()), ncol - length(p))
>     p <- c(p, pe)
>   }
>   cowplot::plot_grid(plotlist = p, ncol = ncol)
> })
>
> cowplot::plot_grid(plotlist = plots_mydf, ncol = 1)
>
> # Or if you prefer not to split the plots on the same row
> plots_mydf <- lapply(s_mydf, function(x, ncol = 2){
>
>   p <- list(ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
>     facet_wrap(~grp))
>
>   if(length(unique(x$grp)) < ncol){
>     pe <- rep(list(ggplot() + theme_void()), ncol - length(p))
>     p <- c(p, pe)
>   }else{
>     ncol <- 1
>   }
>   cowplot::plot_grid(plotlist = p, ncol = ncol)
> })
>
> cowplot::plot_grid(plotlist = plots_mydf, ncol = 1)
>
> ```
>
> On 2020-09-09 17:30, Ivan Calandra wrote:
>> Dear useRs,
>>
>> I have an issue with the argument nrow of ggplot2::facet_wrap().
>>
>> Let's consider some sample data:
>> mydf <- data.frame(grp = rep(letters[1:6], each = 15), cat = rep(1:3,
>> 30), var = rnorm(90))
>>
>> And let's try to plot with 5 rows:
>> library(ggplot2)
>> ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
>> facet_wrap(~grp, nrow = 5)
>> It plots 2 rows and 3 columns rather than 5 rows and 2 columns as
>> wanted.
>>
>> These plots are as expected:
>> ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
>> facet_wrap(~grp, nrow = 2)
>> ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
>> facet_wrap(~grp, nrow = 6)
>>
>> My guess is that 5 rows is not ideal for 6 facets (5 facets in 1st
>> column and only 1 facet for 2nd column) so it overrides the value of
>> nrow. In the case of 2 or 6 rows, the facets are well distributed in the
>> layout.
>>
>> The reason why I need 5 rows with 6 facets is that this facet plot is
>> part of a patchwork and I would like to have the same number of rows for
>> all facet plots of the patchwork (so that they all align well).
>>
>> Is there a way to force the number of rows in the facet_wrap()?
>>
>> Thank you in advance.
>> Best,
>> Ivan
>>
>> -- 
>>
>>
>> -- 
>> Dr. Ivan Calandra
>> TraCEr, laboratory for Traceology and Controlled Experiments
>> MONREPOS Archaeological Research Centre and
>> Museum for Human Behavioural Evolution
>> Schloss Monrepos
>> 56567 Neuwied, Germany
>> +49 (0) 2631 9772-243
>> https://www.researchgate.net/profile/Ivan_Calandra
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> 

Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Rui Barradas

Hello,

Sorry, there's an instruction missing. See inline.

Às 11:44 de 10/09/20, Rui Barradas escreveu:
If you want a function automating Karl's code, here it is. It returns an 
object of S3 class "htest", R's standard for hypothesis tests functions. 
The returned object can then be subset in the usual ways, ht$statistic, 
ht$parameter, ht$p.value, etc.



library(QSutils)

hutcheson.test <- function(x1, x2){
   dataname1 <- deparse(substitute(x1))
   dataname2 <- deparse(substitute(x2))
   method <- "Hutcheson's t-test for Shannon diversity equality"
   alternative <- "the diversities of the two samples are not equal"
   h1 <- Shannon(x1)
   varh1 <- ShannonVar(x1)
   n1 <- sum(x1)
   h2 <- Shannon(x2)
   varh2 <- ShannonVar(x2)
   n2 <- sum(x2)
   degfree <- (varh1 + varh2)**2 / (varh1**2/n1 + varh2**2/n2)
   tstat <- (h1 - h2)/sqrt(varh1 + varh2)
   p.value <- 2*pt(-abs(tstat), df = degfree)
   ht <- list(
     statistic = c(t = tstat),
     parameter = c(df = degfree),
     p.value = p.value,
     alternative = alternative,
     method = method,
     data.name = paste(dataname1, dataname2, sep = ", ")
   )
   class(ht) <- "htest"
   ht
}

earlier <- c(0,0,146,0,5,46,0,1,295,0,0,3,0,0,0,0,0)
later <- c(0,0,142,0,5,46,0,1,246,0,0,3,0,0,0,0,0)

hutcheson.test(earlier, later)



With the data you provided:


bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
bird_1959 <- c(0,0,14,59,26,68,0)
bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)

hutcheson.test(bird_1956, bird_1957)




Note that like David said earlier, there might be better ways to 
interpret Shannon's diversity index. If h is the sample's diversity, 
exp(h) gives the number of equally-common species with equivalent 
diversity.



s1 <- Shannon(earlier)
s2 <- Shannon(later)
c(earlier = s1, later = s2)
exp(c(earlier = s1, later = s2))   # Both round to 3
eq_common <- rep(1, 3) # Can be 1 specimen or any other number
Shannon(eq_common) # Slightly greater than the samples' 
diversity





# Create a list with all the data
birds <- mget(ls(pattern = "^bird"))


round(exp(sapply(birds, Shannon))) # Your data



Hope this helps,

Rui Barradas




#-


Earlier Karl wrote [1] that


Here var(h) is calculated as in ref 1 cited by Rui Barradas - I guess
that explains the minor numerical differences obtained with the code
above and the published variances.


I don't believe the published variances were computed with the published 
variance estimator. The code below computes the variances like QSutils 
and with formula (4) in Hutcheson's paper. The latter does not give the 
same results.


var_est <- function(n){
   s <- length(n)
   N <- sum(n)
   p <- n/N
   i <- p != 0
   inv.p <- numeric(s)
   inv.p[i] <- 1/p[i]
   log.p <- numeric(s)
   log.p[i] <- log(p[i])
   #
   term1 <- (sum(p * log.p^2) - sum(p * log.p)^2)/N
   term2 <- (s - 1)/(2*N^2)
   #
   numer3 <- -1 + sum(inv.p) - sum(inv.p * log.p) + sum(inv.p)*sum(p * 
log.p)

   denom3 <- 6*N^3
   term3 <- numer3/denom3
   list(
     Bioc = term1 + term2,
     Hutch = term1 + term2 + term3
   )
}

Vh1 <- var_est(earlier)
Vh1
all.equal(ShannonVar(earlier), Vh1$Bioc)
ShannonVar(earlier) - Vh1$Bioc    # FAQ 7.31

Vh2 <- var_est(later)
Vh2
identical(ShannonVar(later), Vh2$Bioc)    # TRUE



[1] https://stat.ethz.ch/pipermail/r-help/2020-September/468664.html


Hope this helps,

Rui Barradas


Às 09:38 de 10/09/20, Luigi Marongiu escreveu:

Update:
I can see that you used the function Shannon from the package QSutils.
This would supplement the iNext package I used and solve the problem.
Thank you.

On Thu, Sep 10, 2020 at 10:35 AM Luigi Marongiu
 wrote:


Thank you very much for the code, that was very helpful.
I got the article by Hutcheson -- I don't know if I can distribute it
, given the possible copyrights, or if I can attach it here -- but it
does not report numbers directly: it refers to a previous article
counting bird death on a telegraph each year. The numbers
are:
bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
bird_1959 <- c(0,0,14,59,26,68,0)
bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)

This for sake of the argument.
As for my problem, I implemented the Shannon index with the package
iNext, which only gives me the index itself and the 95% CI. Even when
I implemented it with vegan, I only got the index. Essentially I don't
have a count of species I could feed into the Hutcheson's. Is there a
way to extract these data? Or to run a Hutcheson's on the final index?
Thank you

On Tue, Sep 8, 2020 at 7:43 PM Karl Schilling
 wrote:


Dear Luigi,

below some code I cobbled together based on the Hutcheson paper you
mentioned. I was lucky to find code to calculate h and, importantly, 
its

variance in the R-package QSutils 

Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Rui Barradas
If you want a function automating Karl's code, here it is. It returns an 
object of S3 class "htest", R's standard for hypothesis tests functions. 
The returned object can then be subset in the usual ways, ht$statistic, 
ht$parameter, ht$p.value, etc.



library(QSutils)

hutcheson.test <- function(x1, x2){
  dataname1 <- deparse(substitute(x1))
  dataname2 <- deparse(substitute(x2))
  method <- "Hutcheson's t-test for Shannon diversity equality"
  alternative <- "the diversities of the two samples are not equal"
  h1 <- Shannon(x1)
  varh1 <- ShannonVar(x1)
  n1 <- sum(x1)
  h2 <- Shannon(x2)
  varh2 <- ShannonVar(x2)
  n2 <- sum(x2)
  degfree <- (varh1 + varh2)**2 / (varh1**2/n1 + varh2**2/n2)
  tstat <- (h1 - h2)/sqrt(varh1 + varh2)
  p.value <- 2*pt(-abs(tstat), df = degfree)
  ht <- list(
statistic = c(t = tstat),
parameter = c(df = degfree),
p.value = p.value,
alternative = alternative,
method = method,
data.name = paste(dataname1, dataname2, sep = ", ")
  )
  class(ht) <- "htest"
  ht
}

earlier <- c(0,0,146,0,5,46,0,1,295,0,0,3,0,0,0,0,0)
later <- c(0,0,142,0,5,46,0,1,246,0,0,3,0,0,0,0,0)

hutcheson.test(earlier, later)



With the data you provided:


bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
bird_1959 <- c(0,0,14,59,26,68,0)
bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)

hutcheson.test(bird_1956, bird_1957)




Note that like David said earlier, there might be better ways to 
interpret Shannon's diversity index. If h is the sample's diversity, 
exp(h) gives the number of equally-common species with equivalent 
diversity.



s1 <- Shannon(earlier)
s2 <- Shannon(later)
c(earlier = s1, later = s2)
exp(c(earlier = s1, later = s2))   # Both round to 3
eq_common <- rep(1, 3) # Can be 1 specimen or any other number
Shannon(eq_common) # Slightly greater than the samples' 
diversity



round(exp(sapply(birds, Shannon))) # Your data


#-


Earlier Karl wrote [1] that


Here var(h) is calculated as in ref 1 cited by Rui Barradas - I guess
that explains the minor numerical differences obtained with the code
above and the published variances.


I don't believe the published variances were computed with the published 
variance estimator. The code below computes the variances like QSutils 
and with formula (4) in Hutcheson's paper. The latter does not give the 
same results.


var_est <- function(n){
  s <- length(n)
  N <- sum(n)
  p <- n/N
  i <- p != 0
  inv.p <- numeric(s)
  inv.p[i] <- 1/p[i]
  log.p <- numeric(s)
  log.p[i] <- log(p[i])
  #
  term1 <- (sum(p * log.p^2) - sum(p * log.p)^2)/N
  term2 <- (s - 1)/(2*N^2)
  #
  numer3 <- -1 + sum(inv.p) - sum(inv.p * log.p) + sum(inv.p)*sum(p * 
log.p)

  denom3 <- 6*N^3
  term3 <- numer3/denom3
  list(
Bioc = term1 + term2,
Hutch = term1 + term2 + term3
  )
}

Vh1 <- var_est(earlier)
Vh1
all.equal(ShannonVar(earlier), Vh1$Bioc)
ShannonVar(earlier) - Vh1$Bioc# FAQ 7.31

Vh2 <- var_est(later)
Vh2
identical(ShannonVar(later), Vh2$Bioc)# TRUE



[1] https://stat.ethz.ch/pipermail/r-help/2020-September/468664.html


Hope this helps,

Rui Barradas


Às 09:38 de 10/09/20, Luigi Marongiu escreveu:

Update:
I can see that you used the function Shannon from the package QSutils.
This would supplement the iNext package I used and solve the problem.
Thank you.

On Thu, Sep 10, 2020 at 10:35 AM Luigi Marongiu
 wrote:


Thank you very much for the code, that was very helpful.
I got the article by Hutcheson -- I don't know if I can distribute it
, given the possible copyrights, or if I can attach it here -- but it
does not report numbers directly: it refers to a previous article
counting bird death on a telegraph each year. The numbers
are:
bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
bird_1959 <- c(0,0,14,59,26,68,0)
bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)

This for sake of the argument.
As for my problem, I implemented the Shannon index with the package
iNext, which only gives me the index itself and the 95% CI. Even when
I implemented it with vegan, I only got the index. Essentially I don't
have a count of species I could feed into the Hutcheson's. Is there a
way to extract these data? Or to run a Hutcheson's on the final index?
Thank you

On Tue, Sep 8, 2020 at 7:43 PM Karl Schilling
 wrote:


Dear Luigi,

below some code I cobbled together based on the Hutcheson paper you
mentioned. I was lucky to find code to calculate h and, importantly, its
variance in the R-package QSutils - you may find it on the Bioconductor
website.

here is the code, along with an example. I also attach the code as an
R-file.

Hope that helps.
All my best

Karl
PS don't forget to adjust for multiple testing if you compare more than
two groups.
K


# load 

Re: [R-es] SALDO EN RESUMEN DE CUENTA CONTABLE

2020-09-10 Thread Carlos J. Gil Bellosta
Hola, ¿qué tal?

Lo que quieres es algo así como

cumsum(debe) - cumsum(haber)

o una variante de lo anterior.

Un saludo,

Carlos J. Gil Bellosta
http://www.datanalyltics.com

El jue., 10 sept. 2020 a las 11:50, Jesus MARTIN F. ()
escribió:

> Tengo que preparar un Dataset que termine siendo un resumen de cuenta
> contable a partir de los datos del diario contable de una empresa que el
> script va filtrando cuenta por cuenta. He podido importar los datos con
> variables: fecha , concepto, valor debe, valor haber y me falta una
> variable que sea el saldo. Sucede que el saldo se tiene que calcular fila a
> fila, es decir sumando el valor debe y restando el valor haber al saldo de
> la fila anterior y no he logrado hacerlo. Podrían ayudarme ?
>
>Gracias
>
>Jesús
> --
> Jesus Martin Frade
> Papelera ANDORPEL
> (+54) (9) 11 4946 2131
> jesusmar...@andorpel.com
> jesusmar...@andorra.ad
>
> [[alternative HTML version deleted]]
>
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es
>

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] facet_wrap(nrow) ignored

2020-09-10 Thread Ulrik Stervbo via R-help

Dear Ivan,

I don't think it is possible to force a number of rows - but I'm 
honestly just guessing.


What you can do is to add an empty plot. Here I use cowplot, but 
gridExtra should also work well.


I add an indication of the row number for the plot to the initial 
data.frame, and loop over these.


In the first variant, I add an unused factor to the grp which creates an 
empty facet. I personally think this looks a little confusing, so in the 
second variant, I add a number of empty plots.


HTH
Ulrik

```
mydf <- data.frame(
  grp = rep(letters[1:6], each = 15),
  cat = rep(1:3, 30),
  var = rnorm(90),
  row_num = rep(c(1, 1, 2, 3, 4, 5), each = 15)
)

s_mydf <- split(mydf, mydf$row_num)

plots_mydf <- lapply(s_mydf, function(x){
  # Ensure no unused factors
  x$grp <- droplevels.factor(x$grp)
  if(length(unique(x$grp)) == 1){
x$grp <- factor(x$grp, levels = c(unique(x$grp), ""))
  }
  ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, drop=FALSE)
})

cowplot::plot_grid(plotlist = plots_mydf, nrow = 5)

# Maybe more elegant output
plots_mydf <- lapply(s_mydf, function(x, ncol = 2){
  # Ensure no unused factors
  x$grp <- droplevels.factor(x$grp)
  x <- split(x, x$grp)

  p <- lapply(x, function(x){
ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
  facet_wrap(~grp)
  })

  if(length(p) < ncol){
pe <- rep(list(ggplot() + theme_void()), ncol - length(p))
p <- c(p, pe)
  }
  cowplot::plot_grid(plotlist = p, ncol = ncol)
})

cowplot::plot_grid(plotlist = plots_mydf, ncol = 1)

# Or if you prefer not to split the plots on the same row
plots_mydf <- lapply(s_mydf, function(x, ncol = 2){

  p <- list(ggplot(data = x, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp))

  if(length(unique(x$grp)) < ncol){
pe <- rep(list(ggplot() + theme_void()), ncol - length(p))
p <- c(p, pe)
  }else{
ncol <- 1
  }
  cowplot::plot_grid(plotlist = p, ncol = ncol)
})

cowplot::plot_grid(plotlist = plots_mydf, ncol = 1)

```

On 2020-09-09 17:30, Ivan Calandra wrote:

Dear useRs,

I have an issue with the argument nrow of ggplot2::facet_wrap().

Let's consider some sample data:
mydf <- data.frame(grp = rep(letters[1:6], each = 15), cat = rep(1:3,
30), var = rnorm(90))

And let's try to plot with 5 rows:
library(ggplot2)
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 5)
It plots 2 rows and 3 columns rather than 5 rows and 2 columns as 
wanted.


These plots are as expected:
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 2)
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 6)

My guess is that 5 rows is not ideal for 6 facets (5 facets in 1st
column and only 1 facet for 2nd column) so it overrides the value of
nrow. In the case of 2 or 6 rows, the facets are well distributed in 
the

layout.

The reason why I need 5 rows with 6 facets is that this facet plot is
part of a patchwork and I would like to have the same number of rows 
for

all facet plots of the patchwork (so that they all align well).

Is there a way to force the number of rows in the facet_wrap()?

Thank you in advance.
Best,
Ivan

--


--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R-es] SALDO EN RESUMEN DE CUENTA CONTABLE

2020-09-10 Thread Jesus MARTIN F.
Tengo que preparar un Dataset que termine siendo un resumen de cuenta
contable a partir de los datos del diario contable de una empresa que el
script va filtrando cuenta por cuenta. He podido importar los datos con
variables: fecha , concepto, valor debe, valor haber y me falta una
variable que sea el saldo. Sucede que el saldo se tiene que calcular fila a
fila, es decir sumando el valor debe y restando el valor haber al saldo de
la fila anterior y no he logrado hacerlo. Podrían ayudarme ?

   Gracias

   Jesús
-- 
Jesus Martin Frade
Papelera ANDORPEL
(+54) (9) 11 4946 2131
jesusmar...@andorpel.com
jesusmar...@andorra.ad

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] aplicar codigo

2020-09-10 Thread Marcelino de la Cruz Rot

Hola:
Como dice Carlos, algo así, por ejemplo:

transforma <- function(df) sapply(df, function(x) 
ifelse(x%in%c("x1","x2","x3"), 
"prueba1",ifelse(x%in%c("x4","x5","x6"),"prueba2",x)))



> transforma(df1)
  col1
 [1,] "prueba1"
 [2,] "prueba1"
 [3,] "x11"
 [4,] "prueba1"
 [5,] "x33"
 [6,] "prueba1"
 [7,] "prueba2"
 [8,] "prueba2"
 [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"


> transforma(df2)
 col2
[1,] "x12"
[2,] "prueba2"
[3,] "prueba2"
[4,] "x771"
[5,] "prueba2"
[6,] "prueba1"


> transforma(df3)
  col3
 [1,] "x7"
 [2,] "prueba1"
 [3,] "prueba2"
 [4,] "prueba2"
 [5,] "x111"
 [6,] "prueba1"
 [7,] "prueba2"
 [8,] "prueba2"
 [9,] "x35"
[10,] "prueba1"
[11,] "prueba1"
[12,] "prueba2"
[13,] "prueba2"
[14,] "prueba2"

Saludos,

Marcelino

El 10/09/2020 a las 9:41, Carlos Ortega escribió:

Hola,
Si lo estoy entendiendo bien, lo que tienes que crearte es una función.
A la función le pasarías unos parámetros (las columnas y el dataframe al
que añadir la transformación), y en el cuerpo de la función haces esos
cálculos.

Gracias,
Carlos.



Gracias,
Carlos.

El jue., 10 sept. 2020 a las 3:55, Samura . ()
escribió:


Hola,
me gustaría hacer algo como en el siguiente ejemplo

A un df añadirle una columna que es la transformación de otra,
en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
todo lo que sea x4,x5,x6 lo llamo prueba 2
el resto de x las dejo como están.

Sería algo así

col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
'x1','x2', 'x4')
df1<-data.frame(col1)
attach(df1)

df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
"prueba1",
ifelse(col1 == "x4"|col1 == "x5"| col1 == "x6",
"prueba2", col1))

detach(df1)
df1

pero ahora en vez de un df tengo varios

col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2',
'x4','x6', 'x5')
df2<-data.frame(col2)
df3<-data.frame(col3)

¿cómo puedo aplicar el código al resto de los df sin tener que repetirlo?



 [[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es





--
Marcelino de la Cruz Rot
Depto. de Biología y Geología
Física y Química Inorgánica
Universidad Rey Juan Carlos
Móstoles España

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Luigi Marongiu
Update:
I can see that you used the function Shannon from the package QSutils.
This would supplement the iNext package I used and solve the problem.
Thank you.

On Thu, Sep 10, 2020 at 10:35 AM Luigi Marongiu
 wrote:
>
> Thank you very much for the code, that was very helpful.
> I got the article by Hutcheson -- I don't know if I can distribute it
> , given the possible copyrights, or if I can attach it here -- but it
> does not report numbers directly: it refers to a previous article
> counting bird death on a telegraph each year. The numbers
> are:
> bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
> bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
> bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
> bird_1959 <- c(0,0,14,59,26,68,0)
> bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)
>
> This for sake of the argument.
> As for my problem, I implemented the Shannon index with the package
> iNext, which only gives me the index itself and the 95% CI. Even when
> I implemented it with vegan, I only got the index. Essentially I don't
> have a count of species I could feed into the Hutcheson's. Is there a
> way to extract these data? Or to run a Hutcheson's on the final index?
> Thank you
>
> On Tue, Sep 8, 2020 at 7:43 PM Karl Schilling
>  wrote:
> >
> > Dear Luigi,
> >
> > below some code I cobbled together based on the Hutcheson paper you
> > mentioned. I was lucky to find code to calculate h and, importantly, its
> > variance in the R-package QSutils - you may find it on the Bioconductor
> > website.
> >
> > here is the code, along with an example. I also attach the code as an
> > R-file.
> >
> > Hope that helps.
> > All my best
> >
> > Karl
> > PS don't forget to adjust for multiple testing if you compare more than
> > two groups.
> > K
> >
> >
> > # load package needed
> > # QSutils is on Bioconductor
> > library(QSutils)
> >
> > # here some exemplary data - these are the data from Pilou 1966 that are
> > used
> > # in the second example of Hutcheson, J theor Biol 129:151-154 (1970)
> >
> > earlier <- c(0,0,146,0,5,46,0,1,295,0,0,3,0,0,0,0,0)
> > later <- c(0,0,142,0,5,46,0,1,246,0,0,3,0,0,0,0,0)
> > # numbers of the first example used by Hutcheson were unfortunately not
> > # available to me
> >
> > # here starts the code ; you may replace the variables "earlier" and "later"
> > # by your own numbers.
> >
> > # calculate h, var(h) etc
> > h1 <- Shannon(earlier)
> > varh1 <- ShannonVar(earlier)
> > n1 <- sum (earlier)
> > h2 <- Shannon(later)
> > varh2 <- ShannonVar(later)
> > n2 <- sum (later)
> > degfree <- (varh1 + varh2)**2 /(varh1**2/n1 + varh2**2 /n2)
> >
> > # compare numbers with those in the paper
> > h1
> > h2
> > varh1
> > varh2
> > # I assume that minor numerical differences are due to differences in the
> > # numerical precision of computers in the early seventies and today / KS
> >
> > # this is the actual t-test
> > t <- (h1-h2) /sqrt(varh1 + varh2)
> > p <- 2*pt(-abs(t),df= degfree)
> > p
> >
> > # that's it
> > # Best
> > # Karl
> > --
> > Karl Schilling, MD
> > Professor of Anatomy and Cell Biology
> > Anatomisches Institut
> > Rheinische Friedrich-Wilhelms-Universität
> > Nussallee 10
> >
> > D-53115 Bonn
> > Germany
> >
> > phone ++49-228-73-2602
> >
>
>
> --
> Best regards,
> Luigi



-- 
Best regards,
Luigi

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Some code to run Hutcheson t-test using R

2020-09-10 Thread Luigi Marongiu
Thank you very much for the code, that was very helpful.
I got the article by Hutcheson -- I don't know if I can distribute it
, given the possible copyrights, or if I can attach it here -- but it
does not report numbers directly: it refers to a previous article
counting bird death on a telegraph each year. The numbers
are:
bird_1956 <- c(4,4,190,135,56,3,2,2,1,12,41,201,1,0,131,3)
bird_1957 <- c(4,111,53,66,146,222,126,61,0,2323,21)
bird_1958 <- c(0,3,32,228,56,102,0,11,2,220,0)
bird_1959 <- c(0,0,14,59,26,68,0)
bird_1960 <- c(0,0,73,66,71,25,0,109,63,1)

This for sake of the argument.
As for my problem, I implemented the Shannon index with the package
iNext, which only gives me the index itself and the 95% CI. Even when
I implemented it with vegan, I only got the index. Essentially I don't
have a count of species I could feed into the Hutcheson's. Is there a
way to extract these data? Or to run a Hutcheson's on the final index?
Thank you

On Tue, Sep 8, 2020 at 7:43 PM Karl Schilling
 wrote:
>
> Dear Luigi,
>
> below some code I cobbled together based on the Hutcheson paper you
> mentioned. I was lucky to find code to calculate h and, importantly, its
> variance in the R-package QSutils - you may find it on the Bioconductor
> website.
>
> here is the code, along with an example. I also attach the code as an
> R-file.
>
> Hope that helps.
> All my best
>
> Karl
> PS don't forget to adjust for multiple testing if you compare more than
> two groups.
> K
>
>
> # load package needed
> # QSutils is on Bioconductor
> library(QSutils)
>
> # here some exemplary data - these are the data from Pilou 1966 that are
> used
> # in the second example of Hutcheson, J theor Biol 129:151-154 (1970)
>
> earlier <- c(0,0,146,0,5,46,0,1,295,0,0,3,0,0,0,0,0)
> later <- c(0,0,142,0,5,46,0,1,246,0,0,3,0,0,0,0,0)
> # numbers of the first example used by Hutcheson were unfortunately not
> # available to me
>
> # here starts the code ; you may replace the variables "earlier" and "later"
> # by your own numbers.
>
> # calculate h, var(h) etc
> h1 <- Shannon(earlier)
> varh1 <- ShannonVar(earlier)
> n1 <- sum (earlier)
> h2 <- Shannon(later)
> varh2 <- ShannonVar(later)
> n2 <- sum (later)
> degfree <- (varh1 + varh2)**2 /(varh1**2/n1 + varh2**2 /n2)
>
> # compare numbers with those in the paper
> h1
> h2
> varh1
> varh2
> # I assume that minor numerical differences are due to differences in the
> # numerical precision of computers in the early seventies and today / KS
>
> # this is the actual t-test
> t <- (h1-h2) /sqrt(varh1 + varh2)
> p <- 2*pt(-abs(t),df= degfree)
> p
>
> # that's it
> # Best
> # Karl
> --
> Karl Schilling, MD
> Professor of Anatomy and Cell Biology
> Anatomisches Institut
> Rheinische Friedrich-Wilhelms-Universität
> Nussallee 10
>
> D-53115 Bonn
> Germany
>
> phone ++49-228-73-2602
>


-- 
Best regards,
Luigi

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R-es] aplicar codigo

2020-09-10 Thread Carlos Ortega
Hola,
Si lo estoy entendiendo bien, lo que tienes que crearte es una función.
A la función le pasarías unos parámetros (las columnas y el dataframe al
que añadir la transformación), y en el cuerpo de la función haces esos
cálculos.

Gracias,
Carlos.



Gracias,
Carlos.

El jue., 10 sept. 2020 a las 3:55, Samura . ()
escribió:

> Hola,
> me gustaría hacer algo como en el siguiente ejemplo
>
> A un df añadirle una columna que es la transformación de otra,
> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
> todo lo que sea x4,x5,x6 lo llamo prueba 2
> el resto de x las dejo como están.
>
> Sería algo así
>
> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35',
> 'x1','x2', 'x4')
> df1<-data.frame(col1)
> attach(df1)
>
> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3",
> "prueba1",
>ifelse(col1 == "x4"|col1 == "x5"| col1 == "x6",
> "prueba2", col1))
>
> detach(df1)
> df1
>
> pero ahora en vez de un df tengo varios
>
> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2',
> 'x4','x6', 'x5')
> df2<-data.frame(col2)
> df3<-data.frame(col3)
>
> ¿cómo puedo aplicar el código al resto de los df sin tener que repetirlo?
>
>
>
> [[alternative HTML version deleted]]
>
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es
>


-- 
Saludos,
Carlos Ortega
www.qualityexcellence.es

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] aplicar codigo

2020-09-10 Thread Emilio L. Cano
Buenos días,

Yo usaría dplyr::recode, mira los ejemplos aquí:
https://dplyr.tidyverse.org/reference/recode.html 


Un saludo,
Emilio

> El 10 sept 2020, a las 3:55, Samura .  escribió:
> 
> Hola,
> me gustar�a hacer algo como en el siguiente ejemplo
> 
> A un df a�adirle una columna que es la transformaci�n de otra,
> en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
> todo lo que sea x4,x5,x6 lo llamo prueba 2
> el resto de x las dejo como est�n.
> 
> Ser�a algo as�
> 
> col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35', 'x1','x2', 
> 'x4')
> df1<-data.frame(col1)
> attach(df1)
> 
> df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3", "prueba1",
>   ifelse(col1 == "x4"|col1 == "x5"| col1 == "x6", 
> "prueba2", col1))
> 
> detach(df1)
> df1
> 
> pero ahora en vez de un df tengo varios
> 
> col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
> col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2', 
> 'x4','x6', 'x5')
> df2<-data.frame(col2)
> df3<-data.frame(col3)
> 
> �c�mo puedo aplicar el c�digo al resto de los df sin tener que repetirlo?
> 
> 
> 
>   [[alternative HTML version deleted]]
> 
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es