[R] Show all elements

Leandro Marino leandromarino at leandromarino.com.br
Mon Mar 9 14:47:56 CET 2015


Hi,

Look to the following code:

set.seed(1)
dados =
data.frame(valor=rnorm(100),var=sample(LETTERS[c(1,2,3,5)],100,replace=T),peso=rpois(100,2))
dados[1:10,]
dados$var <- factor(dados$var,levels=LETTERS[1:5])
table(dados$var)
 A  B  C  D  E
31 31 19  0 19

When I try to use summarize, Hmisc package it shows me the result without D
category.

g1 <- function(y) wtd.mean(y[,1],y[,2])
summarize(dados[,c(1,3)], llist(var=dados$var), g1,stat.name = 'med')
  var         med
1   A  0.02589377
2   B  0.37123239
3   C -0.57820359
4   E  0.39584514

How do I get med = NA or something else with summarize?

I realy need to the function to return all factors in the var even it they
are an empty set.

thanks in advance.

leandro

	[[alternative HTML version deleted]]



More information about the R-help mailing list