Why doesn't this work with data.table
?
It works with data.frame
. Is there a way to do this with a data table?
x <- data.table(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20])
y <- x[ , sapply(x, is.numeric)]
This returns:
v1 v2 v3 v4
TRUE TRUE TRUE FALSE
To select columns that are only of numeric datatype from a Pandas DataFrame, call DataFrame. select_dtypes() method and pass np. number or 'number' as argument for include parameter.
To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.
To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.
From data.table 1.13.0
".SDcols
accepts a function which is used to select the columns of .SD
". Thus, simply .SDcols = is.numeric
:
x[ , .SD, .SDcols = is.numeric]
data.table
needs the with=FALSE
to grab column numbers.
tokeep <- which(sapply(x,is.numeric))
x[ , tokeep, with=FALSE]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With