[R] Creating a table
Benilton Carvalho
bcarvalh at jhsph.edu
Tue Nov 14 21:52:44 CET 2006
tb = table(df$loc, cut(df$year, seq(1970, 1985, by=5), right=F))
rs = rowSums(tb)
tb = cbind(tb, rs)
cs = colSums(tb)
tb = rbind(tb, cs)
cheers,
b
On Nov 14, 2006, at 3:20 PM, Michael Graber wrote:
> Dear R List,
>
> I am a new to R, so my question may be easy to answer for you:
>
> I have a dataframe, for example:
>
> df<-data.frame(loc=c("A","B","A","A","A"),
> year=as.numeric(c("1970","1970","1970","1976","1980")))
>
> and I want to create the following table without using loops:
>
> 1970-74 ; 1975-79 ; 1980-85; rowsum
> A 2 1 1 4
> B 1 0 0 1
> colsum 3 1 1 5
>
> so that the frequencies of df$loc are shown in the table for different
> time intervals.
>
> Thanks in advance for any hint,
>
> Michael Graber
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
More information about the R-help
mailing list