# How to subset rows from a data frame with comparison operators in R

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.

• 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/…. – Ed Fine May 20 '15 at 13:58
• data_frame %>% head(30) That will print the first 30 rows of your data set. – dhiraj kumar Nov 10 '18 at 14:38

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".