# NOT RUN {
# operations on vector, '%d%' means 'diff'
1:6 %d% greater(4) # 1:4
1:6 %d% (1 | greater(4)) # 2:4
# '%i%' means 'intersect
1:6 %i% (is_min() | is_max()) # 1, 6
# with Excel-style boolean operators
1:6 %i% or(is_min(), is_max()) # 1, 6
letters %i% (contains("a") | contains("z")) # a, z
letters %i% perl("a|z") # a, z
letters %i% from("w") # w, x, y, z
letters %i% to("c") # a, b, c
letters %i% (from("b") & to("e")) # b, d, e
c(1, 2, NA, 3) %i% not_na() # c(1, 2, 3)
# examples with count_if
df1 = data.frame(
a=c("apples", "oranges", "peaches", "apples"),
b = c(32, 54, 75, 86)
)
count_if(greater(55), df1$b) # greater than 55 = 2
count_if(not_equals(75), df1$b) # not equals 75 = 3
count_if(greater(32) & less(86), df1$b) # greater than 32 and less than 86 = 2
count_if(and(greater(32), less(86)), df1$b) # the same result
# infix version
count_if(35 %thru% 80, df1$b) # greater than or equals to 35 and less than or equals to 80 = 2
# values that started on 'a'
count_if(like("a*"), df1) # 2
# the same with Perl-style regular expression
count_if(perl("^a"), df1) # 2
# count_row_if
count_row_if(perl("^a"), df1) # c(1,0,0,1)
# examples with 'n_intersect' and 'n_diff'
data(iris)
iris %>% n_intersect(to("Petal.Width")) # all columns up to 'Species'
# 'Sepal.Length', 'Sepal.Width' will be left
iris %>% n_diff(from("Petal.Length"))
# except first column
iris %n_d% items(1)
# 'recode' examples
qvar = c(1:20, 97, NA, NA)
recode(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, 11 %thru% hi ~ 3, other ~ 0)
# the same result
recode(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, greater_or_equal(11) ~ 3, other ~ 0)
# }
Run the code above in your browser using DataLab