[R] working on a vector with named elements - unname

William Dunlap wdunlap at tibco.com
Wed Feb 15 17:57:28 CET 2012


You could use [[ instead of [, as the latter drops
the names.  E.g., after
  > v <- c("a"=3,"a"=4,"c"=5,"d"=6)
change your
  > c("n"=v["a"]+v["c"],"n1"=v["d"]*3) # "n.a"=8 "n1.d"=18
   n.a n1.d 
     8   18 
to
  > c("n"=v[["a"]]+v[["c"]],"n1"=v[["d"]]*3) # "n"=8 "n1"=18
   n n1 
   8 18

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alexander
> Sent: Wednesday, February 15, 2012 1:43 AM
> To: r-help at r-project.org
> Subject: [R] working on a vector with named elements - unname
> 
> Hi, I am working under R2.11.1 Windows.
> I work with a set of parameters which is save under a vector. I think it is
> easier to understand, which parameter is called in the vector by naming all
> vector elements. For example, we have a vector which the parameters a,b,c,d.
> Is it normal to allow, that there are two elements with the same name? I
> think for lists, it is also possible. If I want to regroup the elements and
> work on them, they always keep their old name. Event if I attribute to a new
> name, the new name and the old name merge (lign3)
> 
> v<-c("a"=3,"a"=4,"c"=5,"d"=6)
> v["a"] #output 3
> c("n"=v["a"]+v["c"],"n1"=v["d"]*3) # "n.a"=8 "n1.d"=18
> 
> Is there any better solution/datatype to this ?
> 
> c("n"=unname(v["a"])+unname(v["c"]),"n1"=unname(v["d"])*3) # "n"=8 "n1"=18
> 
> Thanks a lot
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/working-on-a-vector-with-named-elements-
> unname-tp4389930p4389930.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list