# Julia reporting an extra ) when it doesn't exist

**URL:** https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644
**Category:** Performance
**Tags:** question, syntax, error-message
**Created:** [March 21, 2021, 6:18am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644 "2021-03-21T06:18:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 21, 2021, 6:18am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/1 "2021-03-21T06:18:23Z")

</div>

I have this for loop in Julia:

```julia
    for country in countries_data_labels
        new_dataframe = get_country(df, country)
        new_dataframe = DataFrame(new_dataframe)
        df_rows, df_columns = size(new_dataframe)
        new_dataframe_long = stack(new_dataframe, begin:end-4)
        y_axis[!, Symbol("$country")] = new_dataframe_long[!, :value]
    end

```

and I’m getting this error:

```julia
syntax: extra token ")" after end of expression

```

I decided to comment all of the body of the for loop except the 1st one and ran the cell each time after uncommenting to see which line was throwing this error and it was the 4th line in the body:

```julia
new_dataframe_long = stack(new_dataframe, begin:end-4)

```

There is no reason for this error to exist. There are no extra bracket pieces in this line.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 21, 2021, 6:23am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/2 "2021-03-21T06:23:26Z")

</div>

On my phone so can’t check, but `begin` and `end` syntax only works when indexing with square brackets `[] `

As an aside, DataFrames accept strings as column names, so no reason to do `y_axis[!, Symbol("$country")]`

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 21, 2021, 6:37am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/3 "2021-03-21T06:37:50Z")

</div>

Could you elaborate the 1st part? This is my full cell:

```julia
begin
	countries_data_labels = ["Canada", "Italy", "China", "United States", "Spain"]
	y_axis = DataFrame()
	
	
	for country in countries_data_labels
		
		new_dataframe = get_country(df, country)
		
		new_dataframe = DataFrame(new_dataframe)
		
		df_rows, df_columns = size(new_dataframe)
		
		new_dataframe_long = stack(new_dataframe, begin:end-4)
		
		y_axis[!, Symbol("$country")] = new_dataframe_long[!, :value]
		
	end
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 21, 2021, 8:29am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/4 "2021-03-21T08:29:45Z")

</div>

What I tried to say was that `begin` and `end` are special cased by the parser if they occur within an indexing expression. Take this example:

```julia
julia> a = collect(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

We can now get subsets of this vector using `begin` and `end` inside square brackets:

```julia
julia> a[begin:end-3]
2-element Vector{Int64}:
 1
 2

```

but let’s define our own indexing function which, when called, does not use square brackets:

```julia
julia> my_getter(vector, idxs) = vector[idxs]
my_getter (generic function with 1 method)

```

If we pass some numerical range to this it works as expected:

```julia
julia> my_indexer(a, 2:4)
3-element Vector{Int64}:
 2
 3
 4

```

but when we try to use `begin` and `end`:

```julia
julia> my_indexer(a, begin:end-2)
ERROR: syntax: "begin" at REPL[15]:1 expected "end", got ")"
Stacktrace:
 [1] top-level scope
   @ none:1

```

this doesn’t work, as they don’t occur inside square brackets. When inside square brackets, `end` is just syntactic sugar for `lastindex`, i.e. `a[end]` is the same as `a[lastindex(a)]`.

More broadly, it’s not really clear what your code is trying to achieve but it looks overly complicated. It seems you just want to subset your original DataFrame with a set of countries and then `stack` to turn it into long format, in which case it seems your whole cell could be replaced by a single line:

```julia
stack(df[in(countries_data_labels).(df."Country/Region"), :], measure_vars)

```

(check the docstring for `stack` to make sure you get the correct `measure_vars` and `id_vars`, this is always a little hard to do without an MWE)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 21, 2021, 8:45am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/5 "2021-03-21T08:45:17Z")

</div>

> [@nilshg](#):
>
> `my_indexer(a, begin:end-2)`

To explain further, what the parser sees here would be similar to this:

```julia
my_indexer(a, begin
  :end - 2)

```

Since `begin` outside of indexing expressions creates a new block of syntax and `:end` is parsed as the Symbol `Symbol("end")`, it’s expecting a closing of that open block instead of a closing bracket.

---

<div class="post-metadata">

### Author: ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)
#### Post date: [March 21, 2021, 8:46am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/6 "2021-03-21T08:46:11Z")

</div>

I did check the docstring but Pluto.jl is telling me that `measure_vars` is not defined

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 21, 2021, 8:50am UTC](https://discourse.julialang.org/t/julia-reporting-an-extra-when-it-doesnt-exist/57644/7 "2021-03-21T08:50:05Z")

</div>

`measure_vars` are just the variables that you want to stack into the `value` column, as the docstring shows:

```julia
  julia> df = DataFrame(a = repeat([1:3;], inner = [2]),
                        b = repeat([1:2;], inner = [3]),
                        c = randn(6),
                        d = randn(),
                        e = map(string, 'a':'f'))
  6×5 DataFrame
   Row │ a b c d e
       │ Int64 Int64 Float64 Float64 String
  ─────┼───────────────────────────────────────────
     1 │ 1 1 0.867347 0.532813 a
     2 │ 1 1 -0.901744 0.532813 b
     3 │ 2 1 -0.494479 0.532813 c
     4 │ 2 2 -0.902914 0.532813 d
     5 │ 3 2 0.864401 0.532813 e
     6 │ 3 2 2.21188 0.532813 f
  
  julia> stack(df, [:c, :d])
  12×5 DataFrame
   Row │ a b e variable value
       │ Int64 Int64 String String Float64
  ─────┼───────────────────────────────────────────
     1 │ 1 1 a c 0.867347
     2 │ 1 1 b c -0.901744
     3 │ 2 1 c c -0.494479
     4 │ 2 2 d c -0.902914
    ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮
    10 │ 2 2 d d 0.532813
    11 │ 3 2 e d 0.532813
    12 │ 3 2 f d 0.532813
                                     5 rows omitted

```

you need to replace `measure_vars` with whatever variables.

There are loads of additional examples in the excellent DataFrames tutorial which I’ve already recommended twice to you, long-to-wide and wide-to-long reshapes are covered here: [Julia-DataFrames-Tutorial/09\_reshaping.ipynb at master · bkamins/Julia-DataFrames-Tutorial · GitHub](https://github.com/bkamins/Julia-DataFrames-Tutorial/blob/master/09_reshaping.ipynb)
