I have a file like this:
a 0.1
a 0.2
a 0.9
b 0.5
b 0.9
b 0.7
c 0.6
c 0.99
c 0.88
Which I would like to get to be the following matrix:
0.1 0.2 0.3 0.4 ...
a 1 2 0 0
b 0 0 0 0
..
I.e: each place in the matrix denotes how many entries in each
category that are betwee 0.0 and 0.1, 0.1 and 0.2 and so on.
The way I was thinking of doing it was by constructing an empty matrix
and then doing a for loop testing each element and incrementing in the
matrix as appropriate. However, it struck me that this has to be
easier to do than that. Am I right?
Karin
--
Karin Lagesen, PhD student
karin.lagesen at medisin.uio.no
http://www.cmbn.no/rognes/
you could use something like this (but maybe there are better
proposals):
dat <- data.frame(g = rep(letters[1:3], each = 5), val = runif(15))
out <- do.call(rbind, lapply(split(dat$val, dat$g), function(x){
f <- factor(findInterval(x, vec = seq(0, 1, 0.1)), levels = 1:10)
table(f)
}))
colnames(out) <- seq(0.1, 1, 0.1)
out
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Karin Lagesen" <karin.lagesen at medisin.uio.no>
To: <r-help at r-project.org>
Sent: Thursday, September 29, 2005 3:16 PM
Subject: [R] reshaping data?
>
> I have a file like this:
>
>
> a 0.1
> a 0.2
> a 0.9
> b 0.5
> b 0.9
> b 0.7
> c 0.6
> c 0.99
> c 0.88
>
> Which I would like to get to be the following matrix:
>
> 0.1 0.2 0.3 0.4 ...
> a 1 2 0 0
> b 0 0 0 0
> ..
>
> I.e: each place in the matrix denotes how many entries in each
> category that are betwee 0.0 and 0.1, 0.1 and 0.2 and so on.
>
> The way I was thinking of doing it was by constructing an empty
> matrix
> and then doing a for loop testing each element and incrementing in
> the
> matrix as appropriate. However, it struck me that this has to be
> easier to do than that. Am I right?
>
> Karin
> --
> Karin Lagesen, PhD student
> karin.lagesen at medisin.uio.no
> http://www.cmbn.no/rognes/
>
> ______________________________________________
> 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
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
On Thu, 29 Sep 2005, Karin Lagesen wrote:> > I have a file like this: > > > a 0.1 > a 0.2 > a 0.9 > b 0.5 > b 0.9 > b 0.7 > c 0.6 > c 0.99 > c 0.88 > > Which I would like to get to be the following matrix: > > 0.1 0.2 0.3 0.4 ... > a 1 2 0 0 > b 0 0 0 0 > .. > > I.e: each place in the matrix denotes how many entries in each > category that are betwee 0.0 and 0.1, 0.1 and 0.2 and so on. >If your variables are called, say, id and value, and value is between 0 and 1 then table(id,cut(value,breaks=(0:10)/10)) works. -thomas
This can be shortened slightly using cut: table(data.frame(g = dat$g, val = cut(dat$val, 0:10/10, lab = 1:10/10))) On 9/29/05, Dimitris Rizopoulos <dimitris.rizopoulos at med.kuleuven.be> wrote:> you could use something like this (but maybe there are better > proposals): > > dat <- data.frame(g = rep(letters[1:3], each = 5), val = runif(15)) > > out <- do.call(rbind, lapply(split(dat$val, dat$g), function(x){ > f <- factor(findInterval(x, vec = seq(0, 1, 0.1)), levels = 1:10) > table(f) > })) > colnames(out) <- seq(0.1, 1, 0.1) > out > > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://www.med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Karin Lagesen" <karin.lagesen at medisin.uio.no> > To: <r-help at r-project.org> > Sent: Thursday, September 29, 2005 3:16 PM > Subject: [R] reshaping data? > > > > > > I have a file like this: > > > > > > a 0.1 > > a 0.2 > > a 0.9 > > b 0.5 > > b 0.9 > > b 0.7 > > c 0.6 > > c 0.99 > > c 0.88 > > > > Which I would like to get to be the following matrix: > > > > 0.1 0.2 0.3 0.4 ... > > a 1 2 0 0 > > b 0 0 0 0 > > .. > > > > I.e: each place in the matrix denotes how many entries in each > > category that are betwee 0.0 and 0.1, 0.1 and 0.2 and so on. > > > > The way I was thinking of doing it was by constructing an empty > > matrix > > and then doing a for loop testing each element and incrementing in > > the > > matrix as appropriate. However, it struck me that this has to be > > easier to do than that. Am I right? > > > > Karin > > -- > > Karin Lagesen, PhD student > > karin.lagesen at medisin.uio.no > > http://www.cmbn.no/rognes/ > > > > ______________________________________________ > > 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 > > > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > > ______________________________________________ > 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 slightly more to: table(g = dat$g, val = cut(dat$val, 0:10/10, lab = 1:10/10)) On 9/29/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> This can be shortened slightly using cut: > > table(data.frame(g = dat$g, val = cut(dat$val, 0:10/10, lab = 1:10/10))) > > On 9/29/05, Dimitris Rizopoulos <dimitris.rizopoulos at med.kuleuven.be> wrote: > > you could use something like this (but maybe there are better > > proposals): > > > > dat <- data.frame(g = rep(letters[1:3], each = 5), val = runif(15)) > > > > out <- do.call(rbind, lapply(split(dat$val, dat$g), function(x){ > > f <- factor(findInterval(x, vec = seq(0, 1, 0.1)), levels = 1:10) > > table(f) > > })) > > colnames(out) <- seq(0.1, 1, 0.1) > > out > > > > > > I hope it helps. > > > > Best, > > Dimitris > > > > ---- > > Dimitris Rizopoulos > > Ph.D. Student > > Biostatistical Centre > > School of Public Health > > Catholic University of Leuven > > > > Address: Kapucijnenvoer 35, Leuven, Belgium > > Tel: +32/(0)16/336899 > > Fax: +32/(0)16/337015 > > Web: http://www.med.kuleuven.be/biostat/ > > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > > > > ----- Original Message ----- > > From: "Karin Lagesen" <karin.lagesen at medisin.uio.no> > > To: <r-help at r-project.org> > > Sent: Thursday, September 29, 2005 3:16 PM > > Subject: [R] reshaping data? > > > > > > > > > > I have a file like this: > > > > > > > > > a 0.1 > > > a 0.2 > > > a 0.9 > > > b 0.5 > > > b 0.9 > > > b 0.7 > > > c 0.6 > > > c 0.99 > > > c 0.88 > > > > > > Which I would like to get to be the following matrix: > > > > > > 0.1 0.2 0.3 0.4 ... > > > a 1 2 0 0 > > > b 0 0 0 0 > > > .. > > > > > > I.e: each place in the matrix denotes how many entries in each > > > category that are betwee 0.0 and 0.1, 0.1 and 0.2 and so on. > > > > > > The way I was thinking of doing it was by constructing an empty > > > matrix > > > and then doing a for loop testing each element and incrementing in > > > the > > > matrix as appropriate. However, it struck me that this has to be > > > easier to do than that. Am I right? > > > > > > Karin > > > -- > > > Karin Lagesen, PhD student > > > karin.lagesen at medisin.uio.no > > > http://www.cmbn.no/rognes/ > > > > > > ______________________________________________ > > > 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 > > > > > > > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm > > > > ______________________________________________ > > 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 > > >
Karin Lagesen wrote:> I have a file like this: > > > a 0.1 > a 0.2 > a 0.9 > b 0.5 > b 0.9 > b 0.7 > c 0.6 > c 0.99 > c 0.88 > > Which I would like to get to be the following matrix: > > 0.1 0.2 0.3 0.4 ... > a 1 2 0 0 > b 0 0 0 0 > .. >If you have a file named KL.dat with the above data in it: KL.df<-read.table("KL.dat",header=FALSE) KL.mat<-as.matrix(table(KL.df)) Jim