Dear R community, I am a beginner to R and have a question concerning mget, about I could not find anything in the various documentation. I have a column in a dataframe x for which I want to get values in y: mget(x[,1], env=y, ifnotfound=NA) I receive an error mesage: Error in mget(x[, 1], env = y, ifnotfound = NA) : attempt to use zero-length variable name this is probably due to the fact that some of the values in x[,1] are empty ("") I would like to get a "NA" for these values, and the found value for those where mget finds one. Could anyone tell me how this is done efficiently? Thanks a lot in advance! Georg P.S. I am in digest mode, cc'ing the answer directly to me would be nice -- Georg Wilhelm Otto Max-Planck-Institute for Developmental Biology Spemannstrasse 35/III D-72076 Tuebingen Germany phone: +49-7071-601 401 http://www.eb.tuebingen.mpg.de
Georg Otto wrote:> Dear R community, > > I am a beginner to R and have a question concerning mget, about I could > not find anything in the various documentation. > > I have a column in a dataframe x for which I want to get values in y: > > mget(x[,1], env=y, ifnotfound=NA) > > I receive an error mesage: > > Error in mget(x[, 1], env = y, ifnotfound = NA) : > attempt to use zero-length variable name > > this is probably due to the fact that some of the values in x[,1] are > empty ("") > > I would like to get a "NA" for these values, and the found value for > those where mget finds one. Could anyone tell me how this is done > efficiently? > > Thanks a lot in advance! > > Georg > > > P.S. I am in digest mode, cc'ing the answer directly to me would be nice > >Is y and environment? Please make your example reproducible - as long as I have not seen your example, I guess you don't want to use mget() at all ... Uwe Ligges
Here's one way to work around it:> nm <- c("x", "", "y") > x <- 10; y <- 20 > myObj <- structure(vector(mode="list", length=length(nm)), names=nm) > goodNames <- nm[nchar(nm) > 0] > myObj[goodNames] <- mget(goodNames, .GlobalEnv) > myObj$x [1] 10 [[2]] NULL $y [1] 20 Andy> From: Georg Otto > > Dear R community, > > I am a beginner to R and have a question concerning mget, > about I could > not find anything in the various documentation. > > I have a column in a dataframe x for which I want to get values in y: > > mget(x[,1], env=y, ifnotfound=NA) > > I receive an error mesage: > > Error in mget(x[, 1], env = y, ifnotfound = NA) : > attempt to use zero-length variable name > > this is probably due to the fact that some of the values in x[,1] are > empty ("") > > I would like to get a "NA" for these values, and the found value for > those where mget finds one. Could anyone tell me how this is done > efficiently? > > Thanks a lot in advance! > > Georg > > > P.S. I am in digest mode, cc'ing the answer directly to me > would be nice > > > -- > Georg Wilhelm Otto > Max-Planck-Institute for Developmental Biology > Spemannstrasse 35/III > D-72076 Tuebingen Germany > phone: +49-7071-601 401 > http://www.eb.tuebingen.mpg.de > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >