I am doing this a kinda dumb way, and it is apparetnly taking forever. I have a data frame with two numeric columns. I want to look at their correlation, and I am looking at the size ratio between the two. i.e. plot(density(data$V1/data$V2)) This kinda gives me a normal curve showing something about the distribution of the two values. I want to make sure that V1/V2 is always > 1 ... for (i in 1:length(row.names(data)) ){ ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 } This is a bit of a hack, and is taking forever for some reson (about 40,000 rows in my data.frame). I would just like to swap the values in the data frame (to put the bigest first for example), then use the above plot to get what I want. Should I use the DB backend to the data to make the dump in this way? (involves some hacky sql) Considering I am interested in the range of the ratio between V1 and V2, should I be looking at doing a different analysis? I am so dumb, any help is appreciated, Dan.
Dimitris Rizopoulos
2004-Sep-16 12:34 UTC
[R] Conditionally swap two columns of a data.frame?
Hi Dan, do you need something like that, dat <- data.frame(V1=rnorm(40000, 10), V2=rnorm(40000, 10)) ratioV1V2 <- ifelse(dat$V1>dat$V2, dat$V1/dat$V2, dat$V2/dat$V1) 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/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Thursday, September 16, 2004 2:19 PM Subject: [R] Conditionally swap two columns of a data.frame?> > I am doing this a kinda dumb way, and it is apparetnly taking > forever. > > I have a data frame with two numeric columns. I want to look attheir> correlation, and I am looking at the size ratio between the two. > > i.e. > > plot(density(data$V1/data$V2)) > > This kinda gives me a normal curve showing something about the > distribution of the two values. > > I want to make sure that V1/V2 is always > 1 ... > > for (i in 1:length(row.names(data)) ){ > ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 > } > > This is a bit of a hack, and is taking forever for some reson (about > 40,000 rows in my data.frame). > > I would just like to swap the values in the data frame (to put thebigest> first for example), then use the above plot to get what I want. > > Should I use the DB backend to the data to make the dump in thisway?> (involves some hacky sql) > > Considering I am interested in the range of the ratio between V1 andV2,> should I be looking at doing a different analysis? > > I am so dumb, any help is appreciated, > > Dan. > > ______________________________________________ > 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>
Prof Brian Ripley
2004-Sep-16 12:34 UTC
[R] Conditionally swap two columns of a data.frame?
On Thu, 16 Sep 2004, Dan Bolser wrote:> > I am doing this a kinda dumb way, and it is apparetnly taking > forever. > > I have a data frame with two numeric columns. I want to look at their > correlation, and I am looking at the size ratio between the two. > > i.e. > > plot(density(data$V1/data$V2)) > > This kinda gives me a normal curve showing something about the > distribution of the two values. > > I want to make sure that V1/V2 is always > 1 ... > > for (i in 1:length(row.names(data)) ){ > ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 > }data$ratioV1V2 <- ifelse(V1>V2, V1/V2, V2/V1) # or pmax(V1,V2)/pmin(V1, V2) and either attach(data) or use inside with(data, ).> This is a bit of a hack, and is taking forever for some reson (about > 40,000 rows in my data.frame).You appear to be doing a single calculation 40,000 times. Did you not get a warning there? (Maybe 40,000 warnings?) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
ifelse accomplishes this pretty easily (at least I think it does what you want) Look at ?apply too. HTH, Andy ## Try this foo.dat <- data.frame(Var1 = rnorm(40000, 1, 1), Var2 = (rnorm(40000, 1, 1) * 0.25)) plot(density(foo.dat$Var1 / foo.dat$Var2)) RatioOne <- ifelse(foo.dat$Var1 > foo.dat$Var2, foo.dat$Var1 / foo.dat$Var2, foo.dat$Var2 / foo.dat$Var1) RatioTwo <- numeric() for (i in 1:nrow(foo.dat)) { RatioTwo[i] <- if(foo.dat$Var1[i] > foo.dat$Var2[i]) foo.dat$Var1[i] / foo.dat$Var2[i] else foo.dat$Var2[i] / foo.dat$Var1[i] } cor(RatioOne, RatioTwo)
Minter! Is there an R cookbook? which lists this kind of common problem and common solution? On Thu, 16 Sep 2004, Dimitris Rizopoulos wrote:>Hi Dan, > >do you need something like that, > >dat <- data.frame(V1=rnorm(40000, 10), V2=rnorm(40000, 10)) >ratioV1V2 <- ifelse(dat$V1>dat$V2, dat$V1/dat$V2, dat$V2/dat$V1) > >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/16/396887 >Fax: +32/16/337015 >Web: http://www.med.kuleuven.ac.be/biostat/ > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > >----- Original Message ----- >From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> >To: <r-help at stat.math.ethz.ch> >Sent: Thursday, September 16, 2004 2:19 PM >Subject: [R] Conditionally swap two columns of a data.frame? > > >> >> I am doing this a kinda dumb way, and it is apparetnly taking >> forever. >> >> I have a data frame with two numeric columns. I want to look at >their >> correlation, and I am looking at the size ratio between the two. >> >> i.e. >> >> plot(density(data$V1/data$V2)) >> >> This kinda gives me a normal curve showing something about the >> distribution of the two values. >> >> I want to make sure that V1/V2 is always > 1 ... >> >> for (i in 1:length(row.names(data)) ){ >> ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 >> } >> >> This is a bit of a hack, and is taking forever for some reson (about >> 40,000 rows in my data.frame). >> >> I would just like to swap the values in the data frame (to put the >bigest >> first for example), then use the above plot to get what I want. >> >> Should I use the DB backend to the data to make the dump in this >way? >> (involves some hacky sql) >> >> Considering I am interested in the range of the ratio between V1 and >V2, >> should I be looking at doing a different analysis? >> >> I am so dumb, any help is appreciated, >> >> Dan. >> >> ______________________________________________ >> 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 >> >
Dimitris Rizopoulos
2004-Sep-16 12:56 UTC
[R] Conditionally swap two columns of a data.frame?
type "help.start()" in the R console or open manually the "C:\Program Files\R\rw1091\doc\html\rwin.html" file and you will get all the available on-line documentation that ships with each R installation. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> To: "Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> Cc: <r-help at stat.math.ethz.ch> Sent: Thursday, September 16, 2004 2:55 PM Subject: Re: [R] Conditionally swap two columns of a data.frame?> > Minter! > > Is there an R cookbook? which lists this kind of common problem andcommon> solution? > > > On Thu, 16 Sep 2004, Dimitris Rizopoulos wrote: > > >Hi Dan, > > > >do you need something like that, > > > >dat <- data.frame(V1=rnorm(40000, 10), V2=rnorm(40000, 10)) > >ratioV1V2 <- ifelse(dat$V1>dat$V2, dat$V1/dat$V2, dat$V2/dat$V1) > > > >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/16/396887 > >Fax: +32/16/337015 > >Web: http://www.med.kuleuven.ac.be/biostat/ > > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > > > >----- Original Message ----- > >From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> > >To: <r-help at stat.math.ethz.ch> > >Sent: Thursday, September 16, 2004 2:19 PM > >Subject: [R] Conditionally swap two columns of a data.frame? > > > > > >> > >> I am doing this a kinda dumb way, and it is apparetnly taking > >> forever. > >> > >> I have a data frame with two numeric columns. I want to look at > >their > >> correlation, and I am looking at the size ratio between the two. > >> > >> i.e. > >> > >> plot(density(data$V1/data$V2)) > >> > >> This kinda gives me a normal curve showing something about the > >> distribution of the two values. > >> > >> I want to make sure that V1/V2 is always > 1 ... > >> > >> for (i in 1:length(row.names(data)) ){ > >> ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 > >> } > >> > >> This is a bit of a hack, and is taking forever for some reson(about> >> 40,000 rows in my data.frame). > >> > >> I would just like to swap the values in the data frame (to putthe> >bigest > >> first for example), then use the above plot to get what I want. > >> > >> Should I use the DB backend to the data to make the dump in this > >way? > >> (involves some hacky sql) > >> > >> Considering I am interested in the range of the ratio between V1and> >V2, > >> should I be looking at doing a different analysis? > >> > >> I am so dumb, any help is appreciated, > >> > >> Dan. > >> > >> ______________________________________________ > >> 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 > >> > > >
On Thu, 16 Sep 2004, Dimitris Rizopoulos wrote:>type "help.start()" in the R console or open manually the "C:\Program >Files\R\rw1091\doc\html\rwin.html" file and you will get all the >available on-line documentation that ships with each R installation.That is the problem, you get it all ;) Thanks though, Dan.> >Best, >Dimitris > >---- >Dimitris Rizopoulos >Ph.D. Student >Biostatistical Centre >School of Public Health >Catholic University of Leuven > >Address: Kapucijnenvoer 35, Leuven, Belgium >Tel: +32/16/396887 >Fax: +32/16/337015 >Web: http://www.med.kuleuven.ac.be/biostat/ > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > > >----- Original Message ----- >From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> >To: "Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> >Cc: <r-help at stat.math.ethz.ch> >Sent: Thursday, September 16, 2004 2:55 PM >Subject: Re: [R] Conditionally swap two columns of a data.frame? > > >> >> Minter! >> >> Is there an R cookbook? which lists this kind of common problem and >common >> solution? >> >> >> On Thu, 16 Sep 2004, Dimitris Rizopoulos wrote: >> >> >Hi Dan, >> > >> >do you need something like that, >> > >> >dat <- data.frame(V1=rnorm(40000, 10), V2=rnorm(40000, 10)) >> >ratioV1V2 <- ifelse(dat$V1>dat$V2, dat$V1/dat$V2, dat$V2/dat$V1) >> > >> >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/16/396887 >> >Fax: +32/16/337015 >> >Web: http://www.med.kuleuven.ac.be/biostat/ >> > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm >> > >> >----- Original Message ----- >> >From: "Dan Bolser" <dmb at mrc-dunn.cam.ac.uk> >> >To: <r-help at stat.math.ethz.ch> >> >Sent: Thursday, September 16, 2004 2:19 PM >> >Subject: [R] Conditionally swap two columns of a data.frame? >> > >> > >> >> >> >> I am doing this a kinda dumb way, and it is apparetnly taking >> >> forever. >> >> >> >> I have a data frame with two numeric columns. I want to look at >> >their >> >> correlation, and I am looking at the size ratio between the two. >> >> >> >> i.e. >> >> >> >> plot(density(data$V1/data$V2)) >> >> >> >> This kinda gives me a normal curve showing something about the >> >> distribution of the two values. >> >> >> >> I want to make sure that V1/V2 is always > 1 ... >> >> >> >> for (i in 1:length(row.names(data)) ){ >> >> ratioV1V2 <- if(V1>V2) V1/V2 else V2/V1 >> >> } >> >> >> >> This is a bit of a hack, and is taking forever for some reson >(about >> >> 40,000 rows in my data.frame). >> >> >> >> I would just like to swap the values in the data frame (to put >the >> >bigest >> >> first for example), then use the above plot to get what I want. >> >> >> >> Should I use the DB backend to the data to make the dump in this >> >way? >> >> (involves some hacky sql) >> >> >> >> Considering I am interested in the range of the ratio between V1 >and >> >V2, >> >> should I be looking at doing a different analysis? >> >> >> >> I am so dumb, any help is appreciated, >> >> >> >> Dan. >> >> >> >> ______________________________________________ >> >> 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 >> >> >> > >> >
Hoi Dan, --On donderdag 16 september 2004 13:55 +0100 Dan Bolser <dmb at mrc-dunn.cam.ac.uk> wrote:> Is there an R cookbook? >Yes there is (sort of): StatsRus <http://www.ku.edu/~pauljohn/R/Rtips.html> kind regards, Paul -- Paul Lemmens NICI, University of Nijmegen ASCII Ribbon Campaign /"\ Montessorilaan 3 (B.01.05) Against HTML Mail \ / NL-6525 HR Nijmegen X The Netherlands / \ Phonenumber +31-24-3612648 Fax +31-24-3616066