[R] Is there a better way to check if an element of a list exists than using match on names?
Paul.Rustomji at csiro.au
Paul.Rustomji at csiro.au
Wed Jul 30 00:20:33 CEST 2008
Hello R mailing list
Is there a better way than this to see if an element exists *within* a
list object :
#generate "file.txt" using current routine
cat("var1=33\nvar2=TRUE",file="file.txt")
#load file to a list called "ipf"
f <- function(.file){source(.file,local=TRUE);as.list(environment())}
ipf<- f("file.txt")
print(ipf)
#this is the bit I need help with...
res <- match("var1",names(ipf)) # returns result > 0 if "var" exists
within ipf ie. TRUE
print(res)
res <- match("var3",names(ipf)) #returns NA as "var3" is not in ipf ie.
FALSE
print(res)
I have tried the exists function but cannot get it to apply to items of
a list (though can get it to see if the list exists as an object...)
Thanks
Paul.
Paul Rustomji
Rivers and Estuaries
CSIRO Land and Water
GPO Box 1666
Canberra ACT 2601
ph +61 2 6246 5810
mobile 0406 375 739
More information about the R-help
mailing list