[R] summing columns using partial labels

2005-04-05 Thread T Petersen
I have a dataset of the form Year tosk.fai tosk.isd tosk.gr ... tosk.total hysa.fai hysa.isd ... and so on. I want to sum all the columns using the first four letters in the columns label(e.g. 'tosk', 'hysa' etc.). How can you do that? Also, the sums should be without the

Re: [R] summing columns using partial labels

2005-04-05 Thread Rich FitzJohn
Gidday, Perhaps try something along these lines: ## Establish which 4-letter group each row belongs to prefix - substr(names(d), 1, 4) gp - match(prefix, unique(prefix)) gp[regexpr(\\.total$, names(d)) -1] - NA # Exclude `*.total' rows ## Sum up each of the groups d.sums -