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]]
I suspect you need a get() in there somewhere, but this isn't a completely reproducible example so I didn't try it. get(filename) will return the R object whose name is given by the string filename. Sarah 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 > >-- Sarah Goslee http://www.functionaldiversity.org
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?
It sounds like if you know you can pass the string "A1" where you need it to go, the command get() *might* be what you are looking for. Also, you can make your code much faster/prettier because R is smart about recycling vectors of different lengths: char <- paste("A",1:10, sep="") x <- paste("X",1:10, sep="") y <- paste("Y",1:10, sep="") Hope this helps, Michael On Thu, Aug 25, 2011 at 9:06 AM, J. Augusiak <jaugusiak@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@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. >[[alternative HTML version deleted]]
> > 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 myattempts to> solve it by myself since then failed. Sorry for repeated posting! Herethe> 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 itthe> xy (coordination) information in form of a data frame. I have 20 animalsto> analyze (per different setup), so I want it to work in a handier waythan> keep pushing buttons. By the way I handle it now, I only hand over theterm> "A1" but not the data frame "A1". > > > > How can I solve this. Turning the vector into a list does not work and Iam> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.