# Issues with For Loop - Appending New DataFrames

**URL:** <https://discourse.julialang.org/t/issues-with-for-loop-appending-new-dataframes/37534>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 13, 2020, 10:40pm UTC](https://discourse.julialang.org/t/issues-with-for-loop-appending-new-dataframes/37534 "2020-04-13T22:40:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aedunn12](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@aedunn12](https://discourse.julialang.org/u/aedunn12)\
**Post date:** [April 13, 2020, 10:40pm UTC](https://discourse.julialang.org/t/issues-with-for-loop-appending-new-dataframes/37534/1 "2020-04-13T22:40:00Z")

</div>

Hello! I’m trying to run a pretty simple for loop. The premise is that I have a dataframe with 250 different subjects with time points over a number of different dates/times. The issue is that I need my my time to be sequential - but as it stands, with each new day starts a new time = 0. To solve this, I want to mark the turn of each new day for each subject, and add in sequential times accordingly. I’m having issue with the initial for loop in which I keep getting an error that df\_fin does not exist. Any help would be appreciated!

# create a column that marks each change of DATE for each ID

df\_pk.OCC\_DATE = 1  
df\_fin = DataFrame()  
for i = 1:250  
df\_new = df\_pk |\> @filter(\_.ID == i)  
df\_new = DataFrame(df\_new)  
for n = 2:length(df\_new.ID)  
if df\_new.DATE[n] != df\_new.DATE[n-1]  
df\_new.OCC\_DATE[n] = df\_new.OCC\_DATE[n-1]+1  
else  
df\_new.OCC\_DATE[n] = df\_new.OCC\_DATE[n-1]  
end  
end  
df\_fin = vcat(df\_fin, df\_new)  
end

# create a new time based on OCC\_DATE

for i = 1:length(df\_fin.ID)  
if OCC\_DATE == 1  
df\_pk.TIME\_NEW[i] = df\_pk.TIME[i]  
elseif OCC\_DATE == 2  
df\_pk.TIME\_NEW[i] = df\_pk.TIME[i]+24  
elseif OCC\_DATE == 3  
df\_pk.TIME\_NEW[i] = df\_pk.TIME[i]+48  
elseif OCC\_DATE == 4  
df\_pk.TIME\_NEW[i] - df\_pk.TIME[i]+72  
end  
end

 ![example](https://global.discourse-cdn.com/julialang/original/3X/2/7/27a250dc35d5739f614a4fcc3987d20281eca2b1.jpeg)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 13, 2020, 11:41pm UTC](https://discourse.julialang.org/t/issues-with-for-loop-appending-new-dataframes/37534/2 "2020-04-13T23:41:23Z")

</div>

Take a look at [some tips](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/8) for how to post code on discourse.

I think you are running to the sometimes unintuitive [behavior of global scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1).

Try adding `global df_fin` right after your `for i = 1:250`.
