Dear all, I have two probably very easy questions: (1) Is there a way to access certain variables by their string-based name representation? Example: numbers <- c("one", "two", "three") varname <- "numbers" print(varname[2]) (2) I need this functionality for a customized na.exclude() function that I am building, which should only exclude rows that have NA in certain columns. Maybe there is already a function which does exactly what I need, so I'd highly appreciate if someone could point me there ;) My current implementation looks like this: naexlcude <- function(data, varnames) { for(v in varnames){ data = subset(data, !is.na(v)) } data } Best Philipp
On Thu, Feb 11, 2010 at 4:18 PM, Philipp Rappold <philipp.rappold at gmail.com> wrote:> Dear all, > > I have two probably very easy questions: > > (1) Is there a way to access certain variables by their string-based name > representation? > > Example: > numbers <- c("one", "two", "three") > varname <- "numbers" > print(varname[2])print(get(varname)[2])> > (2) I need this functionality for a customized na.exclude() function that I > am building, which should only exclude rows that have NA in certain columns. > Maybe there is already a function which does exactly what I need, so I'd > highly appreciate if someone could point me there ;) > > My current implementation looks like this: > > naexlcude <- function(data, varnames) > { > ? ? ? ?for(v in varnames){ > ? ? ? ? ? ? ? ?data = subset(data, !is.na(v)) > ? ? ? ?} > > ? ? ? ?data > }f = function(x, vars) x[complete.cases(x[vars]),] b> > Best > Philipp > > ______________________________________________ > 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. >
Philipp Rappold wrote:> Dear all, > > I have two probably very easy questions: > > (1) Is there a way to access certain variables by their string-based > name representation? > > Example: > numbers <- c("one", "two", "three") > varname <- "numbers" > print(varname[2]) > > (2) I need this functionality for a customized na.exclude() function > that I am building, which should only exclude rows that have NA in > certain columns. Maybe there is already a function which does exactly > what I need, so I'd highly appreciate if someone could point me there ;) > > My current implementation looks like this: > > naexlcude <- function(data, varnames) > { > for(v in varnames){ > data = subset(data, !is.na(v)) > } > > data > } >Well, you can use get(varname)[2] or more generally things like eval(bquote(.(as.name(varname))[2])), but for this particular application, why not just use the standard indexing techniques? I think this will do ix <- apply(is.na(data[varnames]), 1, any) data[!ix,] e.g.> table(apply(is.na(airquality[c("Wind","Ozone")]),1, any))FALSE TRUE 116 37> colSums(is.na(airquality))Ozone Solar.R Wind Temp Month Day 37 7 0 0 0 0> table(apply(is.na(airquality[c("Solar.R","Ozone")]),1, any))FALSE TRUE 111 42 (Using functions like subset inside another function often leads to problems because of the nonstandard evaluation tricks that it uses. It is mainly useful to save tying on the command line.) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Philipp Rappold <philipp.rappold <at> gmail.com> writes:> > Dear all, >[...]> (2) I need this functionality for a customized na.exclude() function > that I am building, which should only exclude rows that have NA in > certain columns. Maybe there is already a function which does > exactly what I need, so I'd highly appreciate if someone could point > me there ;)I would use something like naexclude <- function(data, varnames) d[rowSums(is.na(data[,varnames,drop=FALSE])) == 0,] Dan> > My current implementation looks like this: > > naexlcude <- function(data, varnames) > { > for(v in varnames){ > data = subset(data, !is.na(v)) > } > > data > } > > Best > Philipp > >
For your first question, use the get() function -Don At 5:18 PM +0100 2/11/10, Philipp Rappold wrote:>Dear all, > >I have two probably very easy questions: > >(1) Is there a way to access certain variables by their string-based >name representation? > >Example: >numbers <- c("one", "two", "three") >varname <- "numbers" >print(varname[2]) > >(2) I need this functionality for a customized na.exclude() function >that I am building, which should only exclude rows that have NA in >certain columns. Maybe there is already a function which does >exactly what I need, so I'd highly appreciate if someone could point >me there ;) > >My current implementation looks like this: > >naexlcude <- function(data, varnames) >{ > for(v in varnames){ > data = subset(data, !is.na(v)) > } > > data >} > >Best >Philipp > >______________________________________________ >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
Maybe Matching Threads
- Using transform to add a date column to a dataframe
- Conditionally skip over "for(..){" and "}"??
- Odd behaviour in within.list() when deleting 2+ variables
- [LLVMdev] Make a comparation with IR builder
- Odd behaviour in within.list() when deleting 2+ variables