I'd like to perform some calculations across a series of columns. Is there an easy way to do so? For example, I'd like to calculate the maximum value from columns 6-42. Although I can use, say: pmax(data[,6],data[,7],data[,8], etc.), I'd prefer to be able to do something like pmax(data[,6:42]). Unfortunately, doing so returns 36 columns. Thanks, Tim -- View this message in context: http://www.nabble.com/Calculate-across-columns-tp22549648p22549648.html Sent from the R help mailing list archive at Nabble.com.
?apply look at the MARGIN argument On Mon, Mar 16, 2009 at 8:56 PM, timbean <timbean at gmail.com> wrote:> > I'd like to perform some calculations across a series of columns. Is there an > easy way to do so? For example, I'd like to calculate the maximum value from > columns 6-42. Although I can use, say: > > pmax(data[,6],data[,7],data[,8], etc.), I'd prefer to be able to do > something like > > pmax(data[,6:42]). Unfortunately, doing so returns 36 columns. > > Thanks, > Tim > -- > View this message in context: http://www.nabble.com/Calculate-across-columns-tp22549648p22549648.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
try: apply(data[,c(6:42)], 2, max) (as stephen sefick Just mentioned...) On Tue, Mar 17, 2009 at 2:56 AM, timbean <timbean@gmail.com> wrote:> > I'd like to perform some calculations across a series of columns. Is there > an > easy way to do so? For example, I'd like to calculate the maximum value > from > columns 6-42. Although I can use, say: > > pmax(data[,6],data[,7],data[,8], etc.), I'd prefer to be able to do > something like > > pmax(data[,6:42]). Unfortunately, doing so returns 36 columns. > > Thanks, > Tim > -- > View this message in context: > http://www.nabble.com/Calculate-across-columns-tp22549648p22549648.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: www.talgalili.com www.biostatistics.co.il [[alternative HTML version deleted]]