r - Selecting and filtering on the same variables in dplyr -
let's have matrix 3 columns , 100 rows. let column names a_dem
, b_dem
, c_blah
. , let's imagine each cell can have value between 0 , 100.
is there way use select()
, filter()
, %>%
select observations end "_dem" , have value larger than, say, 50?
i would've kinda imagined along these lines:
dat %>% select(ends_with("dem")) %>% filter(>50) %>% summary()
but doesn't work, obviously.
so, there way kind of selection , filtering, or have resort more complicated?
you this:
library(dplyr) set.seed(2) a_dem <- runif(100,0,100) b_dem <- runif(100,0,100) c_blah <- runif(100,0,100) dat <- data.frame(a_dem, b_dem, c_blah) newdat1 <- dat %>% select(ends_with("_dem")) filtered <- sapply(newdat1, function(x) ifelse(x>50, x, na)) >head(filtered) a_dem b_dem [1,] na na [2,] 70.23740 na [3,] 57.33263 98.06000 [4,] na 82.89221 [5,] 94.38393 na [6,] 94.34750 59.59169
and depending on want next exclude na
values.
update:
to in dplyr
can use method linked here @sgp667
newdat2 <- dat %>% select(ends_with("_dem")) %>% mutate_each(funs(((function(x){ifelse(x>50, x, na)})(.)))) > head(newdat2) a_dem b_dem 1 na na 2 70.23740 na 3 57.33263 98.06000 4 na 82.89221 5 94.38393 na 6 94.34750 59.59169
Comments
Post a Comment