How to subset a matrix by column factor values?

喜夏-厌秋 提交于 2019-12-11 20:16:37

问题


I've prepared a matrix called 'grades', but I fail to select all pass cases (pass = 2).

>      essay exam final pass
> Ann     4   20    12    2
> Ben     6   10     8    1
> Cox     8   16    12    2
> Dan    11   17    14    2
> Eos     3   13     8    1

When I tried this, it returns an error

> grades[grades$pass == 2,]

Error in grades$pass : $ operator is invalid for atomic vectors

I understand the matrix is not a data frame, so the error occurred. I then made a data frame.

> grades.df <- data.frame(grades)
> is.data.frame(grades.df)

[1] TRUE

But still failed after I typed the following syntax:

> grades.df[grades.df$pass = 2]

Thank you for helping me answer how to extract all rows with factor 2:

>      essay exam final pass
> Ann     4   20    12    2
> Cox     8   16    12    2
> Dan    11   17    14    2

来源:https://stackoverflow.com/questions/27755737/how-to-subset-a-matrix-by-column-factor-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!