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
addmargins(table(df)) On 14/11/06, Michael Graber <michael_graber at gmx.de> 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Michael
One solution:
df<-data.frame(loc=c("A","B","A","A","A"),
year=c(1970,1970,1970,1976,1980))
df[,3] <- cut(df$year, c(1969.5,1974.5,1979.5,1984.5),
c('1970-74','1975-79','1980-85'))
with(df, addmargins(table(loc, V3)))
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Michael Graber
> Sent: Wednesday, 15 November 2006 9:21 a.m.
> To: R-Mailingliste
> Subject: [R] Creating a table
>
> 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.
>
______________________________________________________
The contents of this e-mail are privileged and/or confidenti...{{dropped}}
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.