簡體   English   中英

獲取連接值的NA-dplyr包中的left_join()

[英]Getting NA for join values - left_join() in dplyr package

我是新來的dplyr包並在嘗試join操作兩個數據幀。

數據如下

> overview
Source: local data frame [972 x 14]

       household                        names  x2003  x2004  x2005  x2006  x2007  x2008  x2009  x2012 last.avail last.avail.year absChange.last annChange.last
           (chr)                       (fctr)  (int)  (int)  (int)  (int)  (int)  (int)  (int)  (int)      (int)           (dbl)          (int)          (dbl)
1  single parent            totala utgifterna 215300 219870 241920 241060 229290 253590 255950 277260     277260            2012          61960     0.02850119
2  single parent              köpta livsmedel  26420  27910  28160  29100  28310  33020  35910  33740      33740            2012           7320     0.02754621
3  single parent     bröd, spannmålsprodukter   4760   5770   4940   5360   5070   5830   6310     NA       6310            2009           1550     0.04810245
4  single parent     ris och produkter av ris    240    290    210    230    220    280    420     NA        420            2009            180     0.09775732
5  single parent                          ris    100     70     70    130    110    180    200     NA        200            2009            100     0.12246205
6  single parent                 risprodukter    140    220    130    100    120    100    220     NA        220            2009             80     0.07824083
7  single parent pasta och produkter av pasta    410    450    370    460    580    490    600     NA        600            2009            190     0.06551908

> translation
Source: local data frame [8 x 2]

            translation                                   household
                 (fctr)                                      (fctr)
1          Accomodation                                      bostad
2   Leisure and culture                           fritid och kultur
3        Transportation                                   transport
4      Bought Groceries                             köpta livsmedel
5 Rent for accomodation hyra/avgift för hyres-/borätt (inkl garage)
6    Household services                            hushållstjänster
7           Rents (net)                             räntor (brutto)
8          Car expenses                                drift av bil

現在,當我運行left_join(overview, translation)我得到以下結果。

> left_join(overview, translation)
Joining by: "household"
Source: local data frame [972 x 15]

       household                        names  x2003  x2004  x2005  x2006  x2007  x2008  x2009  x2012 last.avail last.avail.year absChange.last annChange.last translation
           (chr)                       (fctr)  (int)  (int)  (int)  (int)  (int)  (int)  (int)  (int)      (int)           (dbl)          (int)          (dbl)      (fctr)
1  single parent            totala utgifterna 215300 219870 241920 241060 229290 253590 255950 277260     277260            2012          61960     0.02850119          NA
2  single parent              köpta livsmedel  26420  27910  28160  29100  28310  33020  35910  33740      33740            2012           7320     0.02754621          NA
3  single parent     bröd, spannmålsprodukter   4760   5770   4940   5360   5070   5830   6310     NA       6310            2009           1550     0.04810245          NA
4  single parent     ris och produkter av ris    240    290    210    230    220    280    420     NA        420            2009            180     0.09775732          NA
5  single parent                          ris    100     70     70    130    110    180    200     NA        200            2009            100     0.12246205          NA
6  single parent                 risprodukter    140    220    130    100    120    100    220     NA        220            2009             80     0.07824083          NA

如您所見,所有翻譯都是NA值。 例如,在索引號[2, 'names']翻譯應等於“購買的食品”,但不等於。

發生了什么,如何解決?

看起來translationhousehold列實際上應稱為names以便它與overview的相應列具有相同的名稱。 您當前的代碼是通過每個數據框中的“ household列聯接的,但是它們沒有對應的值,因此您在兩個數據框中沒有任何匹配項。

您可以通過顯式指定連接列來使代碼以當前形式工作:

left_join(overview, translation, by=c("names"="household"))

在這種情況下,最好固定列名,以避免兩個數據幀之間的混淆。 盡管如此,我還是希望明確指定連接列,以便我可以准確地看到代碼在做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM