Re: [R] How to sum values across multiple variables using a wildcard?

From: Simon Blomberg <blomsp_at_ozemail.com.au>
Date: Tue 21 Feb 2006 - 12:09:47 EST

data <- data.frame(var1=c(1,2,3), var2=c(3,4,5), var3=c(4,5,6), foo = c(100,200,300))
# sum rows with "var" in their name
rowSums(data[, grep("var", names(data))])

 1 2 3
 8 11 14

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

-- 
Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat.
Centre for Resource and Environmental Studies
The Australian National University
Canberra ACT 0200
Australia
T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au
F: +61 2 6125 0757
CRICOS Provider # 00120C

______________________________________________
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 Tue Feb 21 13:25:16 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:38 EST