[R] {R} How to extract correctly from vector?

jim holtman jholtman at gmail.com
Thu Aug 25 15:57:30 CEST 2011


Do you want to use 'get' in the last statement:

for (i in 1:10){assign(nam[i],as.ltraj(xy=get(char[i]),date=DATE,...)

I would also suggest the you use a 'list' instead of creating a lot of
variables in the workspace; e.g.,

result <- lapply(char, function(x) as.ltraj(xy = get(x), date = DATE, ...))



On Thu, Aug 25, 2011 at 9:06 AM, J. Augusiak <jaugusiak at googlemail.com> wrote:
> Dear list,
>
>
>
> I have problem that I cannot solve and would like to ask your opinion. I
> tried to ask a few days ago already but got no answer and all my attempts to
> solve it by myself since then failed. Sorry for repeated posting! Here the
> problem broken down a bit.
>
>
>
> My problem basically is, that I want to use the elements of a character
> vector as names for objects and by recalling only the subscript of the
> character vector get the object information I assigned.
>
>
>
> Example:
>
>
>
> vector of names created as follows:
>
> char <- paste("A",1,sep="")
>
> for (i in 2:10) {char[i] <- paste("A",i,sep="")}
>
> char
>
> [1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10"
>
> Now assign each element as name for a data frame:
>
> (d <-
> data.frame(cbind(X1=1,Y1=1:10,X2=1,Y2=1:10,X3=1,Y3=1:10,X4=1,Y4=1:10,X5=1,Y5
> =1:10,
>
>
> X6=1,Y6=1:10,X7=1,Y7=1:10,X8=1,Y8=1:10,X9=1,Y9=1:10,X10=1,Y10=1:10)))
>
> x <- paste("X",1,sep="")
>
> y <- paste("Y",1,sep="")
>
> for (i in 2:10) {x[i] <- paste("X",i,sep="")
>
>                 y[i] <- paste("Y",i,sep="")}
>
> for (k in 1:10) {assign(char[k],d[,c(x[k],y[k])])}
>
>
>
> If I call "A1", I get a dataframe with columns "X1" and "Y1", so far so
> good.
>
>
>
> Now the trouble part:
>
> nam <- paste("test",1,sep="")
>
> for (i in 2:10) {nam[i] <- paste("test",i,sep="")}
>
>
>
> for (i in 1:10){assign(nam[i],as.ltraj(xy=char[1],date=DATE,...)
> ????????????
>
>
>
> Please don't get carried away by as.ltraj, it's an object type of the
> package adehabitat to analyze animal tracks. However, I need to feed it the
> xy (coordination) information in form of a data frame. I have 20 animals to
> analyze (per different setup), so I want it to work in a handier way than
> keep pushing buttons. By the way I handle it now, I only hand over the term
> "A1" but not the data frame "A1".
>
>
>
> How can I solve this. Turning the vector into a list does not work and I am
> not sure if there's something I can do...
>
>
>
> Please help if you can! Many thanks in advance already.
>
>
>
> Jacqueline
>
>
>
>
>        [[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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list