[R-br] trabalhar com médias
Edson Lira
edinhoestat em yahoo.com.br
Terça Julho 2 09:33:24 BRT 2013
Allan, um CMR é o que nossos amigos precisam para te ajudar.
Poderia postar o teu banco em um site tipo dropbox, datafile host e enviar o link no email para consulta.
De qualquer forma, talvez você possa usar a rotina abaixo:Examples
#AGGREGATE
No R
?aggregate
## Compute the averages for the variables in 'state.x77', grouped
## according to the region (Northeast, South, North Central, West) that
## each state belongs to.
aggregate(state.x77, list(Region = state.region), mean) ## Compute the averages according to region and the occurrence of more
## than 130 days of frost.
aggregate(state.x77, list(Region = state.region, Cold = state.x77[,"Frost"] > 130), mean)
## (Note that no state in 'South' is THAT cold.) ## example with character variables and NAs
testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9), v2 = c(11,33,55,77,88,33,55,NA,44,55,77,99) )
by1 <- c("red","blue",1,2,NA,"big",1,2,"red",1,NA,12)
by2 <- c("wet","dry",99,95,NA,"damp",95,99,"red",99,NA,NA)
aggregate(x = testDF, by = list(by1, by2), FUN = "mean") # and if you want to treat NAs as a group
fby1 <- factor(by1, exclude = "")
fby2 <- factor(by2, exclude = "")
aggregate(x = testDF, by = list(fby1, fby2), FUN = "mean") ## Formulas, one ~ one, one ~ many, many ~ one, and many ~ many:
aggregate(weight ~ feed, data = chickwts, mean)
aggregate(breaks ~ wool + tension, data = warpbreaks, mean)
aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean)
aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum) ## Dot notation:
aggregate(. ~ Species, data = iris, mean)
aggregate(len ~ ., data = ToothGrowth, mean) ## Often followed by xtabs():
ag <- aggregate(len ~ ., data = ToothGrowth, mean)
xtabs(len ~ ., data = ag) ## Compute the average annual approval ratings for American presidents.
aggregate(presidents, nfrequency = 1, FUN = mean)
## Give the summer less weight.
aggregate(presidents, nfrequency = 1, FUN = weighted.mean, w = c(1, 1, 0.5, 1)) Outra rotina:
No R
?apply
#APPLY
## Compute row and column sums for a matrix:
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
dimnames(x)[[1]] <- letters[1:8]
apply(x, 2, mean, trim = .2)
col.sums <- apply(x, 2, sum)
row.sums <- apply(x, 1, sum)
rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums))) stopifnot( apply(x, 2, is.vector)) ## Sort the columns of a matrix
apply(x, 2, sort) ##- function with extra args:
cave <- function(x, c1, c2) c(mean(x[c1]), mean(x[c2]))
apply(x,1, cave, c1="x1", c2=c("x1","x2")) ma <- matrix(c(1:4, 1, 6:8), nrow = 2)
ma
apply(ma, 1, table) #--> a list of length 2
apply(ma, 1, stats::quantile)# 5 x n matrix with rownames stopifnot(dim(ma) == dim(apply(ma, 1:2, sum))) ## Example with different lengths for each call
z <- array(1:24, dim=2:4)
zseq <- apply(z, 1:2, function(x) seq_len(max(x)))
zseq ## a 2 x 3 matrix
typeof(zseq) ## list
dim(zseq) ## 2 3
zseq[1,]
apply(z, 3, function(x) seq_len(max(x)))
# a list without a dim attribute
Edson Lira
Estatístico
Manaus-Amazonas
________________________________
De: "alanarocha em sapo.pt" <alanarocha em sapo.pt>
Para: r-br <r-br em listas.c3sl.ufpr.br>
Enviadas: Terça-feira, 2 de Julho de 2013 7:41
Assunto: [R-br] trabalhar com médias
Olá,
eu tenho estes dados de 4 grupos:
A. pthi <150 B. pthi 150-300 C. pthi 300-600 D. >600
112 277 317 1153
126 208 454 1199
210 475 2794
257 444 2081
246 493 1576
264 599 901
589 871
583 722
599 1110
548 1412
454 732
382 601
533 1130
335 642
598 682
448 1035
355 1203
462 1136
500 1109
354 805
369 912
547 1454
842
2384
760
675
810
1215
1565
1585
942
827
1015
631
647
709
2192
1392
1028
1201
892
1287
1579
1071
669
1103
964
926
1501
979
1178
625
678
1054
855
983
1076
876
que codigos eu posso fazer no R para responder a estas perguntas:
7. Obter a Dose média em cada grupo e o gráfico da sua evolução ao longo do tempo.
Ana
_______________________________________________
R-br mailing list
R-br em listas.c3sl.ufpr.br
https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br
Leia o guia de postagem (http://www.leg.ufpr.br/r-br-guia) e forneça código mínimo reproduzível.
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20130702/97771208/attachment.html>
Mais detalhes sobre a lista de discussão R-br