2013-07-16 69 views
1

我有一個數據幀,看起來像這樣:從一個數據幀傳輸到另一個plyr

library(car) 
mtcarsSubset <- car::some(mtcars) 
mtcarsSubset <- mtcarsSubset[, c("mpg", "drat")] 
mtcarsSubset$rows <- rownames(mtcarsSubset) 

mtcarsSubset 
        mpg drat    rows 
Datsun 710   22.8 3.85   Datsun 710 
Duster 360   14.3 3.21   Duster 360 
Merc 240D   24.4 3.69   Merc 240D 
Merc 280   19.2 3.92   Merc 280 
Merc 450SLC  15.2 3.07  Merc 450SLC 
Cadillac Fleetwood 10.4 2.93 Cadillac Fleetwood 
Toyota Corolla  33.9 4.22  Toyota Corolla 
Toyota Corona  21.5 3.70  Toyota Corona 
Dodge Challenger 15.5 2.76 Dodge Challenger 
Ford Pantera L  15.8 4.22  Ford Pantera L 

我有一個看起來像這樣的第二個數據幀:

mtcars <- mtcars[,c("gear", "carb")] 
mtcars$new.variable <- NA 
mtcars$rows <- rownames(mtcars) 
        gear carb new.variable    rows 
Mazda RX4    4 4   NA   Mazda RX4 
Mazda RX4 Wag   4 4   NA  Mazda RX4 Wag 
Datsun 710    4 1   NA   Datsun 710 
Hornet 4 Drive   3 1   NA  Hornet 4 Drive 
Hornet Sportabout  3 2   NA Hornet Sportabout 
Valiant    3 1   NA    Valiant 
Duster 360    3 4   NA   Duster 360 
Merc 240D    4 2   NA   Merc 240D 
Merc 230    4 2   NA   Merc 230 
Merc 280    4 4   NA   Merc 280 
Merc 280C    4 4   NA   Merc 280C 
Merc 450SE    3 3   NA   Merc 450SE 
Merc 450SL    3 3   NA   Merc 450SL 
Merc 450SLC   3 3   NA   Merc 450SLC 
Cadillac Fleetwood  3 4   NA Cadillac Fleetwood 
Lincoln Continental 3 4   NA Lincoln Continental 
Chrysler Imperial  3 4   NA Chrysler Imperial 
Fiat 128    4 1   NA   Fiat 128 
Honda Civic   4 2   NA   Honda Civic 
Toyota Corolla   4 1   NA  Toyota Corolla 
Toyota Corona   3 1   NA  Toyota Corona 
Dodge Challenger  3 2   NA Dodge Challenger 
AMC Javelin   3 2   NA   AMC Javelin 
Camaro Z28    3 4   NA   Camaro Z28 
Pontiac Firebird  3 2   NA Pontiac Firebird 
Fiat X1-9    4 1   NA   Fiat X1-9 
Porsche 914-2   5 2   NA  Porsche 914-2 
Lotus Europa   5 2   NA  Lotus Europa 
Ford Pantera L   5 4   NA  Ford Pantera L 
Ferrari Dino   5 6   NA  Ferrari Dino 
Maserati Bora   5 8   NA  Maserati Bora 
Volvo 142E    4 2   NA   Volvo 142E 

mtcarsSubset平等的rowrowsmtcars,我想轉讓mpg。我可以用這個實現這個for循環:

for (i in seq_along(mtcarsSubset$rows)) { 

    x <- which(mtcars$rows == mtcarsSubset$rows[i]) 
    mtcars$new.variable[x] <- mtcarsSubset$mpg[i] 

} 

        gear carb new.variable    rows 
Mazda RX4    4 4   NA   Mazda RX4 
Mazda RX4 Wag   4 4   21.0  Mazda RX4 Wag 
Datsun 710    4 1   22.8   Datsun 710 
Hornet 4 Drive   3 1   NA  Hornet 4 Drive 
Hornet Sportabout  3 2   NA Hornet Sportabout 
Valiant    3 1   18.1    Valiant 
Duster 360    3 4   NA   Duster 360 
Merc 240D    4 2   24.4   Merc 240D 
Merc 230    4 2   NA   Merc 230 
Merc 280    4 4   19.2   Merc 280 
Merc 280C    4 4   NA   Merc 280C 
Merc 450SE    3 3   NA   Merc 450SE 
Merc 450SL    3 3   NA   Merc 450SL 
Merc 450SLC   3 3   15.2   Merc 450SLC 
Cadillac Fleetwood  3 4   NA Cadillac Fleetwood 
Lincoln Continental 3 4   NA Lincoln Continental 
Chrysler Imperial  3 4   NA Chrysler Imperial 
Fiat 128    4 1   32.4   Fiat 128 
Honda Civic   4 2   NA   Honda Civic 
Toyota Corolla   4 1   NA  Toyota Corolla 
Toyota Corona   3 1   NA  Toyota Corona 
Dodge Challenger  3 2   NA Dodge Challenger 
AMC Javelin   3 2   15.2   AMC Javelin 
Camaro Z28    3 4   13.3   Camaro Z28 
Pontiac Firebird  3 2   NA Pontiac Firebird 
Fiat X1-9    4 1   27.3   Fiat X1-9 
Porsche 914-2   5 2   NA  Porsche 914-2 
Lotus Europa   5 2   NA  Lotus Europa 
Ford Pantera L   5 4   NA  Ford Pantera L 
Ferrari Dino   5 6   NA  Ferrari Dino 
Maserati Bora   5 8   NA  Maserati Bora 
Volvo 142E    4 2   NA   Volvo 142E 

我一個plyr的解決方案後,特別是要完成的mpgmtcarsSubset轉移到mtcars。我設想要麼ddply()ldply將能夠實現這一點,雖然我不知道如何。這可能與plyr

回答

3

這裏是一種方法,特別是與plyr結合ifelse()做到這一點。如果您使用ddply()transform,則不需要初始化變量「new.variable」。

require(plyr)

ddply(mtcars, .(rows), transform, new.variable = ifelse(rows %in% mtcarsSubset$rows, mtcarsSubset$mpg[mtcarsSubset$rows == rows], NA))

0

也許您在尋找merge()

merge(mtcars, mtcarsSubset[ , "new.variable"], by="row.names", all.x=T)

+0

我一個plyr解決方案後,特別是。 Plus merge()不能與我的真實數據一起工作(我已經修改了代碼,它更貼近地反映了我的真實數據) – luciano

相關問題