In R, how can I filter a data frame by data type using dplyr?

Multi tool use


In R, how can I filter a data frame by data type using dplyr?
I am still learning R and would really appreciate it if someone could show me a simple way to filter a data frame by data type (i.e. only factors) using dplyr so that the output is just a list of variables of the chosen data type?
Thanks in advance!
EDIT:
As it was kindly pointed out, I am missing an example (first post, sorry!). I am trying to do something like the following:
df %>%
filter(typeof(.) == "integer") %>%
names()
The above just returns all of the variables in my data frame, not just those of type integer which is what I would like. I would like to be able to filter for other data types as well, not just integers :)
Presumably something along the lines of
%>% select( is.factor(.) )
but you have not done your reading of the SO "manual" and your question has no Minimal, Complete, and Verifiable example– 42-
yesterday
%>% select( is.factor(.) )
Are you only after factors, or did you want a solution which works for any given data type? You've said i.e. only factors but I suspect you mean e.g. only factors?
– Mike S
yesterday
Sorry, it's my first SO post - I'll edit the question accordingly! And Mike you are right, I am looking for a solution that could be used/altered slightly for any data type :)
– Sean
yesterday
@Tjebo thank you for the clarification on using
select
and filter
, indeed, this is likely why I was having trouble!– Sean
19 hours ago
select
filter
1 Answer
1
I would do like this (package agnostic) using base R:
get_names = names(df)[sapply(df, is.factor)]
df = df[,get_names]
In dplyr
, you can do:
dplyr
df <- df %>%
select_if(is.factor)
Hi YOLO, thank you for your answer! Unless somebody provides an answer using dplyr (which would be most helpful to me), then I will mark your answer as correct since it also does what I was looking for! :)
– Sean
yesterday
@Sean added to answer.
– YOLO
yesterday
I am confused. The OP asked for
filter
, but this is select
...? I guess select is what the OP wanted from the very start...– Tjebo
19 hours ago
filter
select
Sorry for confusing you @Tjebo, as I said, this was my first post so still getting to grips with SO way of things! Even though YOLO's answer has used
select_if
their answer has still filtered (as in to "process or assess (items) in order to reject those that are unwanted") by data type, which is what I was looking for. I will, however, be sure to word my future questions more carefully :)– Sean
19 hours ago
select_if
@Sean haha no worries, this was German humour trying to point out a mixed up terminology (as pointed out in my comment above, to your question)
– Tjebo
19 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I don't know if that would help you, but here you can see by column type: markhneedham.com/blog/2014/09/29/…
– Felipe Augusto
yesterday