[R] Add function to each row excluding certain columns
arun
smartpink111 at yahoo.com
Wed Oct 9 22:56:16 CEST 2013
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!!
More information about the R-help
mailing list