[R] Remove part of string in colname and calculate mean for columns groups
Dieter Menne
dieter.menne at menne-biomed.de
Fri Jan 15 12:20:26 CET 2010
Joel Fürstenberg-Hägg wrote:
>
>
>
> I have two question. First, I wonder how to remove a part of the column
> names in a matrix? I would like to remove the "_ACCX" or "_NAX" part
> below. Is there a method where the "_" as well as all characters after i
> can be removed?
>
> Secondly, I would like to calculate the mean of each column group in the
> matrix, for instance all columns beginning with "Akita", and save all new
> columns as a new matrix.
>
>
If you do that in the example you gave, duplicate column names would be the
result, but let's assume that was a typo.
# Simplify names
data = data.frame(Akita.0_ACC1=1:2,Akita.1_ACC2=2:3,Alc.0_ACC1=3:4)
names(data) = sub("_.*","",names(data))
# Make a new data frame with the Akitas only
dataAkita = data[,grep("Akita",names(data))]
To do the averaging, check the examples in package plyr.
Dieter
--
View this message in context: http://n4.nabble.com/Remove-part-of-string-in-colname-and-calculate-mean-for-columns-groups-tp1014652p1014724.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list