[R] summing columns using partial labels
Rich FitzJohn
rich.fitzjohn at gmail.com
Tue Apr 5 23:30:08 CEST 2005
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 <- lapply(split(seq(along=d), gp), function(x) rowSums(d[x]))
names(d.sums) <- paste(unique(prefix), "sum", sep=".")
## Append to the end of the original data.frame
d.new <- cbind(d, d.sums)
Cheers,
Rich
On Apr 6, 2005 6:05 AM, T Petersen <Innkeyp-r at yahoo.com> wrote:
> 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 '.total'column (e.g. 'tosk.total') as
> this serves as a check that everything was done right.
>
> Kind regards
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Rich FitzJohn
rich.fitzjohn <at> gmail.com | http://homepages.paradise.net.nz/richa183
You are in a maze of twisty little functions, all alike
More information about the R-help
mailing list