Accessing element in named vector in R without variable name -
i trying access value of element in named character vector in r. using example in http://www.r-tutor.com/r-introduction/vector/named-vector-members tried following:
v = c("mary", "sue") v [1] "mary" "sue" names(v) = c("first", "last") v first last "mary" "sue" v["first"] first "mary"
i return "mary" without name "first" (and shown in tutorial above), when try gives name along value. tried set variable, hoping give value
> teststr = v["first"] > teststr first "mary" >
but still variable name (first) along value. tried following, gives same - value along element name.
> > v[names(v)=="first"] first "mary" >
the data have work project produces same results. appreciate getting "mary" without "first".
thanks - pankaj
you can use unname
> unname(v["first"]) [1] "mary"
also, can use getelement
alternative
> getelement(v, "first") [1] "mary"
Comments
Post a Comment