I'm playing around with flights
database which is included in the nycflights13
library and I thought it may be interesting to select one (or any number) of variables by hand and then some other by condition (for example "carrier" and then all numeric variables).
This can be done in three steps, for example:
library(nycflights13)
data(flights)
flights
a <- flights[,"carrier", drop=FALSE]
b <- flights[, lapply(flights,is.numeric) == TRUE, drop=FALSE]
ab <- cbind(a,b)
str(ab) # 'data.frame': 336776 obs. of 15 variables:
But this doesn't work:
flights[, "carrier" & c(lapply(flights,is.numeric)) == TRUE, drop=FALSE]
flights[, "carrier" & lapply(flights,is.numeric) == TRUE, drop=FALSE]
Error in "carrier" & lapply(flights, is.numeric) == TRUE :
solo son posibles operaciones para variables de tipo numérico, compleja o lógico
I must say select_if
from tidyverse
is not useful either.
So my question is: is it possible to achieve what I want to do in one call and how can it be done? Thanks for any comment or suggestion
Instead of
lapply
, we can usesapply
. It will give a logical output, use that to extract the names or usewhich
, and then concatenate with the 'carrier'The
drop
is not needed fortibble
as by default it won't dropWith
dplyr
one option is