aesthetistician (aesthetistician) wrote in stat_geeks,

Taking out selected rows of a data frame in R

I have the following dataframe in R, data1:
  v1 v2  v3
a  1 1.1
a  3 1.6
a  4 4.8
b  1 4.1
b  5 2.6
c  2 3.2
c  6 5.4
c  2 1.8

I want to create a new dataframe with only the rows where v1 has the value "b". That is,
v1 v2  v3
b  1 4.1
b  5 2.6

There should be a straightforward, simple way to do that, right? But the only thing I can get to work at all is
v1 <- data1$v1[data1$v1 == "b"]
v2 <- data1$v2[data1$v1 == "b"]
v3 <- data1$v3[data1$v1 == "b"]
data2 <- data.frame(v1, v2, v3)

Can anyone help me out?
  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.