DataFrames not showing value

d = .05
n = .01
g = .015
s = .3
k = 6
i=s
t=0:50
yn= function(t)
    ((y-k)+I*t)
using DataFrames
df = DataFrame(A = yn, B = t)
print(df)

When I run this, istead of giving each value of yn for each value of t, it has #11 for each value of A. Why is this, and how do I change it?

Because yn is pointing to an anonymous function called β€œ#11” which you then put in the column A. Are you by chance copying and pasting Matlab code? The syntax, while close, are not compatible. Here’s how the Julia code should look like.

t= 0:50

function yn(t)  ## now the name of the function is yn()
   d = .05
   n = .01
   g = .015
   s = .3
   k = 6
   t*(d+n+g+s)
end 

You’ll see that I have defined a function called yn which accepts one argument called t. I’ve put all your local variables inside the function itself (Julia likes it when everything is inside a function). I’ve also got rid of the expression ((y-k)+I*t) since I don’t know what I and y are and replaced it with my own expression for illustration.

You can now evaluate this function as follows: yn(t) where t = 1:15 although this returns a lazy evaluator so you may need to collect the results… i.e. myresults = collect(yn(t)). Then you can assign this to a dataframe DataFrame(A = myresults, B = t).

Alternatively, given that you probably don’t have a lot of experience yet, I’d just do it more simply with a for loop

myresults = zeros(Float64, 15)
for t = 1:15 
   myresults[t] = yn(t) # evaluate the function at every time instead of a range like 1:15
end
DataFrame(A = myresults, B = 1:15)

Now it’s repeating yn in the A column.

BTW: please provide a working example, which one can improve. In your example y and I are not defined, so I can only guess what you want to do.

I see that yn is a function. You probably want to have in df the result of yn. So you have to call it when you define df. Maybe you intend something like that:

yn(t, y, k, I) = (y-k) .+ I .* t  # definition of yn, probably you need some broadcasting in it, it returns a vector

t = 0:50
df = DataFrame(A=yn(t, 1, 6, 100:150), B=t)    # now you call yn

julia> first(df, 6)
6Γ—2 DataFrame
β”‚ Row β”‚ A     β”‚ B     β”‚
β”‚     β”‚ Int64 β”‚ Int64 β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1   β”‚ -5    β”‚ 0     β”‚
β”‚ 2   β”‚ 96    β”‚ 1     β”‚
β”‚ 3   β”‚ 199   β”‚ 2     β”‚
β”‚ 4   β”‚ 304   β”‚ 3     β”‚
β”‚ 5   β”‚ 411   β”‚ 4     β”‚
β”‚ 6   β”‚ 520   β”‚ 5     β”‚
1 Like

I assigned a value to I, since I realize this was forgotten. Should be a MWE now

Thank’s that solved the issue. Only thing is it says yn is used, so I needed a new variable. Is there a way to remove a variable assignment in console?

you have to restart the repl (CTRL^D).

you need to read a bit more regarding function declaration, mapping a function, before just putting things that runs in REPL and hope it does what you imagined. A quick one line way of doing what you want would be:

yn = @. (y-k)+I*t

though I don’t see y, I being defined…

Not sure I have REPL, or what console. On my laptop, there is no console of anykind.

how are you using julia?

it is REPL, I had to look at it.

Got it. Thank you very much!