0

我将非常感谢以下定义问题的解决方案;我认为这是一件非常困难的事情。

我加入 2 个 data.framest1t2使用merge(). 在生成的 data.frame 中,我将其命名为“testing”,我想用“NA”替换源自的非唯一行的条目,t1以便仅保留与t2. 条件是:

min(sqrt((xCor.y - xCor.x)^2 + (yCor.y - yCor.x)^2))  
# scroll to end for result I am looking for

这适用于约 1GB 的数据集,因此我必须避免遍历所有数据。

t1<- data.frame(trackLabel = c(1, 2, 3, 4, 4, 5, 5, 7, 7, 7), 
                   objNumber = 1:10, 
                   parentObjNumber = rep(0, 10), 
                    time = rep(1,10), 
                    xCor = runif(10), 
                    yCor = runif(10))

t2<-  data.frame(trackLabel = c(1, 2, 2, 4, 4, 4, 6, 7, 7, 7, 7), 
                    objNumber = 11:21, 
                    parentObjNumber = c(1, 2, 2, 4, 4, 4, 7, 8, 9, 9, 9),
                    time = rep(2,11),
                    xCor = runif(11),
                    yCor = runif(11))


testing<-  merge(t1, t2, 
                by.x = c("trackLabel", "objNumber"),
                 by.y = c("trackLabel", "parentObjNumber"),
                all = TRUE,
                incomparables = NA)

#Warning message:
#In merge.data.frame(t1, t2, by.x = c("trackLabel", "objNumber"),  :
#  column name ‘objNumber’ is duplicated in the result

ind<-colnames(testing)=="objNumber"
colnames(testing)[min(which(ind == TRUE )) ] <- paste("objNumber", 1, sep = "")

> t1
   trackLabel objNumber parentObjNumber time       xCor        yCor
1           1         1               0    1 0.25852366 0.360631607
2           2         2               0    1 0.69987607 0.048360258
3           3         3               0    1 0.23047883 0.414221880
4           4         4               0    1 0.58169548 0.718223111
5           4         5               0    1 0.61419336 0.435153774
6           5         6               0    1 0.50028765 0.735970291
7           5         7               0    1 0.41380332 0.097256739
8           7         8               0    1 0.57563080 0.828142024
9           7         9               0    1 0.39512092 0.728903233
10          7        10               0    1 0.16675690 0.284307824

> t2
   trackLabel objNumber parentObjNumber time        xCor        yCor
1           1        11               1    2 0.473735625 0.454637752
2           2        12               2    2 0.623971860 0.517089522
3           2        13               2    2 0.470885840 0.703872484
4           4        14               4    2 0.188280842 0.678683831
5           4        15               4    2 0.198772198 0.160836676
6           4        16               4    2 0.251950005 0.958747183
7           6        17               7    2 0.545521560 0.005505346
8           7        18               8    2 0.477450908 0.819060935
9           7        19               9    2 0.509430458 0.997968108
10          7        20               9    2 0.027918865 0.138014769
11          7        21               9    2 0.568532497 0.911921770

> testing

   trackLabel objNumber1 parentObjNumber time.x     xCor.x      yCor.x objNumber time.y      xCor.y      yCor.y
1           1          1               0      1 0.25852366 0.360631607        11      2 0.473735625 0.454637752
2           2          2               0      1 0.69987607 0.048360258        12      2 0.623971860 0.517089522
3           2          2               0      1 0.69987607 0.048360258        13      2 0.470885840 0.703872484
4           3          3               0      1 0.23047883 0.414221880        NA     NA          NA          NA
5           4          4               0      1 0.58169548 0.718223111        14      2 0.188280842 0.678683831
6           4          4               0      1 0.58169548 0.718223111        15      2 0.198772198 0.160836676
7           4          4               0      1 0.58169548 0.718223111        16      2 0.251950005 0.958747183
8           4          5               0      1 0.61419336 0.435153774        NA     NA          NA          NA
9           5          6               0      1 0.50028765 0.735970291        NA     NA          NA          NA
10          5          7               0      1 0.41380332 0.097256739        NA     NA          NA          NA
11          6          7              NA     NA         NA          NA        17      2 0.545521560 0.005505346
12          7          8               0      1 0.57563080 0.828142024        18      2 0.477450908 0.819060935
13          7          9               0      1 0.39512092 0.728903233        19      2 0.509430458 0.997968108
14          7          9               0      1 0.39512092 0.728903233        20      2 0.027918865 0.138014769
15          7          9               0      1 0.39512092 0.728903233        21      2 0.568532497 0.911921770
16          7         10               0      1 0.16675690 0.284307824        NA     NA          NA          NA


# and here is what I want to achieve:
> testing[c(3, 6, 7, 13, 14 ), 1:6] <-NA
> testing
   trackLabel objNumber1 parentObjNumber time.x     xCor.x      yCor.x objNumber time.y      xCor.y      yCor.y
1           1          1               0      1 0.25852366 0.360631607        11      2 0.473735625 0.454637752
2           2          2               0      1 0.69987607 0.048360258        12      2 0.623971860 0.517089522
3          NA         NA              NA     NA         NA          NA        13      2 0.470885840 0.703872484
4           3          3               0      1 0.23047883 0.414221880        NA     NA          NA          NA
5           4          4               0      1 0.58169548 0.718223111        14      2 0.188280842 0.678683831
6          NA         NA              NA     NA         NA          NA        15      2 0.198772198 0.160836676
7          NA         NA              NA     NA         NA          NA        16      2 0.251950005 0.958747183
8           4          5               0      1 0.61419336 0.435153774        NA     NA          NA          NA
9           5          6               0      1 0.50028765 0.735970291        NA     NA          NA          NA
10          5          7               0      1 0.41380332 0.097256739        NA     NA          NA          NA
11          6          7              NA     NA         NA          NA        17      2 0.545521560 0.005505346
12          7          8               0      1 0.57563080 0.828142024        18      2 0.477450908 0.819060935
13         NA         NA              NA     NA         NA          NA        19      2 0.509430458 0.997968108
14         NA         NA              NA     NA         NA          NA        20      2 0.027918865 0.138014769
15          7          9               0      1 0.39512092 0.728903233        21      2 0.568532497 0.911921770
16          7         10               0      1 0.16675690 0.284307824        NA     NA          NA          NA
4

0 回答 0