abbassix
abbassix

Reputation: 645

split data frame in R w.r.t. column data classes

Suppose I have a data frame with some columns with different data classes. Some are character, some integers, etc. I want to split the data frame into several data frames each containing only one specific data class.

Upvotes: 0

Views: 63

Answers (1)

Yuriy Saraykin
Yuriy Saraykin

Reputation: 8880

CLASS <- sapply(iris, class)
split.default(iris, CLASS)
#> $factor
#>        Species
#> 1       setosa
#> 2       setosa
#> 3       setosa
#> 4       setosa
#> 5       setosa
#> 6       setosa

#> 
#> $numeric
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1            5.1         3.5          1.4         0.2
#> 2            4.9         3.0          1.4         0.2
#> 3            4.7         3.2          1.3         0.2
#> 4            4.6         3.1          1.5         0.2
#> 5            5.0         3.6          1.4         0.2
#> 6            5.4         3.9          1.7         0.4

Created on 2021-06-21 by the reprex package (v2.0.0)

Upvotes: 2

Related Questions