Hi Eric, Hopefully the following addresses your question:
library(XML) ## please do not include rm(list=ls()) in code other's might copy and paste ## it could remove things precious to them url <- "http://webapp.montcopa.org/sherreal/salelist.asp?saledate=05/25/2011" tbl <-data.frame(readHTMLTable(url))[2:404, c(3,5,6,8,9)] ## the ";" you had was unnecessary since the code is on one line names(tbl) <- c("Address", "Township", "Parcel", "SaleDate", "Costs") ## a better way to reset row names rownames(tbl) <- NULL ## not sure why you made x, I did not v <- gregexpr("( aka )|( AKA )", tbl$Address) ## having simplified how v is referenced ## it is easy to see that you are doing something fundamentally different ## with sapply(), you are operating on unlist()ed v ## with the for loop, you are unlisting each element of v ## Note the slight differences between s, s2, v1, & v2 s <- sapply(unlist(v), max) s2 <- sapply(v, function(x) max(unlist(x))) v1 <- numeric(length(v)) for(i in 1 :length(v)) v1[i] <- max(unlist(v[i])) v2 <- numeric(length(unlist(v))) for(i in 1:length(unlist(v))) v2[i] <- max(unlist(v)[i]) all.equal(s, v1) all.equal(s, v2) all.equal(s2, v1) ## For your edification note the following all.equal(c(1:length(tbl[,1])), 1:length(tbl[,1])) all.equal(v[c(1:length(v))], v) ## given that both are the same, it is generally best ## to choose the simpler of the two, so ## 1:length(tbl[, 1]) and v, NOT ## c(1:length(tbl[, 1])) and v[c(1:length(v))] HTH, Josh On Wed, May 25, 2011 at 9:49 PM, eric <ericst...@aol.com> wrote: > Statement 9 using sapply does not seem to give the correct answer (or at > least to me). Yet I do what I think is the same thing with statement 11 and > I get the answer I'm looking for. > > 9 : s <-sapply(unlist(v[c(1:length(v))]), max) > 11: for(i in 1 :length(v)) v1[i] <- max(unlist(v[i])) > > Shouldn't I get the same answer ? > > > library(XML) > rm(list=ls()) > url <- > "http://webapp.montcopa.org/sherreal/salelist.asp?saledate=05/25/2011" > tbl <-data.frame(readHTMLTable(url))[2:404, c(3,5,6,8,9)] > names(tbl) <- c("Address", "Township", "Parcel", "SaleDate", "Costs"); > rownames(tbl) <- c(1:length(tbl[,1])) > x <-tbl > v <- gregexpr("( aka )|( AKA )",x$Address) > s <-sapply(unlist(v[c(1:length(v))]), max) > v1 <-numeric(length(v)) > for(i in 1 :length(v)) v1[i] <- max(unlist(v[i])) > > -- > View this message in context: > http://r.789695.n4.nabble.com/What-am-I-doing-wrong-with-sapply-tp3551598p3551598.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ 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.