Hola Jesús,

Usando la función by, prueba con

by(datos$Gain, datos$Diet, mean, na.rm=T)

para obviar los NA y con

by(datos$Gain, datos$Diet, function(x) sum(is.na(x)))

para contar los missings en cada categoría.

Puedes sustituir la función by también por la función tapply.

Klaus.


On 28/10/2015 9:07, Jesús Para Fernández wrote:



Me gusta la respuesta uqe has dado, pero si por ejemplo, alguno de los datos 
tiene datos faltantes, entonces devuelve NA.

He probado con:
sapply(split(datos$uno, as.factor(datos$dos)), mean(na.rm=TRUE))

pero da fallo.

¿Cómo se podría hacer para que devolviera además la media obviando los NA y que 
contara el numero de NA por categoria?

Date: Wed, 28 Oct 2015 00:13:45 +0100
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [R-es] pregunta

Otras variantes con y sin paquetes adicionales...

sapply(split(datIn$Gain, as.factor(datIn$Diet)), mean)
  d1  d2  d3
280 278 312
by(datIn$Gain, datIn$Diet, mean)
datIn$Diet: d1
[1] 280
--------------------------------------------------------------
datIn$Diet: d2
[1] 278
--------------------------------------------------------------
datIn$Diet: d3
[1] 312
library(dplyr)
summarise(group_by(datIn, Diet), mean(Gain))
Source: local data frame [3 x 2]

    Diet mean(Gain)
   (chr)      (dbl)
1    d1        280
2    d2        278
3    d3        312
library(sqldf)
sqldf("select Diet,avg(Gain) from datIn group by Diet")
   Diet avg(Gain)
1   d1       280
2   d2       278
3   d3       312


Saludos,
Carlos Ortega
www.qualityexcellence.es

2015-10-27 22:45 GMT+01:00 eric <[email protected]>:

tambien te sirve la funcion data.table ... si no tienes instalado el
paquete:

install.packages("data.table")
library(data.table)
jbe <- as.data.table(read.table("tusdatos.txt"))
jbe.ave <- jbe[, .("ave"=mean(Gain)), by=.(Diet)]

jbe.ave
    Diet ave
1:   d1 280
2:   d2 278
3:   d3 312


Saludos.

Eric.


On 10/27/2015 05:16 PM, jbetancourt wrote:

Estimados

Cuando existia epicalc,  hab�a una  manera muy f�cil de determinar la
media de una variable (en esta caso Gain)  por grupos, en este caso (Diet).
?Como se puede hacer ahora?

        Diet Gain
1    d1  270
2    d1  300
3    d1  280
4    d1  280
5    d1  270
6    d2  290
7    d2  250
8    d2  280
9    d2  290
10   d2  280
11   d3  290
12   d3  340
13   d3  330
14   d3  300
15   d3  300

Saludos
Jos�

         [[alternative HTML version deleted]]



_______________________________________________
R-help-es mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es


--
Forest Engineer
Master in Environmental and Natural Resource Economics
Ph.D. student in Sciences of Natural Resources at La Frontera University
Member in AguaDeTemu2030, citizen movement for Temuco with green city
standards for living

Nota: Las tildes se han omitido para asegurar compatibilidad con algunos
lectores de correo.


_______________________________________________
R-help-es mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es



--
Saludos,
Carlos Ortega
www.qualityexcellence.es

        [[alternative HTML version deleted]]

_______________________________________________
R-help-es mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es
                                                                                
        [[alternative HTML version deleted]]

_______________________________________________
R-help-es mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es

_______________________________________________
R-help-es mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es

Responder a