返回数据框中列中特定值的行号

pkg*_*7x7 35 numbers row r dataframe

我有一个数据帧(df),我想知道如何返回的行数(个),一个特定的值(2585)4th column (height_chad1)相同的数据帧的?

我试过了:

row(mydata_2$height_chad1, 2585)
Run Code Online (Sandbox Code Playgroud)

我得到以下错误:

Error in factor(.Internal(row(dim(x))), labels = labs) : 
  a matrix-like object is required as argument to 'row'
Run Code Online (Sandbox Code Playgroud)

是否存在适用于数据帧而不是类似矩阵对象的等效代码行?

任何帮助,将不胜感激.

Set*_*thB 60

使用 which(mydata_2$height_chad1 == 2585)

简短的例子

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
Run Code Online (Sandbox Code Playgroud)

正如Matt Weller指出的那样,你可以使用这个length功能.该count函数plyr可用于返回每一个独特的列值的计数.

  • 你可以在Seth提供的答案结果上使用`length`,或者在你的建议中使用`sum`而不是`count`.您是否按照建议尝试了`count`功能?这是一个简单的测试,没有破坏任何东西的危险......! (2认同)