# Mutate a new variable with row numbers

**URL:** https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985
**Category:** Data
**Created:** [November 12, 2019, 1:52am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985 "2019-11-12T01:52:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pradeip1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pradeip1/32/11253_2.png) [@pradeip1](https://discourse.julialang.org/u/pradeip1)
#### Post date: [November 12, 2019, 1:52am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985/1 "2019-11-12T01:52:56Z")

</div>

how to add a row number using Queryverse in Julia?  
for example in R I simply use ID = row\_number()

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [November 12, 2019, 5:37am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985/2 "2019-11-12T05:37:27Z")

</div>

Hi, welcome to the Julia Discourse!  
i dont know if there is a function dedicated for this, but you can do:

```julia

df = DataFrame(a=[1, 2, 3], b=[:a, :b, :c]) #example dataframe
3×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Symbol │
├─────┼───────┼────────┤
│ 1 │ 1 │ a │
│ 2 │ 2 │ b │
│ 3 │ 3 │ c │

df[!,:row_number] = 1:size(df,1) #the ! means an unsafe modification of df

julia> df
3×3 DataFrame
│ Row │ a │ b │ row_number │
│ │ Int64 │ Symbol │ Int64 │
├─────┼───────┼────────┼────────────┤
│ 1 │ 1 │ a │ 1 │
│ 2 │ 2 │ b │ 2 │
│ 3 │ 3 │ c │ 3 │

```

for more information about indexing, you can check [here](http://juliadata.github.io/DataFrames.jl/stable/lib/indexing/)  
edit: changed to `size(df,1)`, my bad

---

<div class="post-metadata">

### Author: ![pradeip1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pradeip1/32/11253_2.png) [@pradeip1](https://discourse.julialang.org/u/pradeip1)
#### Post date: [November 12, 2019, 9:13am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985/3 "2019-11-12T09:13:25Z")

</div>

Thanks…but this only works when I have the following:  
**df[!,:row\_number] = 1:size(df, 1)**

otherwise, I get this error:  
 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/1/7/17e01b5dc0a2bcf2a78e2ff7f699287215ce659a.png)

---

<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: [November 12, 2019, 10:16am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985/4 "2019-11-12T10:16:51Z")

</div>

I think @longemen3000 meant to write `size(df, 1)` above - which returns the number of rows of a DataFrame (in his example there are as many rows as columns so it didn’t matter!)

Consider:

```julia
julia> using DataFrames

julia> df = DataFrame(a = rand(5), b = rand(5))
5×2 DataFrame
│ Row │ a │ b │
│ │ Float64 │ Float64 │
├─────┼──────────┼──────────┤
│ 1 │ 0.814602 │ 0.826607 │
│ 2 │ 0.361759 │ 0.631964 │
│ 3 │ 0.937936 │ 0.485979 │
│ 4 │ 0.755144 │ 0.526638 │
│ 5 │ 0.932572 │ 0.577122 │

julia> size(df, 1)
5

julia> size(df, 2)
3

```

You could instead use the `nrow` function which might be safer than remembering which dimension to call `size` on:

```julia
julia> df[!, :row_number] = 1:nrow(df)
1:5

julia> df
5×3 DataFrame
│ Row │ a │ b │ row_number │
│ │ Float64 │ Float64 │ Int64 │
├─────┼──────────┼──────────┼────────────┤
│ 1 │ 0.814602 │ 0.826607 │ 1 │
│ 2 │ 0.361759 │ 0.631964 │ 2 │
│ 3 │ 0.937936 │ 0.485979 │ 3 │
│ 4 │ 0.755144 │ 0.526638 │ 4 │
│ 5 │ 0.932572 │ 0.577122 │ 5 │

```

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [November 12, 2019, 10:41am UTC](https://discourse.julialang.org/t/mutate-a-new-variable-with-row-numbers/30985/5 "2019-11-12T10:41:48Z")

</div>

or:

```julia
df.row = axes(df, 1)

```

For more info: `?axes`
