1

我有一个看起来像这样的数据框:

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

凡是row等于mtcarsSubsetrowsmtcars我要转让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寻求解决方案来完成mpgfrommtcarsSubset到的转移mtcars。我正在设想ddply()或者ldply将能够实现这一目标,尽管我不确定如何实现。这可能plyr吗?

4

2 回答 2

3

这是一种专门与plyr结合使用的方法ifelse()ddply()如果您使用with ,则不需要初始化变量“new.variable” transform

require(plyr)

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

于 2013-07-16T22:22:26.167 回答
0

也许您正在寻找merge()

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

于 2013-07-16T19:58:05.003 回答