Storing data from a for in a dataframe

Asked

Viewed 39 times

0

I have airport codes and needed to store the data access link of these airports in a DF, I tried to do through a for, but is returning error.

Code I used:

aeroportos <- c("bsb",
                "cgh",
                "gru",
                "vcp",
                "sdu",
                "gig",
                "poa", 
                "cwb",
                "cnf", 
                "bel",
                "ssa",
                "rec", 
                "for",
                "slz",
                "cgr")

links <- data.frame(aeroporto = aeroportos, link = NA)

for(i in aeroportos) {
  
  for(j in 1:aeroportos) {
    
    link[j,2] <- paste0("https://www.flightradar24.com/data/airports/", i,"/statistics"))  
    
  }
}

Is returning the following error:

Unexpected error: '}' in "}"

1 answer

1


Some mistakes are:

Look at the line link[j,2] has two )) remove 1 of them...

you created a dataframe called links but then he’s trying to feed the link(without "s"), that is to say there are quite a lot of syntax errors...

Apparently you are wanting to feed the dataframe links with the right airport URL? if so, no need to create two fors, create an auxiliary variable that increments each iteration one j for example, the functional code would look like this:

aeroportos <- c("bsb",
                "cgh",
                "gru",
                "vcp",
                "sdu",
                "gig",
                "poa", 
                "cwb",
                "cnf", 
                "bel",
                "ssa",
                "rec", 
                "for",
                "slz",
                "cgr")

links <- data.frame(aeroporto = aeroportos, link = NA)

j=1;

for(i in aeroportos) {
  
 links[j,2] <- paste0("https://www.flightradar24.com/data/airports/", i,"/statistics") 

 j=j+1;
    
 
}

print(links)

result of print(links):

   aeroporto                                                       link
1        bsb https://www.flightradar24.com/data/airports/bsb/statistics
2        cgh https://www.flightradar24.com/data/airports/cgh/statistics
3        gru https://www.flightradar24.com/data/airports/gru/statistics
4        vcp https://www.flightradar24.com/data/airports/vcp/statistics
5        sdu https://www.flightradar24.com/data/airports/sdu/statistics
6        gig https://www.flightradar24.com/data/airports/gig/statistics
7        poa https://www.flightradar24.com/data/airports/poa/statistics
8        cwb https://www.flightradar24.com/data/airports/cwb/statistics
9        cnf https://www.flightradar24.com/data/airports/cnf/statistics
10       bel https://www.flightradar24.com/data/airports/bel/statistics
11       ssa https://www.flightradar24.com/data/airports/ssa/statistics
12       rec https://www.flightradar24.com/data/airports/rec/statistics
13       for https://www.flightradar24.com/data/airports/for/statistics
14       slz https://www.flightradar24.com/data/airports/slz/statistics
15       cgr https://www.flightradar24.com/data/airports/cgr/statistics
  • 1

    Thank you! I hadn’t noticed this object name error! It worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.