Hi I have a dataset that I have transformed to only nums and factors, I want to separate the factors columns from the numerical columns. I'm new to coding and its probably very simple but I've looked around and I'm very thankful if anyone could help
'data.frame': 400 obs. of 25 variables:
$ age : num 48 7 62 48 51 60 68 24 52 53 ...
$ bp : num 80 50 80 70 80 90 70 76 100 90 ...
$ sg : num 1.02 1.02 1.01 1 1.01 ...
$ al : num 1 4 2 4 2 3 0 2 3 2 ...
$ su : num 0 0 3 0 0 0 0 4 0 0 ...
$ rbc : Factor w/ 2 levels "1","0": 1 1 1 1 1 1 1 1 1 2 ...
$ pc : Factor w/ 2 levels "1","0": 1 1 1 2 1 1 1 2 2 2 ...
$ pcc : Factor w/ 2 levels "1","0": 2 2 2 1 2 2 2 2 1 1 ...
$ ba : Factor w/ 2 levels "1","0": 2 2 2 2 2 2 2 2 2 2 ...
$ bgr : num 121 148 423 117 106 74 100 410 138 70 ...
$ bu : num 36 18 53 56 26 25 54 31 60 107 ...
$ sc : num 1.2 0.8 1.8 3.8 1.4 1.1 24 1.1 1.9 7.2 ...
$ sod : num 137 137 137 111 137 142 104 137 137 114 ...
$ pot : num 4 4 4 2.5 4 3.2 4 4 4 3.7 ...
$ hemo : num 15.4 11.3 9.6 11.2 11.6 12.2 12.4 12.4 10.8 9.5 ...
$ pcv : num 44 38 31 32 35 39 36 44 33 29 ...
$ wc : num 7800 6000 7500 6700 7300 ...
$ rc : num 5.2 4 4 3.9 4.6 4.4 4 5 4 3.7 ...
$ htn : Factor w/ 2 levels "1","0": 1 2 2 1 2 1 2 2 1 1 ...
$ dm : Factor w/ 2 levels "1","0": 1 2 1 2 2 1 2 1 1 1 ...
$ cad : Factor w/ 2 levels "1","0": 2 2 2 2 2 2 2 2 2 2 ...
$ appet : Factor w/ 2 levels "1","0": 2 2 1 1 2 2 2 2 2 1 ...
$ pe : Factor w/ 2 levels "1","0": 2 2 2 1 2 1 2 1 2 2 ...
$ ane : Factor w/ 2 levels "1","0": 2 2 1 1 2 2 2 2 1 1 ...
$ classification: Factor w/ 2 levels "1","0": 1 1 1 1 1 1 1 1 1 1 ...
CodePudding user response:
Create a boolean vector fac
with information which is factor, then cbind
subsets. Example:
dat
# F1 N2 N1 F2 N3 F3 N4 F4
# 1 A 4 1 D 7 G 10 J
# 2 B 5 2 E 8 H 11 K
# 3 C 6 3 F 9 I 12 L
fac <- sapply(dat, is.factor)
cbind(dat[!fac], dat[fac])
# N2 N1 N3 N4 F1 F2 F3 F4
# 1 4 1 7 10 A D G J
# 2 5 2 8 11 B E H K
# 3 6 3 9 12 C F I L
Data
dat <- structure(list(F1 = structure(1:3, levels = c("A", "B", "C"), class = "factor"),
N2 = 4:6, N1 = 1:3, F2 = structure(1:3, levels = c("D", "E",
"F"), class = "factor"), N3 = 7:9, F3 = structure(1:3, levels = c("G",
"H", "I"), class = "factor"), N4 = 10:12, F4 = structure(1:3, levels = c("J",
"K", "L"), class = "factor")), row.names = c(NA, -3L), class = "data.frame")