[R] the format of the result

ronggui 0034058 at fudan.edu.cn
Fri Jul 1 13:40:44 CEST 2005


I write a function to get the frequency and prop of a variable.

freq<-function(x,digits=3)
{naa<-is.na(x)
nas<-sum(naa)
if (any(naa))
x<-x[!naa]
n<-length(x)
ta<-table(x)
prop<-prop.table(ta)*100
res<-rbind(ta,prop)
rownames(res)<-c("Freq","Prop")
cat("Missing value(s) are",nas,".\n")
cat("Valid case(s) are",n,".\n")
cat("Total case(s) are",(n+nas),".\n\n")
print(res,digits=(digits+2))
cat("\n")
}

> freq(sample(letters[1:3],48,T),2)
Missing value(s) are 0 .
Valid case(s) are 48 .
Total case(s) are 48 .

         a     b     c
Freq 11.00 20.00 17.00
Prop 22.92 41.67 35.42

and i want the result to be like
         a      b     c
Freq 11.00  20.00  17.00
Prop 22.92% 41.67% 35.42%

how should i change my function to get what i want?
-- 
Department of Sociology
Fudan University,Shanghai
Blog:http://sociology.yculblog.com




More information about the R-help mailing list