Re: [R] summing columns using partial labels

From: Rich FitzJohn <rich.fitzjohn_at_gmail.com>
Date: Wed 06 Apr 2005 - 07:30:08 EST

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@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@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

______________________________________________
R-help@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
Received on Wed Apr 06 07:35:25 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:02 EST