Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a reference to the row number from a julia dataframe?

In Julia, dataframes are output with a sequential row number. I would like to access the row numbers as if it were a ROWID in the dataframe. I need this to calculate a regression.

Right now I do this with trick to add an extra column (x1) to mimic the rowid:

data = hcat(data, collect(1:size(data,1)));
glm(Column ~ x1, data, Normal(), IdentityLink())

I would like to achieve this directly, without adding an extra x1 column to the table. The x1 column I use now is a workaround. So what I'd like to achieve is to get the first line that adds an extra column. If the x1 column is not added, the glm() function crashes. So I need to find a direct replacement for the x1 in the glm() function (if that is possible).

Is there a simple way to get to the rowid of the tableframe?

like image 451
Johannes Avatar asked Jan 31 '17 10:01

Johannes


People also ask

How do you obtain the row numbers in a data frame?

Get Number of Rows in DataFrame You can use len(df. index) to find the number of rows in pandas DataFrame, df. index returns RangeIndex(start=0, stop=8, step=1) and use it on len() to get the count.

How do I get the row number in a DataFrame in R?

R provides us nrow() function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.

How do I reference a row index in R?

We can select rows (observations) by Index in R by using a single square bracket operator df[rows,columns] , From the square bracket, we should be using rows position, and columns are used to select columns. In R rows are called observations and columns are called variables.


1 Answers

Starting from DataFrames v0.22.0 rownumber(dfr::DataFrameRow) returns the row number.

julia> data = DataFrame(a=4:6, b=7:9)
3×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     4      7
   2 │     5      8
   3 │     6      9

julia> rownumber.(eachrow(data))
3-element Array{Int64,1}:
 1
 2
 3
like image 150
attdona Avatar answered Jan 04 '23 07:01

attdona