| 1 | #include <R.h> |
| 2 | #include <Rdefines.h> |
| 3 | #include <string.h> |
| 4 | #include <Rmath.h> |
| 5 | #include <stdio.h> |
| 6 | #include <Rinternals.h> |
| 7 | |
| 8 | SEXP numeric_if_possible(SEXP x){ |
| 9 | int real_works = 1, int_works = 1, i; |
| 10 | const char *elem; |
| 11 | char *end; |
| 12 | for(i = 0; i < LENGTH(x); i++){ |
| 13 | elem = CHAR(STRING_ELT(x,i)); |
| 14 | strtol(elem,&end,10); |
| 15 | if(strlen(end)>0) int_works = 0; |
| 16 | strtod(elem,&end); |
| 17 | if(strlen(end)>0) { |
| 18 | real_works=0; |
| 19 | break; |
| 20 | } |
| 21 | } |
| 22 | if(int_works) |
| 23 | return AS_INTEGER(x); |
| 24 | else if(real_works) |
| 25 | return AS_NUMERIC(x); |
| 26 | else return duplicate(x); |
| 27 | } |
| 28 | |
| 29 | |
| 30 |