I have a large data structure that looks like:> strsplit(st,",")[14395][1] "KGEG" [2] "SA => KGEG" [3] "72785" [4] "47.62139" [5] "-117.52778" [6] "723" [7] "WA" [8] "US" [9] "2" [10] "SPOKANE SPOKANE INTERNATIONAL AIRPORT" [11] "1" I'd like to be able to retrieve, for example, the latitude as.numeric(strsplit(st,",")[[14395]][4]) and longitude as.numeric(strsplit(st,",")[[14395]][5]) for the entry in the structure where strsplit(st,",")[[14395]][5])=="KGEG" by specifying various station IDs. That is, if I had a simpler structure I could formulate a logical index which would have something along the lines of as.numeric(st[st[1]=="KSEA"][4]) and it would return 47.62139. Somewhere I'm getting all bollixed up with the indexing and keep getting sytax errors. As you can see, the list is quite long (20K+) and I don't wish to have to look up each coordinate by hand. TIA Clint -- Clint Bowman INTERNET: clint at ecy.wa.gov Air Quality Modeler INTERNET: clint at math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600 FAX: (360) 407-7534 Olympia, WA 98504-7600
I'm not sure I fully understand your problem, but maybe something like: d <- strsplit(st, ",") index <- sapply(d, function(x) x[[1]]) == "KGEG" latitude <- sapply(d[index], function(x) x[[4]]) -roger Clint Bowman wrote:> I have a large data structure that looks like: > > >>strsplit(st,",")[14395] > > [1] "KGEG" > [2] "SA => KGEG" > [3] "72785" > [4] "47.62139" > [5] "-117.52778" > [6] "723" > [7] "WA" > [8] "US" > [9] "2" > [10] "SPOKANE SPOKANE INTERNATIONAL AIRPORT" > [11] "1" > > > I'd like to be able to retrieve, for example, the latitude > as.numeric(strsplit(st,",")[[14395]][4]) and longitude > as.numeric(strsplit(st,",")[[14395]][5]) for the entry in the structure > where strsplit(st,",")[[14395]][5])=="KGEG" by specifying various station > IDs. That is, if I had a simpler structure I could formulate a > logical index which would have something along the lines of > as.numeric(st[st[1]=="KSEA"][4]) and it would return 47.62139. > > Somewhere I'm getting all bollixed up with the indexing and keep getting > sytax errors. As you can see, the list is quite long (20K+) and I don't > wish to have to look up each coordinate by hand. > > TIA > > Clint >
> I have a large data structure that looks like: > >> strsplit(st,",")[14395] > [1] "KGEG" > [2] "SA => KGEG" > [3] "72785" > [4] "47.62139" > [5] "-117.52778" > [6] "723" > [7] "WA" > [8] "US" > [9] "2" > [10] "SPOKANE SPOKANE INTERNATIONAL AIRPORT" > [11] "1" > > > I'd like to be able to retrieve, for example, the latitude > as.numeric(strsplit(st,",")[[14395]][4]) and longitude > as.numeric(strsplit(st,",")[[14395]][5]) for the entry in the structure > where strsplit(st,",")[[14395]][5])=="KGEG" by specifying various station > IDs. That is, if I had a simpler structure I could formulate a > logical index which would have something along the lines of > as.numeric(st[st[1]=="KSEA"][4]) and it would return 47.62139.Too confusing for me. I'd just convert it to a data frame. ## UNTESTED!!! stm <- t(matrix(st,nrow=11)) stdf <- data.frame(stm) stdf[stdf[,1]=="KSEA",4] ## <OPTIONAL> - just makes life easier later ## do some "as.numeric" calls to various columns. stdf[,x] <- as.numeric(stdf[,x]) ## add some names names(stdf) <- c("blah",...) ## </OPTIONAL> Cheers Jason