On Apr 9, 2012, at 12:01 AM, Frank Tamborello wrote:
Dear R Help,
I am attempting to write a function that takes a list of variable
groups and a vector of numbers (e.g., "jtsv" would indicate one
group of variables, "jtsv1", "jtsv2", etc, each of which name a
variable in a data frame), and returns a list of variable group
sums. For example, given list("jtsv", "ptsv") and c(1:10), where
jtsv1, etc, are all numeric vectors of the same length, the function
should return a list of jtsv and ptsv, where
jtsv <- jtsv1 + jtsv2 + jtsv3 + jtsv4 + jtsv5 + jtsv6 + jtsv7 +
jtsv8 + jtsv9 + jtsv10
ptsv <- ptsv1 + ptsv2 + ptsv3 + ptsv4 + ptsv5 + ptsv6 + ptsv7 +
ptsv8 + ptsv9 + ptsv10
So far I've used a for loop to paste together the names of the
variables as character vectors, so that I can at least name the
variables that I want to get, but I'm stuck on how to add together
the numeric vectors that those character vectors are ultimately
supposed to name. It seems like I should be able to map the
primitive "+" operator onto a list or vector that contains the
variable names. If that's a good way to go, what would that look
like? Or if that's not a good way to go, would someone please point
me in a good direction?
Perhaps (.... depending on detail not provided):
jtsv <- rowSums( dfrm[ , grep("jtsv", names(dfrm))] )
--
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.