【问题标题】:R adding 10 to a specific element of a list in a dataframeR将10添加到数据框中列表的特定元素
【发布时间】:2022-07-06 23:04:25
【问题描述】:

我有这个数据框 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

我想通过以下方式操作它:如果 tile_type 是“Outpost”,那么 material_on_hex 的行应该保持不变,但是如果 tile_type!="Outpost" 那么我想在 material_on_hex[tile_type_index] 中添加 10。结果应该是:

  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

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    简单的循环:

    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
    

    也可以使用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
    )
    

    【讨论】:

      猜你喜欢
      • 2022-07-05
      • 2012-09-10
      • 2020-05-22
      • 2021-01-21
      • 2021-05-19
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多