How to get row index number in R?

前端 未结 6 1226
无人共我
无人共我 2021-01-31 13:46

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.

6条回答
  •  旧时难觅i
    2021-01-31 14:22

    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.

提交回复
热议问题