| 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 |
|
| 9 |
|
| 10 |
SEXP update_data_frame (SEXP data, SEXP with, SEXP s_hh, SEXP s_ii, SEXP s_jj, SEXP append){ |
| 11 |
SEXP res, match, matched; |
| 12 |
SEXP x,y = R_NilValue; |
| 13 |
SEXP rownames; |
| 14 |
int i, j, k, l, hh = 0, ii, jj, nn, found; |
| 15 |
if(!inherits(data,"data.frame") || !inherits(with,"data.frame")) |
| 16 |
error("need data.frame arguments"); |
| 17 |
PROTECT(res=duplicate(data)); |
| 18 |
int protectcounter = 1; |
| 19 |
PROTECT(with=AS_LIST(with)); |
| 20 |
protectcounter++; |
| 21 |
int M = LENGTH(VECTOR_ELT(data,0)); |
| 22 |
int N = LENGTH(VECTOR_ELT(with,0)); |
| 23 |
PROTECT(match = NEW_INTEGER(M)); |
| 24 |
protectcounter++; |
| 25 |
PROTECT(matched = NEW_INTEGER(N)); |
| 26 |
protectcounter++; |
| 27 |
PROTECT(s_hh = AS_INTEGER(s_hh)); |
| 28 |
protectcounter++; |
| 29 |
PROTECT(s_ii = AS_INTEGER(s_ii)); |
| 30 |
protectcounter++; |
| 31 |
PROTECT(s_jj = AS_INTEGER(s_jj)); |
| 32 |
protectcounter++; |
| 33 |
PROTECT(append = AS_LOGICAL(append)); |
| 34 |
protectcounter++; |
| 35 |
nn = LENGTH(s_ii); |
| 36 |
if(LENGTH(s_ii)!=LENGTH(s_jj)) error("ii and jj need to have same length"); |
| 37 |
for(i = 0; i < LENGTH(s_hh); i++) |
| 38 |
INTEGER(s_hh)[i]--; |
| 39 |
for(i = 0; i < LENGTH(s_ii); i++) |
| 40 |
INTEGER(s_ii)[i]--; |
| 41 |
for(i = 0; i < LENGTH(s_jj); i++) |
| 42 |
INTEGER(s_jj)[i]--; |
| 43 |
#ifdef DEBUG |
| 44 |
Rprintf("\nres at %d\n",res); |
| 45 |
PrintValue(res); |
| 46 |
Rprintf("\nwith at %d\n",with); |
| 47 |
PrintValue(with); |
| 48 |
Rprintf("\ns_hh\n"); |
| 49 |
PrintValue(s_hh); |
| 50 |
Rprintf("\ns_ii\n"); |
| 51 |
PrintValue(s_ii); |
| 52 |
Rprintf("\ns_jj\n"); |
| 53 |
PrintValue(s_jj); |
| 54 |
#endif |
| 55 |
for(k = 0; k < nn; k++){ |
| 56 |
ii = INTEGER(s_ii)[k]; |
| 57 |
jj = INTEGER(s_jj)[k]; |
| 58 |
x = VECTOR_ELT(data,ii); |
| 59 |
y = VECTOR_ELT(with,jj); |
| 60 |
PROTECT(y = coerceVector(y,TYPEOF(x))); |
| 61 |
SET_VECTOR_ELT(with,jj,y); |
| 62 |
UNPROTECT(1); |
| 63 |
} |
| 64 |
for(i = 0; i < M; i++) INTEGER(match)[i] = -1; |
| 65 |
for(j = 0; j < N; j++){ |
| 66 |
INTEGER(matched)[j] = 0; |
| 67 |
for(i = 0; i < M; i++){ |
| 68 |
found = 1; |
| 69 |
for(k = 0; k < nn; k++){ |
| 70 |
ii = INTEGER(s_ii)[k]; |
| 71 |
jj = INTEGER(s_jj)[k]; |
| 72 |
#ifdef DEBUG |
| 73 |
Rprintf("\ni=%d, j=%d, k=%d, ii=%d, jj=%d",i,j,k,ii,jj); |
| 74 |
#endif |
| 75 |
x = VECTOR_ELT(data,ii); |
| 76 |
y = VECTOR_ELT(with,jj); |
| 77 |
#ifdef DEBUG |
| 78 |
Rprintf("\n TYPEOF(x)=%d, TYPEOF(x)=%d",TYPEOF(x),TYPEOF(y)); |
| 79 |
#endif |
| 80 |
switch(TYPEOF(x)){ |
| 81 |
case LGLSXP: |
| 82 |
found *= (LOGICAL(x)[i]==LOGICAL(y)[j]); |
| 83 |
break; |
| 84 |
case INTSXP: |
| 85 |
found *= (INTEGER(x)[i]==INTEGER(y)[j]); |
| 86 |
break; |
| 87 |
case REALSXP: |
| 88 |
found *= (REAL(x)[i]==REAL(y)[j]); |
| 89 |
break; |
| 90 |
case STRSXP: |
| 91 |
found *= (strcmp(CHAR(STRING_ELT(x,i)),CHAR(STRING_ELT(y,j)))==0); |
| 92 |
break; |
| 93 |
default: error("cannot handle type %d ",TYPEOF(x)); |
| 94 |
} |
| 95 |
} |
| 96 |
if(found){ |
| 97 |
#ifdef DEBUG |
| 98 |
Rprintf("\nFound matching element for %d in %d",j,i); |
| 99 |
#endif |
| 100 |
INTEGER(matched)[j]++; |
| 101 |
INTEGER(match)[i] = j; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
#ifdef DEBUG |
| 106 |
Rprintf("\nmatch\n"); |
| 107 |
PrintValue(match); |
| 108 |
Rprintf("\nmatched\n"); |
| 109 |
PrintValue(matched); |
| 110 |
#endif |
| 111 |
for(k = 0; k < LENGTH(s_hh); k++){ |
| 112 |
hh = INTEGER(s_hh)[k]; |
| 113 |
x = VECTOR_ELT(res,hh); |
| 114 |
y = VECTOR_ELT(with,k); |
| 115 |
#ifdef DEBUG |
| 116 |
Rprintf("\nx\n"); |
| 117 |
PrintValue(x); |
| 118 |
Rprintf("\ny at %d, element %d of 'with' at %d\n",y,k,with); |
| 119 |
PrintValue(y); |
| 120 |
#endif |
| 121 |
for(i = 0; i < M; i++){ |
| 122 |
j = INTEGER(match)[i]; |
| 123 |
if(j >= 0) |
| 124 |
switch(TYPEOF(x)){ |
| 125 |
case LGLSXP: |
| 126 |
LOGICAL(x)[i]=LOGICAL(y)[j]; |
| 127 |
break; |
| 128 |
case INTSXP: |
| 129 |
INTEGER(x)[i]=INTEGER(y)[j]; |
| 130 |
break; |
| 131 |
case REALSXP: |
| 132 |
REAL(x)[i]=REAL(y)[j]; |
| 133 |
break; |
| 134 |
case STRSXP: |
| 135 |
SET_STRING_ELT(x,i,mkChar(CHAR(STRING_ELT(y,j)))); |
| 136 |
break; |
| 137 |
default: error("cannot handle type %d ",TYPEOF(x)); |
| 138 |
} |
| 139 |
} |
| 140 |
#ifdef DEBUG |
| 141 |
Rprintf("\nx\n"); |
| 142 |
PrintValue(x); |
| 143 |
#endif |
| 144 |
} |
| 145 |
|
| 146 |
if(LOGICAL(append)[0]){ |
| 147 |
#ifdef DEBUG |
| 148 |
Rprintf("\nappending unmatched data\n"); |
| 149 |
#endif |
| 150 |
int n_unmatched = 0; |
| 151 |
SEXP unmatched; |
| 152 |
for(k = 0; k < LENGTH(matched); k++){ |
| 153 |
if(INTEGER(matched)[k]==0) n_unmatched++; |
| 154 |
} |
| 155 |
PROTECT(unmatched = NEW_INTEGER(n_unmatched)); |
| 156 |
protectcounter++; |
| 157 |
l = 0; |
| 158 |
for(k = 0; k < LENGTH(matched); k++){ |
| 159 |
if(INTEGER(matched)[k]==0){ |
| 160 |
INTEGER(unmatched)[l] = k; |
| 161 |
l++; |
| 162 |
} |
| 163 |
} |
| 164 |
#ifdef DEBUG |
| 165 |
PrintValue(unmatched); |
| 166 |
Rprintf("\ndoing the appending work now ...filling up NA's\n"); |
| 167 |
#endif |
| 168 |
for(k = 0; k < LENGTH(res); k++){ |
| 169 |
x = VECTOR_ELT(res,k); |
| 170 |
PROTECT(x = lengthgets(x,M+n_unmatched)); |
| 171 |
SET_VECTOR_ELT(res,k,x); |
| 172 |
UNPROTECT(1); |
| 173 |
#ifdef DEBUG |
| 174 |
Rprintf("\nx\n"); |
| 175 |
PrintValue(x); |
| 176 |
#endif |
| 177 |
for(l = M; l < M + n_unmatched; l++){ |
| 178 |
switch(TYPEOF(x)){ |
| 179 |
case LGLSXP: |
| 180 |
LOGICAL(x)[l]=NA_LOGICAL; |
| 181 |
break; |
| 182 |
case INTSXP: |
| 183 |
INTEGER(x)[l]=NA_INTEGER; |
| 184 |
break; |
| 185 |
case REALSXP: |
| 186 |
REAL(x)[l]=NA_REAL; |
| 187 |
break; |
| 188 |
case STRSXP: |
| 189 |
SET_STRING_ELT(x,i,NA_STRING); |
| 190 |
break; |
| 191 |
default: error("cannot handle type %d ",TYPEOF(x)); |
| 192 |
} |
| 193 |
} |
| 194 |
#ifdef DEBUG |
| 195 |
PrintValue(x); |
| 196 |
#endif |
| 197 |
} |
| 198 |
#ifdef DEBUG |
| 199 |
Rprintf("\ndoing the appending work now ...filling up data\n"); |
| 200 |
PrintValue(s_hh); |
| 201 |
PrintValue(with); |
| 202 |
#endif |
| 203 |
for(k = 0; k < LENGTH(s_hh); k++){ |
| 204 |
hh = INTEGER(s_hh)[k]; |
| 205 |
x = VECTOR_ELT(res,hh); |
| 206 |
y = VECTOR_ELT(with,k); |
| 207 |
#ifdef DEBUG |
| 208 |
Rprintf("\nx\n"); |
| 209 |
PrintValue(x); |
| 210 |
Rprintf("\ny at %d, element %d of with at %d\n",y,k,with); |
| 211 |
PrintValue(y); |
| 212 |
#endif |
| 213 |
for(l = 0; l < n_unmatched; l++){ |
| 214 |
i = l + M; |
| 215 |
j = INTEGER(unmatched)[l]; |
| 216 |
#ifdef DEBUG |
| 217 |
Rprintf("i=%d, j=%d\n",i,j); |
| 218 |
#endif |
| 219 |
switch(TYPEOF(x)){ |
| 220 |
case LGLSXP: |
| 221 |
LOGICAL(x)[i]=LOGICAL(y)[j]; |
| 222 |
break; |
| 223 |
case INTSXP: |
| 224 |
INTEGER(x)[i]=INTEGER(y)[j]; |
| 225 |
break; |
| 226 |
case REALSXP: |
| 227 |
REAL(x)[i]=REAL(y)[j]; |
| 228 |
break; |
| 229 |
case STRSXP: |
| 230 |
SET_STRING_ELT(x,i,mkChar(CHAR(STRING_ELT(y,j)))); |
| 231 |
break; |
| 232 |
default: error("cannot handle type %d ",TYPEOF(x)); |
| 233 |
} |
| 234 |
} |
| 235 |
#ifdef DEBUG |
| 236 |
Rprintf("\nx\n"); |
| 237 |
PrintValue(x); |
| 238 |
#endif |
| 239 |
} |
| 240 |
PROTECT(rownames=NEW_INTEGER(M + n_unmatched)); |
| 241 |
protectcounter++; |
| 242 |
for(i = 0; i < M + n_unmatched; i++){ |
| 243 |
INTEGER(rownames)[i] = i + 1; |
| 244 |
} |
| 245 |
PROTECT(rownames = AS_CHARACTER(rownames)); |
| 246 |
protectcounter++; |
| 247 |
setAttrib(res,install("row.names"),rownames); |
| 248 |
} else { |
| 249 |
PROTECT(rownames=NEW_INTEGER(M)); |
| 250 |
protectcounter++; |
| 251 |
for(i = 0; i < M; i++){ |
| 252 |
INTEGER(rownames)[i] = i + 1; |
| 253 |
} |
| 254 |
PROTECT(rownames = AS_CHARACTER(rownames)); |
| 255 |
protectcounter++; |
| 256 |
setAttrib(res,install("row.names"),rownames); |
| 257 |
#ifdef DEBUG |
| 258 |
Rprintf("\nrownames\n"); |
| 259 |
PrintValue(rownames); |
| 260 |
#endif |
| 261 |
} |
| 262 |
for(i = 0; i < LENGTH(match); i++) |
| 263 |
INTEGER(match)[i]++; |
| 264 |
setAttrib(res,install("match"),match); |
| 265 |
setAttrib(res,install("matched"),matched); |
| 266 |
#ifdef DEBUG |
| 267 |
Rprintf("\nmatch\n"); |
| 268 |
PrintValue(match); |
| 269 |
Rprintf("\nmatched\n"); |
| 270 |
PrintValue(matched); |
| 271 |
for(k = 0; k < LENGTH(res); k++){ |
| 272 |
x = VECTOR_ELT(res,k); |
| 273 |
Rprintf("\nres[[%d]]=",k); |
| 274 |
PrintValue(x); |
| 275 |
} |
| 276 |
Rprintf("\nres at %d\n",res); |
| 277 |
PrintValue(res); |
| 278 |
#endif |
| 279 |
UNPROTECT(protectcounter); |
| 280 |
return res; |
| 281 |
} |