1

I would like to ask how do i add/append new observations to empty r dataframe. I will be running a loop and I want to update this dataframe as and when the loop is running.

say for instance, I have this dataframe:

error <- data.frame(error_code = character(), row_no = character(), sleep_time = character(), time_scrape = character())

the loop that I will be running is:

for (i in 1:10) {
  if (i %% 2 == 0) {
    error_code = i
    row_no = i
    sleep_time = 60
    time_scrape = i
   }
error <- error %>% 
add_row(error_code = error_code, row_no = row_no, sleep_time = sleep_time, time_scrape = time_scrape)
}

I want to add in the values based on the value I get from the loop

I tried using add_row but it doesnt work. how should I do this such that at the end, I will get something like this: (p/s: this table below is created manually and not by the code above)

error_code row_no sleep_time time_scrape
1          2      2         60          2
2          4      4         60          4
mischva11
  • 2,811
  • 3
  • 18
  • 34
ruixin lee
  • 29
  • 3
  • Does this answer your question? [How can a add a row to a data frame in R?](https://stackoverflow.com/questions/28467068/how-can-a-add-a-row-to-a-data-frame-in-r) – mischva11 Jun 29 '20 at 08:28
  • 1
    Your code would work if 1) you initialize your dataframe with `integer()` instead of `character()` (because you are storing numbers). 2) Take the `add_row` code inside the `if` block. However, growing dataframe in a loop is inefficient and not a good practice. – Ronak Shah Jun 29 '20 at 08:32

3 Answers3

2

You can just define the data.frame at one go:

i = as.character(seq(2,10,by=2))
data.frame(error_code = i,row_no = i,sleep_time = 60,time_scrape = i)

If you absolutely, really, must do that, this would be how it works:

error <- data.frame(error_code = character(), row_no = character(), sleep_time = character(), time_scrape = character())

for (i in as.character(seq(2,10,by=2))){

error <- error %>%  
add_row(data.frame(error_code = i,
row_no = i,
sleep_time = as.character(60),
time_scrape = i,stringsAsFactors=FALSE))
}

This is not the best way to go about..And why are all your columns in characters when they look like numeric?

mischva11
  • 2,811
  • 3
  • 18
  • 34
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
0

You could solve this with using rbind, though this is very unperformant. Other solutions like from the other answer might me more convinient. But maybe you need the loop structure. So here you go:

 library(dplyr)

error <- data.frame(error_code = character(), row_no = character(), sleep_time = character(), time_scrape = character())


for (i in 1:10) {
  if (i %% 2 == 0) {
    error_code = i
    row_no = i
    sleep_time = 60
    time_scrape = i
    df<-data.frame(error_code, row_no, sleep_time, time_scrape)
    error <- rbind(error, df)
  }
  #error <- rbind(error, df)
}
mischva11
  • 2,811
  • 3
  • 18
  • 34
  • This is an answer already provided [here](https://stackoverflow.com/questions/28467068/how-can-a-add-a-row-to-a-data-frame-in-r). I still like to add this answer here, so OP got helped. Sometimes for begginers it's easier to learn on their own examples and do their transfer for the duplicated question. – mischva11 Jun 29 '20 at 08:33
0

I think this should get you what you want:

     e=c();r=c();s=c();t=c()
     
     for (i in 1:10){
     e[i] = 2*i
     r[i] = 2*i
     t[i] = 2*i
     df=data.frame(error_code=e,row_no=r,sleep_time=60,time_scrape=t)
     }
       }
       
       df