using logical functions to organize data.frames

let's say I have a data set organized in a data.frame object and I use a logical function like dataframe$variable<30 how do I get r to only show me the entries of the dataset for which the outcome of said function is = TRUE, how do I organize only those entries into a new data.frame while otherwise keeping the old data.frame as is?

I know it's probably pretty basic but I'm not an expert and just really need to understand this for my University related work with R.
I'm sorry for the inconvenience and thanks in advance!

Here are two methods. In the first one, the logical condition is used inside of the square brackets that are used to subset a data frame. DF[2,3] give you whatever is in the second row and third column of the data frame and DF[DF$Value < 6, ] gives you all the rows where Value < 6 is TRUE, where Value is the name of a column.
The second method uses the filter function from the dplyr package.

DF <- data.frame(Major = c("A", "A", "B", "B"),
                  Minor = c("X", "Y", "X", "Y"),
                  Value = c(5,6,7,4))
  Major Minor Value
1     A     X     5
2     A     Y     6
3     B     X     7
4     B     Y     4
#method 1
NewDF <- DF[DF$Value < 6, ]
  Major Minor Value
1     A     X     5
4     B     Y     4

#method 2
NewDF2 <- filter(DF, Value < 6)
  Major Minor Value
1     A     X     5
2     B     Y     4
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.