mql4beginner
mql4beginner

Reputation: 2233

Why does this loop return NULL

I would like to replace specific string in an URL so I will get a data frame's columns of links per each "job".It seems that I have a problem with my loop .How can I solve this problem?

        dat <- read.table(text = "  Index          job          age            
                                         44        DBA            0      
                                         55        SAS            0      
                                         66        CLOUD          0      
                                         77        AJAX           0      
                                         88        C              1      
                                         99        FULLSTACK      1  ", header = TRUE)

    dat1<-as.list(dat[[2]])
    url  = "'as/DD/p=DD"
    test <-gsub('DD',dat1[[1]],url,ignore.case = T)   # checking the gsub result manually
    test
    #[1] "'as/DBA/p=DBA"    #this is correct

#Now inside a loop    
    s<- for (i in 1:length(dat1)){
            test <-gsub('DD',dat1[[1]],url,ignore.case = T)
         }
    s
    > s
    NULL  

Why do I get "Null" instead the following desired outcome:

    as/DBA/p=DBA
    as/SAS/p=SAS            
    as/CLOUD/p=CLOUD
    as/AJAX/p=AJAX
    as/C/p=C
    as/FULLSTACK/p=FULLSTACK  

Upvotes: 1

Views: 2593

Answers (1)

USER_1
USER_1

Reputation: 2469

You were not saving your result anywhere. If you change the loop so that it selects the appropriate list element and save the outcome to list it should give you what you want.

for (i in 1:length(dat1)){
  test <-gsub('DD',dat1[[i]],url,ignore.case = T)
  dat1[[i]] <- test
}

Upvotes: 2

Related Questions