How to get row index number in R?

RDataframe

R Problem Overview


Suppose I have a list or data frame in R, and I would like to get the row index, how do I do that? That is, I would like to know how many rows a certain matrix consists of.

R Solutions


Solution 1 - R

I'm interpreting your question to be about getting row numbers.

  • You can try as.numeric(rownames(df)) if you haven't set the rownames. Otherwise use a sequence of 1:nrow(df).
  • The which() function converts a TRUE/FALSE row index into row numbers.

Solution 2 - R

It not quite clear what exactly you are trying to do.

To reference a row in a data frame use df[row,]

To get the first position in a vector of something use match(item,vector), where the vector could be one of the columns of your data frame, eg df$cname if the column name is cname.

Edit:

To combine these you would write:

df[match(item,df$cname),]

Note that the match gives you the first item in the list, so if you are not looking for a unique reference number, you may want to consider something else.

Solution 3 - R

See row in ?base::row. This gives the row indices for any matrix-like object.

Solution 4 - R

rownames(dataframe)

This will give you the index of dataframe

Solution 5 - R

If i understand your question, you just want to be able to access items in a data frame (or list) by row:

x = matrix( ceiling(9*runif(20)), nrow=5  )   
colnames(x) = c("col1", "col2", "col3", "col4")
df = data.frame(x)      # create a small data frame

df[1,]                  # get the first row
df[3,]                  # get the third row
df[nrow(df),]           # get the last row

lf = as.list(df)        

lf[[1]]                 # get first row
lf[[3]]                 # get third row

etc.

Solution 6 - R

Perhaps this complementary example of "match" would be helpful.

Having two datasets:

first_dataset <- data.frame(name = c("John", "Luke", "Simon", "Gregory", "Mary"),
                            role = c("Audit", "HR", "Accountant", "Mechanic", "Engineer"))

second_dataset <- data.frame(name = c("Mary", "Gregory", "Luke", "Simon"))

If the name column contains only unique across collection values (across whole collection) then you can access row in other dataset by value of index returned by match

name_mapping <- match(second_dataset$name, first_dataset$name)

match returns proper row indexes of names in first_dataset from given names from second: 5 4 2 1 example here - accesing roles from first dataset by row index (by given name value)

for(i in 1:length(name_mapping)) {
    role <- as.character(first_dataset$role[name_mapping[i]])   
    second_dataset$role[i] = role
}

===

second dataset with new column:
     name       role
1    Mary   Engineer
2 Gregory   Mechanic
3    Luke Supervisor
4   Simon Accountant

[tag:R]

Solution 7 - R

x <-  matrix(ceiling(9*runif(20)), nrow=5)
colnames(x) <-  c("these", "are", "the", "columnes")
df <-  data.frame(x)

Result: dataframe

which(df == "2")                       #returns rowIndexes results from the entire dataset, in this case it returns a list of 3 index numb

Result: > 5 13 17

length(which(df == "2"))               #count numb. of rows that matches the condition of ==2

Result: > 3

also you can do this column wise, exemple:

which(df$columnName == c("2", "7"))     #you do the same with strings
length(which(df$columnName == c("2", "7")))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlesbegue&#39;s alter egoView Question on Stackoverflow
Solution 1 - RShaneView Answer on Stackoverflow
Solution 2 - RJamesView Answer on Stackoverflow
Solution 3 - RDean EcklesView Answer on Stackoverflow
Solution 4 - RvishalView Answer on Stackoverflow
Solution 5 - RdougView Answer on Stackoverflow
Solution 6 - Rszymon-mView Answer on Stackoverflow
Solution 7 - RYouness BAHIView Answer on Stackoverflow