问题
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