次のようなデータフレームがあります。
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
次のような 2 番目のデータフレームがあります。
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
にmtcarsSubset
等しい場合、を転送したいと思います。この for ループでこれを達成できます。rows
mtcars
mpg
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
の転送を達成するための解決策を求めています。方法はわかりませんが、これを達成できるか、または達成できるかを考えています。これは可能ですか?mpg
mtcarsSubset
mtcars
ddply()
ldply
plyr