Christoph Scherber
2007-Jun-11 12:09 UTC
[R] Selecting all values smaller than X in a dataframe
Dear R users, I have a correlation matrix for a dataframe called "synth", for which I now want to select only those cells that have correlations larger than +/-0.6: synth=data.frame(x=rnorm(10,1),y=rnorm(10,2),z=rnorm(10,0.5)) w=cor(synth,use="pairwise.complete.obs") w=as.data.frame(w) w[,sapply(w,abs(w),">",0.6)] The problem is that using sapply with ">" or "<" doesn?t seem to work. How could I solve this problem? Thank you very much in advance for your help! Best wishes Christoph (I am using R 2.5.0 on Windows XP). -- Christoph Scherber DNPW, Agroecology University of Goettingen Waldweg 26 D-37073 Goettingen +49-(0)551-39-8807
Vladimir Eremeev
2007-Jun-11 12:35 UTC
[R] Selecting all values smaller than X in a dataframe
Christoph Scherber-2 wrote:> > Dear R users, > > I have a correlation matrix for a dataframe called "synth", for which I > now want to select only those cells that have correlations larger than > +/-0.6: > > synth=data.frame(x=rnorm(10,1),y=rnorm(10,2),z=rnorm(10,0.5)) > > w=cor(synth,use="pairwise.complete.obs") > w=as.data.frame(w) > w[,sapply(w,abs(w),">",0.6)] > > The problem is that using sapply with ">" or "<" doesn?t seem to work. > > How could I solve this problem? > >If you want to extract correlations with absolute value >0.6, then simply use w[abs(w)>0.6] Please, reread the help("sapply"). You give some extra arguments to this function. The first goes the vector, the second goes the function, and then - additional arguments to the function. Probably, you wanted w[sapply(abs(w),">",0.6)] This gives the same result. -- View this message in context: http://www.nabble.com/Selecting-all-values-smaller-than-X-in-a-dataframe-tf3901238.html#a11059804 Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2007-Jun-11 12:43 UTC
[R] Odp: Selecting all values smaller than X in a dataframe
Hi r-help-bounces at stat.math.ethz.ch napsal dne 11.06.2007 14:09:45:> Dear R users, > > I have a correlation matrix for a dataframe called "synth", for which I > now want to select only those cells that have correlations larger than > +/-0.6: > > synth=data.frame(x=rnorm(10,1),y=rnorm(10,2),z=rnorm(10,0.5)) > > w=cor(synth,use="pairwise.complete.obs") > w=as.data.frame(w)Why? Better is tu use abs(w)>.6 and/or which(abs(w)>.6, arr.ind=T) or, if you want actual values just w[abs(w)>.6] Regards Petr> w[,sapply(w,abs(w),">",0.6)] > > The problem is that using sapply with ">" or "<" doesn?t seem to work. > > How could I solve this problem? > > Thank you very much in advance for your help! > > Best wishes > Christoph > > (I am using R 2.5.0 on Windows XP). > > > > -- > Christoph Scherber > DNPW, Agroecology > University of Goettingen > Waldweg 26 > D-37073 Goettingen > > +49-(0)551-39-8807 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Ingo Michaelis
2007-Jun-11 13:03 UTC
[R] Selecting all values smaller than X in a dataframe
Christoph Scherber <Christoph.Scherber <at> agr.uni-goettingen.de> writes:> > Dear R users, > > I have a correlation matrix for a dataframe called "synth", for which I > now want to select only those cells that have correlations larger than > +/-0.6: > > synth=data.frame(x=rnorm(10,1),y=rnorm(10,2),z=rnorm(10,0.5)) > > w=cor(synth,use="pairwise.complete.obs") > w=as.data.frame(w) > w[,sapply(w,abs(w),">",0.6)]Dear Christoph, just change the last command to w[abs(w)>0.6] Best wishes Ingo