I am running a program which has an output containing four vectors named meanfevs, meanfevns, pfevs, pfevns. I wish to return all four and be able to access them later. I used the command return(list(a=meanfevs,b=meanfevns,c=pfevs,d=pfevns)) it did give me the ouput. However the values did not get stored in the vectors a,b,c and d and i am not being able to access them later by just calling a/b/c/d. Please help. [[alternative HTML version deleted]]
Hi, No, you won't be able to simply call "a" and have that work. R returns these in a single object with components (elements) named a,b,c,d Here's a concrete example: func <- function(x, y) return(list(a = x+1, b = y + 2)) out <- func(3, 5) out[["a"]] # or out$a out[["b"]] # or out$b give the desired results. If you just do func(3,5) the value will be returned, but since it's not bound to a variable name, simply thrown away. Remember -- R is (almost) always pass-by-value, not by reference, and has pretty strong scoping. Best, Michael On Mon, Jul 9, 2012 at 1:02 PM, PRAGYA SUR <pragya1386 at gmail.com> wrote:> I am running a program which has an output containing four vectors named > meanfevs, meanfevns, pfevs, pfevns. I wish to return all four and be able > to access them later. I used the command > return(list(a=meanfevs,b=meanfevns,c=pfevs,d=pfevns)) > it did give me the ouput. However the values did not get stored in the > vectors a,b,c and d and i am not being able to access them later by just > calling a/b/c/d. Please help. > > [[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.
Indeed. You do not understand lists. The behavior you expect is not how R works. Have you read "An Introduction to R" where this is explained (section 6.1). Also chapter 10 and 10.7 in particular for scoping in R. See also ?with ?within ?eval and e.g. ?lm or ?xyplot for the ubiquitous use of "data" arguments to tell where names are to be looked up. -- Bert On Mon, Jul 9, 2012 at 11:02 AM, PRAGYA SUR <pragya1386@gmail.com> wrote:> I am running a program which has an output containing four vectors named > meanfevs, meanfevns, pfevs, pfevns. I wish to return all four and be able > to access them later. I used the command > return(list(a=meanfevs,b=meanfevns,c=pfevs,d=pfevns)) > it did give me the ouput. However the values did not get stored in the > vectors a,b,c and d and i am not being able to access them later by just > calling a/b/c/d. Please help. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- 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 [[alternative HTML version deleted]]
Hello, Try this: ?meanfevs<-c(3.5,4.25,6.5) ?meanfevns<-c(4.7,3.23,7.6) ?pfevs<-c(1.2,2.8,3.2) ?pfevns<-c(4.2,3.2,8.2) listret<-list(a=meanfevs,b=meanfevns,c=pfevs,d=pfevns) listret $a [1] 3.50 4.25 6.50 $b [1] 4.70 3.23 7.60 $c [1] 1.2 2.8 3.2 $d [1] 4.2 3.2 8.2 #To access the elements stored in e.g. "a" listret[[1]] #[1] 3.50 4.25 6.50 ?listret[["a"]] #[1] 3.50 4.25 6.50 ?listret$a #[1] 3.50 4.25 6.50 ?listret$a[1] #[1] 3.5 listret[[c(1,2)]] #[1] 4.25 A.K. ----- Original Message ----- From: PRAGYA SUR <pragya1386 at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, July 9, 2012 2:02 PM Subject: [R] returning multiple values I am running a program which has an output containing four vectors named meanfevs, meanfevns, pfevs, pfevns. I wish to return all four and be able to access them later. I used the command return(list(a=meanfevs,b=meanfevns,c=pfevs,d=pfevns)) it did give me the ouput. However the values did not get stored in the vectors a,b,c and d and i am not being able to access them later by just calling a/b/c/d. Please help. ??? [[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.