Hi, No problem. It depends upon how many columns you want to delete, or if they have any common names as in "Restriction"
example1<- as.matrix(read.table("example1.txt",header=TRUE,stringsAsFactors=FALSE,sep="\t",row.names=1,check.names=FALSE)) indx<- which(!grepl("Restriction", colnames(example1))) indx #[1] 1 2 3 4 # Changing names of some of the columns colnames(example1)[8:9] <- paste("CARP", 1:2) colnames(example1) #[1] "BARN" "BCGE" "BCVN" "BEAN" #[5] "Restriction 1" "Restriction 2" "Restriction 3" "CARP 1" #[9] "CARP 2" #Using the previous code: indx<- which(!grepl("Restriction", colnames(example1))) indx #[1] 1 2 3 4 8 9 #Remove both "Restriction" and "CARP" columns indxNew <- which(!grepl("Restriction|CARP", colnames(example1))) indxNew #[1] 1 2 3 4 example1New <- cbind(example1,`MAX without Restriction`=apply(example1[,indxNew],1,max,na.rm=TRUE)) #If there is any specific pattern that is followed for the removal process, you should mention. #For example, removing every 3rd column or so: indx2<- colnames(example1)[(((seq_len(ncol(example1))-1)%%3)+1)!=3] indx2 #[1] "BARN" "BCGE" "BEAN" "Restriction 1" #[5] "Restriction 3" "CARP 1" apply(example1[,indx2],1,max,na.rm=TRUE) A.K. Thank you for the fast answer! Is there any way to use the first solution way, but work with the column names instead of the number of the column. Because in further calculation I need to drop more columns than just the restriction ones and the real dataset has to many columns to work with the position of the column. Kind regards ----- Original Message ----- From: arun <smartpink...@yahoo.com> To: R help <r-help@r-project.org> Cc: Sent: Wednesday, October 9, 2013 4:56 PM Subject: Re: Add function to each row excluding certain columns Hi, Try: example1<- as.matrix(read.table("example1.txt",header=TRUE,stringsAsFactors=FALSE,sep="\t",row.names=1,check.names=FALSE)) example2 <- example1 example1New <- cbind(example1, `MAX without Restriction`=apply(example1[,1:4],1,max,na.rm=TRUE)) #or library(matrixStats) `MAX without Restriction`<- rowMaxs(example2[ ,colnames(example2)[!grepl("Restriction", colnames(example2))]],na.rm=TRUE) example2New <- cbind(example2, `MAX without Restriction`) identical(example1New,example2New) #[1] TRUE A.K. Hello I've got a matrix of the following form BARN BCGE BCVN BEAN Restriction 1 Restriction 2 Restriction 3 Restriction 4 Restriction 5 alpha.1 0.000172449 7.87E-05 -0.003271044 0.000921609 9.28E-19 2.00E-05 -0.000608211 NA NA alpha.2 0.000896744 0.000579453 -0.000623357 0.001260358 -1.36E-19 -5.22E-05 NA NA NA alpha.3 0.000832748 0.00076229 0.002170229 0.001159895 3.09E-19 -7.86E-05 NA NA NA alpha.4 0.000920545 NA 0.001680433 0.000459149 -3.08E-19 -3.59E-05 NA NA NA alpha.5 0.001385238 0.000527484 0.000593311 0.000549358 7.72E-19 -6.99E-05 NA NA NA alpha.6 0.000644262 0.000305714 -0.00044514 0.000407448 -9.68E-20 -5.56E-05 NA NA NA alpha.7 -0.00022808 -0.00017047 0.000109545 0.000601197 0 3.50E-05 NA NA NA alpha.8 -1.16E-05 -0.000105657 0.001403036 0.00058719 3.88E-19 8.64E-06 NA NA NA alpha.9 0.000633559 -4.33E-05 0.000724611 0.000841646 -4.82E-20 -3.29E-05 NA NA NA (see also file) Now I'd like to calculate the maximal value of each row, but exclude Restriction 1-5. An add a new column to the given matrix with the name 'MAX without Restriction' and the max of each row. I tried max<-apply(example1,1,function(x)max(x)) new<-cbind(example,max) but it gave a strange output, also I couldn't manage to exclude the columns Restriction 1-5. Thank you for your help!! ______________________________________________ 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.