I have a data frame (a csv file) with dimensions 100x6 and I need only the columns c("X1", "X2", "X4") and the rows in which the value of "X1" is greater than 30. So I did:

  data_frame <- read.csv ("data_frame")
  data_frame [c("X1", "X2", "X4")]

The column subset problem is solved but now I need to subset rows from data_frame [c("X1", "X2", "X4")] where the values of "X1" is greater than 30. I tried:

  data_frame [c("X1" > 30), c("X1", "X2", "X4")] 

But it returned the same data frame as data_frame [c("X1", "X2", "X4")]. Also tried using the function subset() with the same approach but got the same results.

  • $\begingroup$ Just because you are doing data science doesn't mean you have to ask your question here. This question has already been answered in Stackoverflow here and in several other questions (so you could have had your answer imediately): stackoverflow.com/questions/27217331/…. $\endgroup$
    – Ed Fine
    Commented May 20, 2015 at 13:58
  • $\begingroup$ data_frame %>% head(30) That will print the first 30 rows of your data set. $\endgroup$ Commented Nov 10, 2018 at 14:38

1 Answer 1


You want

data_frame[data_frame$X1 > 30, c("X1","X2","X4")]

that will just print it, you probably want to update data_frame or store it in something else:

data_frame = data_frame[data_frame$X1 > 30, c("X1","X2","X4")]

also you probably want to try asking this on StackOverflow, or reading a bit more basic R documentation because it should be well covered. Its a bit simple to be "data science".


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.