function - R: output from for loop into vector/dataframe - object not found -


i trying compile 2 vectors for loop, cbind table. (i trying in 1 step, because of below issue i'm trying simplify.)

i set 3 vectors, id_name, count_rows, , id_test.

going through new_dat (a pre-exiting data frame), i'm setting vector result number of rows id i.

i'm printing output, works fine.

but when try push values vectors, error: object 'id_name' not found. , same other two.

here's code:

id_name <- c() count_rows <- c() id_test <- c()  (i in id) {   result <- sum(new_dat$id == i)    id_test <- c("hello", "world")   id_name <- c(id_name, i)   count_rows <-c(count_rows, result)    print(result)   print(i) } 

initialize vectors differently. see below; may need change numeric if data aren't numeric, , substitute data's actual length n (if know beforehand).

id_name <- vector('numeric', length=n) 

what doing creates null variable, of class null.


Comments