Hi, Here's the data we have:> rs[1:5,]probe_id f1 f2 f3 f4 M A f 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59 I want rs$f be the max of f1, f2, f3, f4 like a function in excel (=max(f1, f2, f3, f4)). How should we do it in R? I'm not familiar with programing with list. So please be patient Thanks -- Regards, Anh Tran [[alternative HTML version deleted]]
Is this what you want:> x <- read.table(textConnection(" probe_id f1 f2 f3 f4 M A f+ 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 + 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 + 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 + 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 + 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59"), header=TRUE)> closeAllConnections() > # 'apply' to the rows; need to convert to numeric since it is coersed to character > x$f <- apply(x, 1, function(a) max(as.numeric(a[c('f1','f2','f3','f4')]))) > xprobe_id f1 f2 f3 f4 M A f 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 58 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 22 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 43 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 34 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 44>On Tue, Jun 24, 2008 at 6:41 PM, Anh Tran <popophobia at gmail.com> wrote:> Hi, > Here's the data we have: > >> rs[1:5,] > probe_id f1 f2 f3 f4 M A f > 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 > 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 > 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 > 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 > 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59 > > I want rs$f be the max of f1, f2, f3, f4 like a function in excel (=max(f1, > f2, f3, f4)). > > How should we do it in R? I'm not familiar with programing with list. So > please be patient > Thanks > -- > Regards, > Anh Tran > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Hi Anh, I know that it is not so elegant, but may work. x<-runif(10) y<-runif(10)*2 z<-runif(10)*3 df<-data.frame(cbind(x,y,z)) df df$f<-NULL for (i in 1:nrow(df)) { df[i,"f"]<-max(c(df[i,"x"],df[i,"y"],df[i,"z"])) } Of course, others that know better apply family will solve if with a line :-) There are some functions for rowSums, rowMeans (see the help). Kind regards, miltinho On 6/24/08, Anh Tran <popophobia@gmail.com> wrote:> > Hi, > Here's the data we have: > > > rs[1:5,] > probe_id f1 f2 f3 f4 M A f > 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 > 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 > 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 > 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 > 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59 > > I want rs$f be the max of f1, f2, f3, f4 like a function in excel (=max(f1, > f2, f3, f4)). > > How should we do it in R? I'm not familiar with programing with list. So > please be patient > Thanks > -- > Regards, > Anh Tran > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
?pmax Anh Tran wrote:> Hi, > Here's the data we have: > >> rs[1:5,] > probe_id f1 f2 f3 f4 M A f > 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 > 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 > 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 > 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 > 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59 > > I want rs$f be the max of f1, f2, f3, f4 like a function in excel (=max(f1, > f2, f3, f4)). > > How should we do it in R? I'm not familiar with programing with list. So > please be patient > Thanks
apply max to columns f1...f4 and assign it to rs$f: rs$f <- apply(rs[,paste("f",1:4,sep="")],1,max) or rs$f <- apply(rs[,2:5],1,max) On Wed, Jun 25, 2008 at 1:41 AM, Anh Tran <popophobia@gmail.com> wrote:> Hi, > Here's the data we have: > > > rs[1:5,] > probe_id f1 f2 f3 f4 M A f > 1 A_68_P20002076 2 58 0 0 1.51778114 6.344453 59 > 2 A_68_P20002775 22 8 15 0 0.43419304 5.488819 59 > 3 A_68_P20005791 43 3 0 0 0.05698666 9.830594 59 > 4 A_68_P20005805 11 34 0 0 1.71076835 6.624038 59 > 5 A_68_P20006729 16 44 0 0 0.85498261 6.044229 59 > > I want rs$f be the max of f1, f2, f3, f4 like a function in excel (=max(f1, > f2, f3, f4)). > > How should we do it in R? I'm not familiar with programing with list. So > please be patient > Thanks > -- > Regards, > Anh Tran > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]