I want to split a dataframe into df1 and df2 based on data type numeric and integer.
The df1 and df2 will respectively contain "bmi", "age", "cal", "pro" and "male", "urban".
sapply(df, class)
Out:
bmi age cal pro male
"numeric" "numeric" "numeric" "numeric" "integer"
urban
"integer"
str(df)
Out:
'data.frame': 4825 obs. of 6 variables:
$ bmi : num 24.7 25.3 22.8 21.7 24.2 ...
$ age : num 37.5 36.5 37 36.9 40 ...
$ cal : num 6.31 1.84 3.87 2.67 1.59 ...
$ pro : num 13.57 13.43 9.57 12.51 11.65 ...
$ male : int 1 0 1 0 1 0 0 1 1 0 ...
$ urban: int 1 1 1 1 1 1 1 1 1 1 ...
How can I do that in R? Thank you.
I try with unlist(lapply(df, is.numeric)), but it doesn't distingue numeric and integer.
Update:
With the code below I get a dataframe but it's not a subset of df, another problem is interger columns included:
numeric <- which(sapply(df,is.numeric))
df1 <- as.data.frame(numeric)
print(df1)
Out:

split.default(df, sapply(df, class)). Uselist2envif you want to get it out of alistand into your global environment.df1anddf2?list2env(your_list, .GlobalEnv). You'll probably want to rename the items in thelistbefore you do that.out <- split.default(df, sapply(df, class)). 2)names(out) <- paste0('df', seq_along(out))3)list2env(out, .GlobalEnv)class.