Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to convert values for each year between 0:15 to the names given in lc_names:

lc_names <- tibble(y2010=0:15, y2011=0:15, y2012=0:15, y2013=0:15, y2014=0:15, y2015=0:15, y2016=0:15, y2017=0:15, y2018=0:15, y2019=0:15,
                   lc_name = c("pland_00_water", 
                               "pland_01_evergreen_needleleaf", 
                               "pland_02_evergreen_broadleaf", 
                               "pland_03_deciduous_needleleaf", 
                               "pland_04_deciduous_broadleaf", 
                               "pland_05_mixed_forest",
                               "pland_06_closed_shrubland", 
                               "pland_07_open_shrubland", 
                               "pland_08_woody_savanna", 
                               "pland_09_savanna", 
                               "pland_10_grassland", 
                               "pland_11_wetland", 
                               "pland_12_cropland", 
                               "pland_13_urban", 
                               "pland_14_mosiac", 
                               "pland_15_barren"))

They way I've tried to go about this:

pland_pred1 <- pland_pred %>% 
  inner_join(lc_names, by = "c(y2010, y2011, y2012, y2013, y2014, y2015, y2016, y2017, y2018, y2019)") %>%
  arrange(c(y2010, y2011, y2012, y2013, y2014, y2015, y2016, y2017, y2018, y2019)) 

However, I get this error:

Error: Join columns must be present in data. x Problem with c(y2010, y2011, y2012, y2013, y2014, y2015, y2016, y2017, y2018, y2019).

The columns are both present:

 pland_pred
# A tibble: 6,041,662 x 12
      id y2010 y2011 y2012 y2013 y2014 y2015 y2016 y2017 y2018 y2019 pland
   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <dbl>
 1     1    12    12    12    12    12    12    12    12    12    12 1    
 2     2    12    12    12    12    12    12    12    12    12    12 1    
 3     3    12    12    12    12    12    12    12    12    12    12 1    
 4     4    12    12    12    12    12    12    12    12    12    12 1    
 5     5    12    12    12    12    12    12    12    12    12    12 1    
 6     6     3     3     3     3     3     3     3     3     5     5 0.125
 7     6     9     9     9     9     9     8     8     8     8     9 0.125
 8     6     9     9    10    10    10    10    10    10    10    10 0.125
 9     6    10    10    10    10    10    12    12    12    12    12 0.125
10     6    10    10    10    10    12    12    12    12    14    14 0.125
--------------------------------------------------------------------------------
lc_names
# A tibble: 16 x 11
   y2010 y2011 y2012 y2013 y2014 y2015 y2016 y2017 y2018 y2019 lc_name                      
   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <chr>                        
 1     0     0     0     0     0     0     0     0     0     0 pland_00_water               
 2     1     1     1     1     1     1     1     1     1     1 pland_01_evergreen_needleleaf
 3     2     2     2     2     2     2     2     2     2     2 pland_02_evergreen_broadleaf 
 4     3     3     3     3     3     3     3     3     3     3 pland_03_deciduous_needleleaf
 5     4     4     4     4     4     4     4     4     4     4 pland_04_deciduous_broadleaf 
 6     5     5     5     5     5     5     5     5     5     5 pland_05_mixed_forest        
 7     6     6     6     6     6     6     6     6     6     6 pland_06_closed_shrubland    
 8     7     7     7     7     7     7     7     7     7     7 pland_07_open_shrubland      
 9     8     8     8     8     8     8     8     8     8     8 pland_08_woody_savanna       
10     9     9     9     9     9     9     9     9     9     9 pland_09_savanna             
11    10    10    10    10    10    10    10    10    10    10 pland_10_grassland           
12    11    11    11    11    11    11    11    11    11    11 pland_11_wetland             
13    12    12    12    12    12    12    12    12    12    12 pland_12_cropland            
14    13    13    13    13    13    13    13    13    13    13 pland_13_urban               
15    14    14    14    14    14    14    14    14    14    14 pland_14_mosiac              
16    15    15    15    15    15    15    15    15    15    15 pland_15_barren  

Where to go from here?

I expect each value under every year to be replaced with their respective names in lc_name. Such as:

#example for each year to look like
   y2010                      
                          
 1 pland_00_water               
 2 pland_01_evergreen_needleleaf
 3 pland_02_evergreen_broadleaf 
 4 pland_03_deciduous_needleleaf
 5 pland_04_deciduous_broadleaf 
 6 pland_05_mixed_forest        
 7 pland_06_closed_shrubland    
 8 pland_07_open_shrubland      
 9 pland_08_woody_savanna       
10 pland_09_savanna             
11 pland_10_grassland           
12 pland_11_wetland             
13 pland_12_cropland            
14 pland_13_urban               
15 pland_14_mosiac              
16 pland_15_barren  

reproducible code:

structure(list(id = 1:6, y2010 = c(12L, 12L, 12L, 12L, 12L, 3L
), y2011 = c(12L, 12L, 12L, 12L, 12L, 3L), y2012 = c(12L, 12L, 
12L, 12L, 12L, 3L), y2013 = c(12L, 12L, 12L, 12L, 12L, 3L), y2014 = c(12L, 
12L, 12L, 12L, 12L, 3L), y2015 = c(12L, 12L, 12L, 12L, 12L, 3L
), y2016 = c(12L, 12L, 12L, 12L, 12L, 3L), y2017 = c(12L, 12L, 
12L, 12L, 12L, 3L), y2018 = c(12L, 12L, 12L, 12L, 12L, 5L), y2019 = c(12L, 
12L, 12L, 12L, 12L, 5L), pland = c(1, 1, 1, 1, 1, 0.125)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

Reproducible code will only have some values to convert to names


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

One option is to subset the column names that startsWith 'y', loop over those column names in map, transmute the column from 'pland_pred' after replacing the values with a corresponding named vector from the 'lc_names' columns for the year column and the 'lc_name', and assign the output back to the same column in 'pland_pred'

library(dplyr)
library(purrr)
library(tibble)
nm1 <- names(pland_pred)[startsWith(names(pland_pred), 'y')]
pland_pred[nm1] <- map(nm1, ~
     pland_pred %>% 
        transmute(!! .x := (lc_names %>%
             select(.x, lc_name) %>% 
             deframe)[as.character(!!rlang::sym(.x))]) %>%
             pull(1))

-output

pland_pred
# A tibble: 6 x 12
#     id y2010        y2011        y2012        y2013        y2014        y2015        y2016        y2017        y2018     y2019     pland
#  <int> <chr>        <chr>        <chr>        <chr>        <chr>        <chr>        #<chr>        <chr>        <chr>     <chr>     <dbl>
#1     1 pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12… pland_12… 1    
#2     2 pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12… pland_12… 1    
#3     3 pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12… pland_12… 1    
#4     4 pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12… pland_12… 1    
#5     5 pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12_cr… pland_12… pland_12… 1    
#6     6 pland_03_de… pland_03_de… pland_03_de… pland_03_de… pland_03_de… pland_03_de… pland_03_de… pland_03_de… pland_05… pland_05… 0.125

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...