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.
______________________________________________
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.