Return row number(s) for a particular value in a column in a dataframe

前端 未结 2 1622
暖寄归人
暖寄归人 2020-12-13 03:39

I have a data frame (df) and I was wondering how to return the row number(s) for a particular value (2585) in the 4th column (height_chad1)

相关标签:
2条回答
  • 2020-12-13 04:35

    Use which(mydata_2$height_chad1 == 2585)

    Short example

    df <- data.frame(x = c(1,1,2,3,4,5,6,3),
                     y = c(5,4,6,7,8,3,2,4))
    df
      x y
    1 1 5
    2 1 4
    3 2 6
    4 3 7
    5 4 8
    6 5 3
    7 6 2
    8 3 4
    
    which(df$x == 3)
    [1] 4 8
    
    length(which(df$x == 3))
    [1] 2
    
    count(df, vars = "x")
      x freq
    1 1    2
    2 2    1
    3 3    2
    4 4    1
    5 5    1
    6 6    1
    
    df[which(df$x == 3),]
      x y
    4 3 7
    8 3 4
    

    As Matt Weller pointed out, you can use the length function. The count function in plyr can be used to return the count of each unique column value.

    0 讨论(0)
  • 2020-12-13 04:35

    which(df==my.val, arr.ind=TRUE)

    0 讨论(0)
提交回复
热议问题