简体   繁体   中英

Converting factor type columns to numeric in R

I have a lot of factor type columns but they hold numeric values. (Not sure why would R not recognise those as numeric/int). Dataframe looks like this:

> str(gran)
'data.frame':   1368 obs. of  1145 variables:
 $ Patch                                                        : Factor w/ 4 levels "Scotland","North East",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ GM                                                           : Factor w/ 4 levels "BVG1","BVG2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Day                                                          : Factor w/ 7 levels "Fri","Mon","Sat",..: 5 7 6 2 4 3 1 5 7 6 ...
 $ Date                                                         : Date, format: "2014-11-06" "2014-11-05" "2014-11-04" "2014-11-03" ...
 $ X24K.Equivalent.Plan                                         : num  28.9 32.3 34.3 34.8 31 30.5 25.7 27.7 31.9 34.8 ...
 $ X24K.Equivalent.Act                                          : num  30.8 34.5 36.6 36.2 32.6 31.8 33 35.4 39.2 38.5 ...
 $ Plan.Rep.WS                                                  : int  2453 2742 2917 2957 2634 2595 2185 2354 2710 2959 ...
 $ Act.Rep.WS                                                   : int  2621 2929 3115 3077 2768 2703 2803 3009 3334 3275 ...
 $ Rep.WS.Var                                                   : int  168 187 198 120 134 108 618 655 624 316 ...
 $ Plan.CAL.Rep.WS                                              : int  878 945 950 876 764 752 683 760 875 926 ...
 $ Act.CAL.Rep.WS                                               : int  734 844 926 931 846 799 786 905 1020 969 ...
 $ CAL.Rep.WS.Var                                               : int  -144 -101 -24 55 82 47 103 145 145 43 ...
 $ Plan.LTOK.Rep.WS                                             : int  298 365 394 382 328 302 290 311 375 417 ...
 $ Act.LTOK.Rep.WS                                              : int  387 434 432 420 344 325 350 381 443 452 ...
 $ LTOK.Rep.WS.Var                                              : int  89 69 38 38 16 23 60 70 68 35 ...
 $ Plan.BRBD.Rep.WS                                             : int  602 650 722 822 692 621 591 627 682 744 ...
 $ Act.BRBD.Rep.WS                                              : int  689 728 784 833 702 605 675 689 731 785 ...
 $ BRBD.Rep.WS.Var                                              : int  87 78 62 11 10 -16 84 62 49 41 ...
 $ Plan.LN.Rep.WS                                               : int  598 711 775 774 768 824 562 589 705 793 ...
 $ Act.LN.Rep.WS                                                : num  717 820 877 789 789 ...
 $ LN.Rep.WS.Var                                                : num  119 109 102 15 21 35 313 319 317 168 ...
 $ Plan.FTTC.Rep.WS                                             : num  77 71 76 103 82 96 59 66 73 79 ...
 $ Act.FTTC.Rep.WS                                              : num  94 103 96 104 87 115 117 126 118 108 ...
 $ FTTC.Rep.WS.Var                                              : num  17 32 20 1 5 19 58 60 45 29 ...
 $ Plan.FTTP.Rep.WS                                             : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Act.FTTP.Rep.WS                                              : num  0 0 0 0 0 0 0 0 0 0 ...
 $ FTTP.Rep.WS.Var                                              : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Plan.Rep.Intakes                                             : num  1073 1074 1094 1134 463 ...
 $ Act.Rep.Intakes                                              : num  265 49 72 111 162 213 93 78 74 126 ...
 $ Rep.Intakes.Var                                              : num  210 199 148 189 41 115 60 92 66 121 ...
 $ Plan.CAL.Rep.Intakes                                         : num  373 371 379 398 175 223 441 444 433 442 ...
 $ Act.CAL.Rep.Intakes                                          : num  215 126 112 141 12 71 143 90 129 191 ...
 $ CAL.Rep.Intakes.Var                                          : Factor w/ 493 levels "-1","-10","-100",..: 167 123 26 55 32 122 51 8 61 111 ...
 $ Plan.LTOK.Rep.Intakes                                        : num  138 125 130 140 62 89 152 158 139 144 ...
 $ Act.LTOK.Rep.Intakes                                         : Factor w/ 442 levels "100","101","102",..: 124 49 58 33 107 98 25 44 42 36 ...
 $ LTOK.Rep.Intakes.Var                                         : Factor w/ 359 levels "-1","-10","-11",..: 90 68 73 47 61 13 20 6 74 49 ...
 $ Plan.BRBD.Rep.Intakes                                        : num  205 214 218 208 130 149 210 217 218 216 ...
 $ Act.BRBD.Rep.Intakes                                         : Factor w/ 506 levels "100","104","105",..: 156 87 114 127 29 45 85 103 91 96 ...
 $ BRBD.Rep.Intakes.Var                                         : Factor w/ 459 levels "-1","-10","-11",..: 104 40 54 83 93 48 38 99 40 45 ...
 $ Plan.LN.Rep.Intakes                                          : num  313 315 319 345 73 101 334 335 324 328 ...
 $ Act.LN.Rep.Intakes                                           : Factor w/ 644 levels "101","104","106",..: 209 93 97 147 177 206 137 145 112 161 ...
 $ LN.Rep.Intakes.Var                                           : Factor w/ 310 levels "-1","-10","-105",..: 147 10 5 107 10 45 99 115 96 146 ...
 $ Plan.FTTC.Rep.Intakes                                        : num  44 49 48 43 23 33 43 40 42 44 ...
 $ Act.FTTC.Rep.Intakes                                         : Factor w/ 241 levels "-1","10","11",..: 59 46 51 35 15 21 53 41 54 49 ...
 $ FTTC.Rep.Intakes.Var                                         : Factor w/ 104 levels "-1","-10","-11",..: 41 30 40 8 1 13 28 38 32 20 ...
 $ Plan.FTTP.Rep.Intakes                                        : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Act.FTTP.Rep.Intakes                                         : Factor w/ 12 levels "-1","-3","0",..: 6 3 3 3 3 4 3 3 3 3 ...
 $ FTTP.Rep.Intakes.Var                                         : Factor w/ 348 levels "-1","-3","0",..: 6 3 3 3 3 4 3 3 3 3 ...
 $ Plan.Rep.Comps                                               : num  1152 1363 1269 1174 140 ...
 $ Act.Rep.Comps                                                : Factor w/ 1139 levels "1","100","1000",..: 272 165 151 72 159 212 125 156 172 120 ...
 $ Rep.Comps.Var                                                : Factor w/ 514 levels "-1","-10","-100",..: 193 154 162 68 114 48 73 93 122 54 ...
 $ Plan.Rep.Comps.incl.Others                                   : int  1152 1363 1269 1174 140 556 1265 1363 1512 1424 ...
 $ Act.Rep.Comps.incl.Others                                    : Factor w/ 430 levels "","100","1001",..: 118 82 73 43 80 99 66 79 86 64 ...
 $ Rep.Comps.Var.incl.Others                                    : Factor w/ 702 levels "","-1","-10",..: 213 170 173 63 51 15 15 34 56 21 ...
 $ Plan.Rep.Comps_DL                                            : num  1079 1303 1203 1116 127 ...
 $ Act.Rep.Comps_DL                                             : Factor w/ 1111 levels "-100","-103",..: 286 180 170 123 169 215 158 174 184 153 ...
 $ Rep.Comps.Var_DL                                             : Factor w/ 707 levels "-1","-10","-100",..: 150 123 129 10 46 2 48 76 92 37 ...
 $ Plan.Rep.Comps_MWF                                           : num  36 33 36 16 9 19 25 17 23 17 ...
 $ Act.Rep.Comps_MWF                                            : Factor w/ 443 levels "-1","-10","-11",..: 85 29 40 22 69 18 31 42 35 40 ...
 $ Rep.Comps.Var_MWF                                            : Factor w/ 435 levels "-1","-10","-11",..: 84 3 17 23 15 20 1 32 67 30 ...
 $ Plan.Rep.Comps_APPRT                                         : num  36 28 30 43 3 6 23 28 28 38 ...
 $ Act.Rep.Comps_APPRT                                          : Factor w/ 280 levels "-1","-10","-12",..: 77 43 42 46 49 68 44 48 57 42 ...
 $ Rep.Comps.Var_APPRT                                          : Factor w/ 214 levels "","-1","-10",..: 49 23 26 9 29 2 39 30 48 8 ...
 $ Act.Rep.Comps_Others                                         : Factor w/ 469 levels "","0","1","10",..: 133 6 129 20 3 38 15 15 6 131 ...
 $ Plan.CAL.Rep.Comps                                           : num  363 438 384 324 63 212 463 522 547 494 ...
 $ Act.CAL.Rep.Comps                                            : Factor w/ 937 levels "-1","-101","-11",..: 247 206 176 138 233 78 128 179 209 150 ...
 $ CAL.Rep.Comps.Var                                            : Factor w/ 448 levels "","-1","-10",..: 123 102 108 113 2 3 67 65 53 68 ...
 $ Plan.CAL.Rep.Comps.incl.Others                               : int  363 438 384 324 63 212 463 522 547 494 ...
 $ Act.CAL.Rep.Comps.incl.Others                                : Factor w/ 362 levels "","166","174",..: 111 86 71 53 105 10 43 73 89 55 ...
 $ CAL.Rep.Comps.Var.incl.Others                                : Factor w/ 644 levels "","-1","-10",..: 192 146 173 184 2 43 46 45 36 48 ...
 $ Plan.CAL.Rep.Comps_DL                                        : num  307 397 329 282 52 186 429 493 515 457 ...
 $ Act.CAL.Rep.Comps_DL                                         : Factor w/ 905 levels "-1","-100","-102",..: 237 203 177 142 219 71 135 174 201 144 ...
 $ CAL.Rep.Comps.Var_DL                                         : Factor w/ 656 levels "-1","-10","-103",..: 121 107 116 117 59 101 51 56 46 57 ...
 $ Plan.CAL.Rep.Comps_MWF                                       : num  26 21 26 7 7 19 17 8 11 9 ...
 $ Act.CAL.Rep.Comps_MWF                                        : Factor w/ 355 levels "-1","-10","-11",..: 66 22 27 20 62 63 20 31 25 37 ...
 $ CAL.Rep.Comps.Var_MWF                                        : Factor w/ 419 levels "-1","-10","-11",..: 68 20 20 44 1 4 18 25 54 30 ...
 $ Plan.CAL.Rep.Comps_APPRT                                     : num  29 21 29 36 3 6 17 21 21 29 ...
 $ Act.CAL.Rep.Comps_APPRT                                      : Factor w/ 245 levels "-1","-10","-11",..: 61 28 29 33 46 55 29 36 40 31 ...
 $ CAL.Rep.Comps.Var_APPRT                                      : Factor w/ 205 levels "","-1","-10",..: 45 29 8 11 31 13 22 2 38 6 ...
 $ Act.CAL.Rep.Comps_Others                                     : Factor w/ 317 levels "","0","1","10",..: 89 70 71 4 2 70 81 72 71 56 ...
 $ Plan.LTOK.Rep.Comps                                          : num  147 192 159 128 8 63 164 179 203 186 ...
 $ Act.LTOK.Rep.Comps                                           : Factor w/ 521 levels "-1","-100","-102",..: 174 133 105 70 141 146 98 120 135 93 ...
 $ LTOK.Rep.Comps.Var                                           : Factor w/ 258 levels "","-1","-10",..: 79 70 13 44 32 6 7 32 49 33 ...
 $ Plan.LTOK.Rep.Comps.incl.Others                              : int  147 192 159 128 8 63 164 179 203 186 ...
 $ Act.LTOK.Rep.Comps.incl.Others                               : Factor w/ 207 levels "","0","1","100",..: 84 57 39 13 63 68 33 50 59 30 ...
 $ LTOK.Rep.Comps.Var.incl.Others                               : Factor w/ 352 levels "","-1","-10",..: 132 113 13 32 25 6 7 25 36 27 ...
 $ Plan.LTOK.Rep.Comps_DL                                       : num  132 176 149 115 6 63 154 165 187 170 ...
 $ Act.LTOK.Rep.Comps_DL                                        : Factor w/ 487 levels "-1","-10","-103",..: 168 122 100 66 131 132 90 110 123 92 ...
 $ LTOK.Rep.Comps.Var_DL                                        : Factor w/ 248 levels "-1","-10","-11",..: 76 70 40 36 12 10 13 42 30 25 ...
 $ Plan.LTOK.Rep.Comps_MWF                                      : num  9 10 9 7 2 0 6 8 10 8 ...
 $ Act.LTOK.Rep.Comps_MWF                                       : Factor w/ 125 levels "-1","-10","-11",..: 32 27 28 25 9 26 29 31 11 26 ...
 $ LTOK.Rep.Comps.Var_MWF                                       : Factor w/ 175 levels "-1","-10","-11",..: 43 13 11 12 9 37 19 19 18 12 ...
 $ Plan.LTOK.Rep.Comps_APPRT                                    : num  6 6 1 6 0 0 4 6 6 8 ...
 $ Act.LTOK.Rep.Comps_APPRT                                     : Factor w/ 98 levels "-1","-10","-11",..: 35 18 33 33 16 17 34 33 19 29 ...
 $ LTOK.Rep.Comps.Var_APPRT                                     : Factor w/ 90 levels "","-1","-13",..: 20 16 19 14 12 13 17 14 17 7 ...
 $ Act.LTOK.Rep.Comps_Others                                    : Factor w/ 286 levels "","0","1","10",..: 85 2 2 2 2 2 2 2 2 2 ...
 $ Plan.BRBD.Rep.Comps                                          : num  239 262 290 308 0 78 238 253 273 278 ...
 $ Act.BRBD.Rep.Comps                                           : Factor w/ 557 levels "-1","-10","-11",..: 185 96 139 145 164 167 124 88 102 119 ...
 $ BRBD.Rep.Comps.Var                                           : Factor w/ 219 levels "","-1","-10",..: 93 9 58 54 87 8 82 10 14 54 ...
  [list output truncated]

As you can see I have about 1145 variables. It is difficult to find and change the type one by one to numeric type. Also if there is "" or ... or .. etc. in the variable as a value I would like it to show as missing or NA. Is there an easy way for doing this?

Try

indx <- !sapply(gran, function(x) all(grepl('[A-Za-z]', x))) &
                                         !sapply(gran, is.numeric)

gran[indx] <- lapply(gran[indx], function(x) as.numeric(as.character(x)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM