Re: [R] easy way to cap a data frame to a max value
Assuming the data frame is all numeric: DF[] - pmax(10, unlist(DF)) On Fri, Nov 7, 2008 at 4:16 PM, Grey Moran [EMAIL PROTECTED] wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] easy way to cap a data frame to a max value
Are they objects of class matrix or data.frame? You seem to make reference to both, but know there is a difference. Try: junk[junk 5] - 5 Grey Moran wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] easy way to cap a data frame to a max value
That should be pmin: On Fri, Nov 7, 2008 at 4:25 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Assuming the data frame is all numeric: DF[] - pmax(10, unlist(DF)) On Fri, Nov 7, 2008 at 4:16 PM, Grey Moran [EMAIL PROTECTED] wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] easy way to cap a data frame to a max value
Thanks to all who replied - lots of good ideas. The one I prefered at the end was: junk[junk 5] - 5 Grey On Fri, Nov 7, 2008 at 4:38 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: That should be pmin: On Fri, Nov 7, 2008 at 4:25 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Assuming the data frame is all numeric: DF[] - pmax(10, unlist(DF)) On Fri, Nov 7, 2008 at 4:16 PM, Grey Moran [EMAIL PROTECTED] wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] easy way to cap a data frame to a max value
On Fri, Nov 7, 2008 at 4:46 PM, Grey Moran [EMAIL PROTECTED] wrote: Thanks to all who replied - lots of good ideas. The one I prefered at the end was: junk[junk 5] - 5 Grey On Fri, Nov 7, 2008 at 4:38 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: That should be pmin: On Fri, Nov 7, 2008 at 4:25 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Assuming the data frame is all numeric: DF[] - pmax(10, unlist(DF)) On Fri, Nov 7, 2008 at 4:16 PM, Grey Moran [EMAIL PROTECTED] wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] easy way to cap a data frame to a max value
A few more. These each have the advantage of not destroying the original data frame: # based on Erik's DF2 - replace(DF, DF 10, 10) # based on my previous one DF2 - replace(DF, TRUE, pmin(10, unlist(DF))) DF2 - (DF + 10)/2 - abs(DF - 10)/2 On Fri, Nov 7, 2008 at 4:38 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: That should be pmin: On Fri, Nov 7, 2008 at 4:25 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Assuming the data frame is all numeric: DF[] - pmax(10, unlist(DF)) On Fri, Nov 7, 2008 at 4:16 PM, Grey Moran [EMAIL PROTECTED] wrote: Hello, I have some rather large matrices. Is there a way (without having to loop) to cap all the values of a data frame to a given ceiling? E.g. junk - cbind(c(1,2,3,4,5),c(2,4,6,8,10)) junk [,1] [,2] [1,]12 [2,]24 [3,]36 [4,]48 [5,]5 10 replace anything over the value of 5 with 5... Thank you all, Grey __ R-help@r-project.org 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. __ R-help@r-project.org 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.