简体   繁体   中英

Problem with putting information in a column in R

I have 2 data frames, one data frame called datos_octubre with 10131000 rows and other dataframe called datos_conductores .

I wanna put a new column called operador , this column will be fill by the follow instruction

for( j in 1:100){
for(i in 1:100){
  if ( (datos_octubre$FECHA_GPS[i]== datos_conductores$fecha[i])){
    if (datos_octubre$EQU_CODIGO[i]== datos_conductores$EQU_CODIGO[j]){
      if (datos_octubre$HORA_GPS[i] <= datos_conductores$hora_fin[j]){
        datos_octubre$Operador[i] <- datos_octubre$NOMBRE[j]
      }
    }
  }

This is the structure of data frame datos_octubre and the head:

> str(datos_octubre)
'data.frame':   10131530 obs. of  14 variables:
 $ REP_GPS_CODIGO : Factor w/ 9329105 levels "MI051","MI051_1832614921789237",..: 2 3 4 5 6 7 8 9 10 11 ...
 $ EQU_CODIGO     : chr  "MI051" "MI051" "MI051" "MI051" ...
 $ TRAM_GPS_CODIGO: Factor w/ 4 levels "01","03","05",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ EVE_GPS_CODIGO : Factor w/ 83 levels "01","02","03",..: 3 3 3 3 3 9 3 3 3 3 ...
 $ FECHA_GPS      : POSIXct, format: "2019-10-01" "2019-10-01" "2019-10-01" "2019-10-01" ...
 $ HORA_GPS       : Factor w/ 86389 levels "-75.6654","-75.6655",..: 16528 16536 16546 16556 16564 16568 16574 16583 16592 16601 ...
 $ LON_GPS        : num  -75.7 -75.7 -75.7 -75.7 -75.7 ...
 $ LAT_GPS        : num  4.8 4.8 4.8 4.8 4.8 ...
 $ VEL_GPS        : num  0 0 0 0 0 0 0 0 0 0 ...
 $ DIR_GPS        : int  0 0 0 101 101 101 101 101 101 101 ...
 $ ACL_GPS        : int  0 0 0 0 0 NA 0 0 0 0 ...
 $ ODO_GPS        : int  28229762 28229762 28229762 28229768 28229770 NA 28229770 28229770 28229770 28229770 ...
 $ ALT_GPS        : Factor w/ 120 levels "","\"MI051_1902402005409507",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Operador       : chr  "" "" "" "" ...
> head(datos_octubre)
          REP_GPS_CODIGO EQU_CODIGO TRAM_GPS_CODIGO EVE_GPS_CODIGO  FECHA_GPS HORA_GPS  LON_GPS LAT_GPS VEL_GPS DIR_GPS ACL_GPS  ODO_GPS ALT_GPS Operador
1 MI051_1832614921789237      MI051              EV             03 2019-10-01 04:35:38 -75.7444 4.79857       0       0       0 28229762                 
2 MI051_1832614964979379      MI051              EV             03 2019-10-01 04:35:46 -75.7444 4.79857       0       0       0 28229762                 
3 MI051_1832616366109032      MI051              EV             03 2019-10-01 04:35:56 -75.7444 4.79857       0       0       0 28229762                 
4 MI051_1832617794914447      MI051              EV             03 2019-10-01 04:36:06 -75.7442 4.79907       0     101       0 28229768                 
5 MI051_1832619516509591      MI051              EV             03 2019-10-01 04:36:14 -75.7442 4.79908       0     101       0 28229770                 
6 MI051_1832619543973570      MI051              EV             10 2019-10-01 04:36:18 -75.7442 4.79908       0     101      NA       NA         

And this is the restructure of datos_conductores and the head:

> str(datos_conductores)
'data.frame':   16522 obs. of  11 variables:
 $ fecha      : POSIXct, format: "2019-10-01" "2019-10-01" "2019-10-01" "2019-10-01" ...
 $ equ_id     : int  99 99 99 99 99 99 99 99 99 99 ...
 $ conductor  : int  34 34 34 34 34 34 34 65 65 65 ...
 $ servicio_id: int  533329 533328 533327 533326 533325 533324 533323 533333 533332 533331 ...
 $ PERA_ID    : int  362 362 362 362 362 362 362 107 107 107 ...
 $ hora_ini   : POSIXct, format: "2019-11-28 09:16:16" "2019-11-28 08:38:16" "2019-11-28 08:00:16" "2019-11-28 07:22:16" ...
 $ hora_fin   : POSIXct, format: "2019-11-28 09:21:00" "2019-11-28 09:16:16" "2019-11-28 08:38:16" "2019-11-28 08:00:16" ...
 $ ruta_id    : int  24 24 24 24 24 24 24 24 24 24 ...
 $ NOMBRE     : Factor w/ 85 levels "ALBERT HERNAN ZAPATA RESTREPO",..: 71 71 71 71 71 71 71 53 53 53 ...
 $ PERA_CEDULA: int  1088253762 1088253762 1088253762 1088253762 1088253762 1088253762 1088253762 10087424 10087424 10087424 ...
 $ EQU_CODIGO : Factor w/ 36 levels "MI051","MI052",..: 9 9 9 9 9 9 9 9 9 9 ...
> head(datos_octubre)
          REP_GPS_CODIGO EQU_CODIGO TRAM_GPS_CODIGO EVE_GPS_CODIGO  FECHA_GPS HORA_GPS  LON_GPS LAT_GPS VEL_GPS DIR_GPS ACL_GPS  ODO_GPS ALT_GPS Operador
1 MI051_1832614921789237      MI051              EV             03 2019-10-01 04:35:38 -75.7444 4.79857       0       0       0 28229762                 
2 MI051_1832614964979379      MI051              EV             03 2019-10-01 04:35:46 -75.7444 4.79857       0       0       0 28229762                 
3 MI051_1832616366109032      MI051              EV             03 2019-10-01 04:35:56 -75.7444 4.79857       0       0       0 28229762                 
4 MI051_1832617794914447      MI051              EV             03 2019-10-01 04:36:06 -75.7442 4.79907       0     101       0 28229768                 
5 MI051_1832619516509591      MI051              EV             03 2019-10-01 04:36:14 -75.7442 4.79908       0     101       0 28229770                 
6 MI051_1832619543973570      MI051              EV             10 2019-10-01 04:36:18 -75.7442 4.79908       0     101      NA       NA                 

Also I tried with operator pype but I'm not getting the result I want.

I already find the solution.

I have to convert all data type data with the function as.POSIXct and in the for make a correction with the time_ini and the time_finish of every data.

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