Elizabeth Smith - 8 months ago 56

R Question

I have a dataframe with numeric variables and categorical factors and i want to separate the dataframe into 2: one with the numeric variables and one with the categorical factors.I am new to R and i am lost.I have tried the split function but i can't work it out.I tried using for loops for checking the variables with is.factor but nothing seems to work!Please help!

Answer

As there are only two types ('numeric' and 'factor'), we get the `class`

of column types by looping through the dataset columns (`sapply(df1, class)`

) and use that to `split`

the data.frame into a `list`

of 'data.frame's.

```
lst <- split.default(df1, sapply(df1, class))
```

```
set.seed(24)
df1 <- data.frame(Col1 = LETTERS[1:5], Col2 = as.numeric(1:5), Col3= rnorm(5))
```

Source (Stackoverflow)