Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # generate some example data
- set.seed(1234)
- df <- data.frame(
- col1 = rbinom(100, 1, 0.5),
- col2 = rbinom(100, 1, 0.5),
- col3 = rbinom(100, 1, 0.5),
- col4 = rbinom(100, 1, 0.5),
- col5 = rbinom(100, 1, 0.5),
- col6 = rbinom(100, 1, 0.5))
- # the long, explicit way with base R
- df[df$col2 == 1 & df$col4 == 1 & df$col6 == 1, ]
- # or, equivalently with "subset"
- subset(df, col2 == 1 & col4 == 1 & col6 == 1)
- # or with "with"
- with(df, df[col2 == 1 & col4 == 1 & col6 == 1, ])
- # or by changing df to a data.table
- library(data.table)
- setDT(df)
- df[col2 == 1 & col4 == 1 & col6 == 1]
- # or via dplyr / tidyverse
- library(dplyr)
- df %>% filter(col2 == 1, col4 == 1, col6 == 1)
- # or if you want to use lots of bad coding practices all at once :)
- attach(df)
- sort(which(col2 * col4*col6== 1)) ->my_fav.Rows
- df[ my_fav.Rows,names(df)]
Add Comment
Please, Sign In to add comment