Home > Enterprise >  R adding 10 to a specific element of a list in a dataframe
R adding 10 to a specific element of a list in a dataframe

Time:07-07

I have this dataframe df:

df<-structure(list(tile_type_index = c(9, 15, 20, 5, 20), tile_type = c("Flowers", 
"Leather", "Outpost", "Wood 2", "Outpost"), material_on_hex = list(
    c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000, 
    0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))), row.names = c(NA, 
5L), class = "data.frame")

  tile_type_index tile_type
1               9   Flowers
2              15   Leather
3              20   Outpost
4               5    Wood 2
5              20   Outpost
                                                material_on_hex
1 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0
3    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

I want to manipulate it in the following way: if the tile_type is "Outpost" then the row of material_on_hex should remain the same but if tile_type!="Outpost" then I want to add 10 to material_on_hex[tile_type_index]. The result should be:

  tile_type_index tile_type
1               9   Flowers
2              15   Leather
3              20   Outpost
4               5    Wood 2
5              20   Outpost
                                                material_on_hex
1 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0
3    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

CodePudding user response:

Trivial with a loop:

library(magrittr)
for (i in seq_len(nrow(df))) {
  if (df$tile_type[i] == 'Outpost') next 
  tidx = df$tile_type_index[i]
  df$material_on_hex[[i]][tidx] %<>% add(10)
}


#   tile_type_index tile_type                                               material_on_hex
# 1               9   Flowers 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 2              15   Leather 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0
# 3              20   Outpost    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 4               5    Wood 2 0, 0, 0, 0, 1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 5              20   Outpost    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

Could also use mapply():

df$material_on_hex = mapply(
  \(x, y, z) {
    if (x!='Outpost') z[y] %<>% add(10)
    return(z)
  }, 
  x = df$tile_type, y = df$tile_type_index, z = df$material_on_hex, 
  SIMPLIFY = FALSE
)
  • Related