An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100115/f97c756c/attachment.pl>
Remove part of string in colname and calculate mean for columns groups
2 messages · Joel Fürstenberg-Hägg, Dieter Menne
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.