Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df0602201701n5f15eb9cjc98608c983ce41a9@mail.gmail.com>
Date: 2006-02-21T01:01:24Z
From: Gabor Grothendieck
Subject: How to sum values across multiple variables using a wildcard?
In-Reply-To: <e40d78ce0602201641t2c9aac89w4a2be86ebed2983b@mail.gmail.com>

See:

?rowSums



On 2/20/06, mtb954 at gmail.com <mtb954 at gmail.com> wrote:
> I have a dataframe called "data" with 5 records (in rows) each of
> which has been scored on each of many variables (in columns).
>
> Five of the variables are named var1, var2, var3, var4, var5 using
> headers. The other variables are named using other conventions.
>
> I can create a new variable called var6 with the value 15 for each
> record with this code:
>
> > var6=var1+var2+var3+var4+var5
>
> but this is tedious for my real dataset with dozens of variables. I
> would rather use a wildcard to add up all the variables that begin
> with "Var" like this pseudocode:
>
> > Var6=sum(var*)
>
> Any suggestions for implementing this in R? Thanks! Mark
>
> ______________________________________________
> 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
>