"对于"在R中循环并检查列中的先前值

时间:2015-08-26 06:30:26

标签: r loops for-loop

我处理的数据框看起来像this

以下是它的样子:

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    

依此类推......直到635行返回。

和我要比较的其他数据集可以找到here

以下是它的样子:

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

它们都共享相同的属性,即category

我想检查是否可以比较第一个数据集中的hour列中的前一个小时,以便将其与第二个数据集中的值进行比较。

在这里,我理想地想在R中找到:

#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 }

我不知道怎么做,这就是我的尝试:

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   
  }
}

这就是我所期望的:

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 

0 个答案:

没有答案