I have read that appending to vectors in R is bad practice. In that case, what should I do when I want to create a vector but I don't know its length ahead of time?
I am looking at a data frame that contains entries about when people are near a specific location. Each entry contains information about the person and the time there were close by, but there can be many entries for a single person.
# loc id time
# 1: z A 00:00
# 2: z A 00:01
# 3: z B 00:02
# 4: z A 00:02
# 5: z C 00:05
# 6: z C 00:07
# 7: z A 00:08
# 8: z A 00:09
# 9: z C 00:09
#10: z C 00:10
I want to create a new data frame in which each entry is a "visit" by a person, collating any entries from a single person that are close in time.
# loc id starttime endtime
# 1: z A 00:00 00:02
# 2: z C 00:05 00:07
# 3: z A 00:08 00:09
# 4: z C 00:09 00:10
They may be 50 entries for a single person in the first data frame which may be collated into 3 "visits" in the new data frame. I don't know ahead of time how many "visits" there are. So how should I go about creating this data frame?
I know of rbind, but in this case I would be binding each row one by one. Is that a good idea?
The other option is to go through the first data frame twice, once to figure out how big to make the second data frame and again to fill it, but that seems even more inefficient.