Sunday, 15 August 2010

Creating a Nested for Loop for Use in Scraping in R -



Creating a Nested for Loop for Use in Scraping in R -

i having problem executing nested loop create list of stems utilize in scrape. simple info frame contains 2 variables, name , number of photos. want code spit out list/matrix of stems this:

alex/1 alex/2 alex/3 alice/1 .....

i can work when doing rows 1 @ time unfortunately cannot figure out how through loop of names.

any help super appreciated , used wants scrape html structure!

here code thusfar:

stems<-list() for(name in data$names){ for(photo in data$photos) stems[photo]<-print(paste(name,1:data$photos,sep="/")) } stems.matrix<-as.matrix(stems)

is want? know said wanted list, think vector of characters easy.

#i'm guessing info looks this... info <- data.frame( names = c("simon" , "alex" , "ben", "dave") , photos = c( 4 , 3 , 2 , 3) , misc = c( 4 , 3 , 4 , 4 ) ) #use apply rather nested loops stem.list <- unlist( apply( info , 1 , function(x) { <- 1:x[2]; paste( x[1] , , x[3] , sep ="/") } ) )

which gives...

> stem.list [1] "simon/1/4" "simon/2/4" "simon/3/4" "simon/4/4" "alex/1/3" "alex/2/3" "alex/3/3" [8] "ben/1/4" "ben/2/4" "dave/1/4" "dave/2/4" "dave/3/4"

r loops nested-loops

No comments:

Post a Comment