Home > front end >  How to replace values from a data frame with the values from a hash map/dictionary?
How to replace values from a data frame with the values from a hash map/dictionary?

Time:05-18

Created a hash from mtcars data frame.

counter <- 0
h <- hash()
for(i in 1:nrow(mtcars)){
  for(j in 1:length(names(mtcars))){
    counter <- counter   1
    h[[as.character(mtcars[i,j])]] <- counter      }
}

What I'm trying to do now is replace the values in the mtcars dataframe with the values from the hash map that I created in r.

For example, 0 in the mtcars data frame now be 338 and 1 in mtcars data frame would be replaced with 350.

CodePudding user response:

I don't understand what you're trying to do. You are assigning values to the hash that are incremented but don't seem to have any particular meaning. Anyway, here is the solution if I have understood you well:

for(i in 1:nrow(mtcars)){
  for(j in 1:length(names(mtcars))){
    mtcars[i,j] <- h[[as.character(mtcars[i,j])]]
  }
}

It's very similar to what you had already done in the first part, only now you're fetching from the hash and assigning back to mtcars.

Output:

> mtcars
                    mpg cyl disp  hp drat  wt qsec  vs  am gear carb
Mazda RX4            12 330   14  37   16   6    7 338 350  351  351
Mazda RX4 Wag        12 330   14  37   16  17   51 338 350  351  351
Datsun 710           89 351   25  26   27  28   29 350 350  351  350
Hornet 4 Drive      342 330   36  37  269  39   40 350 338  274  350
Hornet Sportabout    45 341   69 323  247 116   51 338 338  274  352
Valiant              56 330   58  59  236  61   62 350 338  274  350
Duster 360           67 341   69 257   71 336   73 338 338  274  351
Merc 240D            78 351   80  81   82  83   84 350 338  351  352
Merc 230             89 351   91  92  115 247   95 350 338  351  352
Merc 280            265 330  113 114  115 116  106 350 338  351  351
Merc 280C           111 330  113 114  115 116  282 350 338  351  351
Merc 450SE          122 341  146 147  148 127  128 338 338  274  274
Merc 450SL          249 341  146 147  148 258  139 338 338  274  274
Merc 450SLC         243 341  146 147  148 149  150 338 338  274  274
Cadillac Fleetwood  166 341  157 158  159 160  161 338 338  274  351
Lincoln Continental 166 341  168 169  274 171  172 338 338  274  351
Chrysler Imperial   177 341  179 180  181 182  183 338 338  274  351
Fiat 128            188 351  190 279  280 193  194 350 350  351  350
Honda Civic         298 351  201 202  203 204  205 350 350  351  352
Toyota Corolla      210 351  212 213  313 215  216 350 350  351  350
Toyota Corona       221 351  223 224  225 226  227 350 338  274  350
Dodge Challenger    326 341  234 246  236 237  238 338 338  274  352
AMC Javelin         243 341  245 246  247 248  249 338 338  274  352
Camaro Z28          254 341  256 257  258 259  260 338 338  274  351
Pontiac Firebird    265 341  267 323  269 270  271 338 338  274  352
Fiat X1-9           276 351  278 279  280 281  282 350 350  351  350
Porsche 914-2       287 351  289 290  291 292  293 338 350  340  352
Lotus Europa        298 351  300 301  302 303  304 350 350  340  352
Ford Pantera L      309 341  311 312  313 314  315 338 350  340  351
Ferrari Dino        320 330  322 323  324 325  326 338 350  340  330
Maserati Bora       331 341  333 334  335 336  337 338 350  340  341
Volvo 142E          342 351  344 345  346 347  348 350 350  351  352
  • Related