Frank Tamborello
2012-Apr-09 04:01 UTC
[R] return one vector that is the sum of a list of vectors
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? Sincerely, Frank Tamborello [[alternative HTML version deleted]]
Bert Gunter
2012-Apr-09 04:10 UTC
[R] return one vector that is the sum of a list of vectors
?get ?rowSums ?rowsum a "group" is not a defined data structure in R, btw. You have a much better chance of getting precise answers if you ask precise questions. See the Posting Guide. -- Bert On Sun, Apr 8, 2012 at 9:01 PM, Frank Tamborello <frank.tambo at gmail.com> 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? > > Sincerely, > Frank Tamborello > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
David Winsemius
2012-Apr-09 06:09 UTC
[R] return one vector that is the sum of a list of vectors
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