Mutate a new variable with row numbers

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

1 Like

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


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
edit: changed to size(df,1), my bad

1 Like

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

otherwise, I get this error:
Capture

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> 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> 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          β”‚
2 Likes

or:

df.row = axes(df, 1)

For more info: ?axes

2 Likes