I have the following data frame:
a<-sample(1:300, 10, T)
b<-sample(c("SI","NO"), 10, T)
d<-sample(c(T,F), 10, T)
df<-data.frame(a,b,d)
As you can see they are of different classes.
sapply(df, class)
a b d e
"integer" "factor" "logical" "factor"
My question is what function do I use to keep only the integers.
Thanks in advance.
Well, you almost have it solved,
sapply(df, class)
return a vector with the classes of each column, simply compare it with the string"integer"
and use that comparison in the column selection to generate the new onedata.frame
:Important: Column selection, by default, coerces the result, when a single column is selected, to a vector. With
drop=FALSE
we make sure that the return of the selection remains adata.frame
.