[R] Odp: {R} How to extract correctly from vector?
Petr PIKAL
petr.pikal at precheza.cz
Thu Aug 25 16:20:22 CEST 2011
>
> 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"
Slightly better
char <- paste("A",1:10,sep="")
>
> 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)))
One right parentheses more.
>
> 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="")}
Maybe you wanted
x <- paste("X",1:10,sep="")
y <- paste("Y",1:10,sep="")
>
> for (k in 1:10) {assign(char[k],d[,c(x[k],y[k])])}
I would use list.
lll<- vector(mode="list", 10)
names(lll) <- char
for(k in 1:10) lll[[k]] <- d[,c(x[k],y[k])]
instead of many An objects you have one object with names An and you can
easily subset it.
lll[[1]]
lll[["A5"]]
>
>
>
> 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="")}
nam <- paste("test",1:10,sep="")
>
>
>
> for (i in 1:10){assign(nam[i],as.ltraj(xy=char[1],date=DATE,...)
So the same applies here
lll<- vector(mode="list", 10)
names(lll) <- char
for (i in 1:10) lll[[i]] <- as.ltraj(xy=char[i],date=DATE,...)
However I do not know as.ltraj which may require different input of
parameters.
Anyway in this case are lists by my opinion the only way to be able to
handle such things smoothly and without much headache.
Regards
Petr
> ????????????
>
>
>
> 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.
More information about the R-help
mailing list