首页 > 解决方案 > 根据查找值为列创建新名称

问题描述

我有一个 2010 年和 2019 年土地覆盖类型的数据框。Pland 代表土地覆盖的总价值,1 等于该特定区域的 100%,相对于 id。这些计算是事先进行的,id 代表每个几何图形。

我想执行一个函数,该函数生成具有这些描述性名称的另一列,其中*代表要替换的名称:

lc_names <- tibble(landcover = 0:15,
                   lc_name = c("*_to_water", 
                               "*_to_evergreen_needleleaf", 
                               "*_to_evergreen_broadleaf", 
                               "*_to_deciduous_needleleaf", 
                               "*_to_deciduous_broadleaf", 
                               "*_to_mixed_forest",
                               "*_to_closed_shrubland", 
                               "*_to_open_shrubland", 
                               "*_to_woody_savanna", 
                               "*_to_savanna", 
                               "*_to_grassland", 
                               "*_to_wetland", 
                               "*_to_cropland", 
                               "*_to_urban", 
                               "*_to_mosiac", 
                               "*_to_barren"))

#these names can be used to replace the star
lc_names <- tibble(landcover = 0:15,
                   lc_name = c("water", 
                               "evergreen_needleleaf", 
                               "evergreen_broadleaf", 
                               "deciduous_needleleaf", 
                               "deciduous_broadleaf", 
                               "mixed_forest",
                               "closed_shrubland", 
                               "open_shrubland", 
                               "woody_savanna", 
                               "savanna", 
                               "grassland", 
                               "wetland", 
                               "cropland", 
                               "urban", 
                               "mosiac", 
                               "barren"))

相对于这些值:

A tibble: 50 x 4
      id y2010 y2019  pland
   <int> <int> <int>  <dbl>
 1     1    12    12 1     
 2     2    12    12 1     
 3     3    12    12 1     
 4     4    12    12 1     
 5     5    12    12 1     
 6     6     3     5 0.0345
 7     6     9     9 0.0345
 8     6     9    10 0.0345
 9     6    10    12 0.0345
10     6    10    14 0.0345
# ... with 40 more rows

因此,当它们匹配时,例如,数字 12在这两年中都croplandid1,这将产生一个如下所示的列:

cropland_to_cropland

它的外观示例:

      id y2010 y2019  pland   lc_name
   <int> <int> <int>  <dbl>
 1     1    12    12 1      cropland_to_cropland
 2     2    12    12 1      cropland_to_cropland
 3     3    12    12 1      cropland_to_cropland
 4     4    12    12 1      cropland_to_cropland
 5     5    12    12 1      cropland_to_cropland
 6     6     3     5 0.0345 closed_shrubland_to_deciduous_broadleaf

可重现的代码:

structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 36L
), y2010 = c(12L, 12L, 12L, 12L, 12L, 3L, 9L, 9L, 10L, 10L, 12L, 
12L, 14L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 10L, 12L, 12L, 10L, 12L, 10L, 10L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 8L, 10L
), y2019 = c(12L, 12L, 12L, 12L, 12L, 5L, 9L, 10L, 12L, 14L, 
12L, 12L, 9L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 14L, 12L, 14L, 10L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 8L, 
14L), pland = c(1, 1, 1, 1, 1, 0.0344827586206897, 0.0344827586206897, 
0.0344827586206897, 0.0344827586206897, 0.0344827586206897, 0.758620689655172, 
0.0344827586206897, 0.0344827586206897, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0344827586206897, 0.931034482758621, 
0.0344827586206897, 0.0344827586206897, 0.96551724137931, 0.0344827586206897, 
0.0344827586206897, 0.0344827586206897, 0.896551724137931, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 0.137931034482759, 0.0344827586206897
)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))

标签: r

解决方案


使用简单的子集 - 你基本上就在那里。

# using +1 because you've got this 0 in your values ... maybe reconsider this... :)
mydat$new_col <- 
  paste0(lc_names$lc_name[mydat$y2010 + 1], "_to_", lc_names$lc_name[mydat$y2019 + 1])

mydat
#> # A tibble: 50 x 5
#>       id y2010 y2019  pland new_col                             
#>    <int> <int> <int>  <dbl> <chr>                               
#>  1     1    12    12 1      cropland_to_cropland                
#>  2     2    12    12 1      cropland_to_cropland                
#>  3     3    12    12 1      cropland_to_cropland                
#>  4     4    12    12 1      cropland_to_cropland                
#>  5     5    12    12 1      cropland_to_cropland                
#>  6     6     3     5 0.0345 deciduous_needleleaf_to_mixed_forest
#>  7     6     9     9 0.0345 savanna_to_savanna                  
#>  8     6     9    10 0.0345 savanna_to_grassland                
#>  9     6    10    12 0.0345 grassland_to_cropland               
#> 10     6    10    14 0.0345 grassland_to_mosiac                 
#> # … with 40 more rows

推荐阅读