“For” Schleife in R und Überprüfung des vorherigen Wertes aus einer Spalte

Ich arbeite an einem Datenrahmen, der aussieht wieDie

So sieht es aus:

shape id day     hour week id footfall category          area name
22496   22/3/14  3    12      634      Work cluster     CBD area 1  
22670   22/3/14  3    12      220      Shopping cluster Orchard Road 1  
23287   22/3/14  3    12      723      Airport  Changi  Airport 2   
16430   22/3/14  4    12      947      Work cluster     CBD area 2  
4697    22/3/14  3    12      220      Residential area Ang Mo Kio 2    
4911    22/3/14  3    12      1001     Shopping cluster Orchard Rd 3    
11126   22/3/14  3    12      220      Residential area Ang Mo Kio 2    

und so weiter ... bis 635 Zeilen zurückkehren.

und der andere Datensatz, mit dem ich vergleichen möchte, kann gefunden werdenHie

So sieht es aus:

category         Foreigners Locals
Work cluster     1600000    362,3900
Shopping cluster 1800000    3646666.667
Airport          15095152   8902705
Residential area 527700     280000

ie haben beide dasselbe Attribut, d. category

Ich möchte überprüfen, ob ich das @ vergleichen ka vorherige Stunde aus der Spaltehour im ersten Datensatz, damit ich ihn mit dem Wert aus dem zweiten Datensatz vergleichen kann.

Hier ist, was ich idealerweise in R finden möchte:

#for n in 1: number of rows{
    # check the previous hour from IDA dataset !!!!
    # calculate hourSum - previousHour = newHourSum and store it as newHourSum
    # calculate hour/(newHourSum-previousHour) * Foreigners and store it as footfallHour
    # add to the empty dataframe }

Ich bin mir nicht sicher, wie ich das machen soll und hier ist, was ich versucht habe:

tbl1 <- secondDataset
tbl2 <- firstDataset
mergetbl <- function(tbl1, tbl2)
{

  newtbl = data.frame(hour=numeric(),forgHour=numeric(),locHour=numeric())

  ntbl1rows<-nrow(tbl1) # get the number of rows

  for(n in 1:ntbl1rows)
  {

    #get the previousHour
    newHourSum <- tbl1$hour - previousHour
    footfallHour <- (tbl1$hour/(newHourSum-previousHour)) * tbl2$Foreigners
    #add to newtbl   
  }
}

Das würde, was ich erwartet hatte:

shape id day     hour week id footfall category          area name    forgHour  locHour 
22496   22/3/14  3    12      634      Work cluster     CBD area 1     1         12
22670   22/3/14  3    12      220      Shopping cluster Orchard Road 1 21        25
23287   22/3/14  3    12      723      Airport  Changi  Airport 2      31        34
16430   22/3/14  4    12      947      Work cluster     CBD area 2     41        23
4697    22/3/14  3    12      220      Residential area Ang Mo Kio 2   51        23
4911    22/3/14  3    12      1001     Shopping cluster Orchard Rd 3   61        45
11126   22/3/14  3    12      220      Residential area Ang Mo Kio 2   72        54 

Antworten auf die Frage(0)

Ihre Antwort auf die Frage