R将列表字符转换为其他列表

R Turning list characters into other lists

我无法重现我的数据,所以我将提供类似的示例数据:

lst1 <- list(IT = c("Training", "Helpdesk"), Construction = c("Water", "Waste"))
lst2 <- list(Training = c("Alarm Engineer", "Backend Engineer"),
             Helpdesk = c("Analyst 1", "Analyst 2"),
             Water = c("Water 1", "Water 2"),
             Waste = c("Waste 1", "Waste 2", "Waste 3"))
lst3 <- list(`Alarm Engineer` = head(mtcars), `Backend Engineer` = head(VADeaths),
             `Analyst 1` = head(diamonds), `Analyst 2` = head(ChickWeight),
             `Water 1` = head(state.x77), `Water 2` = head(iris),
             `Waste 1` = head(CO2), `Waste 2` = head(cbind(letters, LETTERS)),
             `Waste 3` = state.abb)

首先 (lst1) 我有一个长度为 35 的小嵌套列表:

head(dis_sub, 2)         
$IT
 [1] "Web Developer"         "Web Designer"          "Training"              "Testing"               "Technician"            "Technical Support"    
 [7] "Systems Engineer"      "Systems Analyst"       "Helpdesk"     

$Construction

 [1] "Water"                     "Waste"                     "Sustainability"            "Structural Engineer"       "Skilled Trades"           
 [6] "Site Manager"              "Mechanical and Electrical" 

然后我有第二个长度为 250 的嵌套列表 (lst2):

head(sub_jobs, 2)
$Mechanical and Electrical

    [1] "Alarm Engineer"          "Backend Engineer"     "Biomass Engineer"                         
  [4] "Building Engineer"         "Cam Engineer"       "Electrician" 

$Helpdesk

 [1] "1st Line Analyst"                 "1st Line Desktop Support Analyst" "1st Line Engineer"                "1st Line Helpdesk"               
 [5] "1st Line Helpdesk Support"    "1st Line IT Support"    "1st Line IT 

然后我有一个最终列表 (lst3),这个列表是一个数据框列表,所有数据框都具有一个公共变量,但具有不同数量的观察值:

   head(list_all, 2)
$Electrician
                              words
1                     17th edition 
2       17th edition qualification 
3           17th edition qualified 
4                          3 phase 
5                     access point 

st Line Helpdesk Support
                                        words
1                 1st line technical support 
2                      2nd line support team 
3                 2nd line technical support 

现在您可能已经在第一个列表中发现了它,在 IT 下我们看到了 Helpdesk。在建设中,我们看到机械和电气

我想将这些从字符转换为列表二中的列表。

然后我们在列表二的机械和电气下看到电工,在帮助台下我们看到第一线帮助台支持。然后我希望将这些字符变成列表 3 中的 data.frames。

列出清单并从那里开始可能对我来说很愚蠢。我显然可以制作 35 和 250 的简单列表,我也有一个数据框,如:

dis           sub       norm
IT            Helpdesk  1st Line Helpdesk Support
IT            Helpdesk  Analyst
IT            Testing   Tester
Construction  Mech...   Electrician 
Construction  Mech..    Alarm Engineer 
Construction  Waste     Recycling Engineer

然后是一个充满 csv 的文件夹,每个 csv 都以上面的每个标准行命名,并包含 data.frames,用于制作列表 3 中 data.frames 的列表。

我想要的是:

>List
$IT
   Helpdesk
            1st Line Helpdesk Support
                    1                 1st line technical support 
                    2                      2nd line support team 
                    3                 2nd line technical support
                    ...
            Analyst
                    ...
            ...
   Web developer
            ...
                    ...
   ...

$Construction
             Mechanical and Electrical
                             Electrician
                                     1                     17th edition 
                                     2       17th edition qualification 
                                     3           17th edition qualified

我们可以嵌套一个子集。

lapply(lst1, function(x) lapply(lst2[x], function(.x) lst3[.x]))

数据

lst1 <- list(IT = c("Training", "Helpdesk"), Construction = c("Water", "Waste"))
lst2 <- list(Training = c("Alarm Engineer", "Backend Engineer"),
             Helpdesk = c("Analyst 1", "Analyst 2"),
             Water = c("Water 1", "Water 2"),
             Waste = c("Waste 1", "Waste 2", "Waste 3"))
lst3 <- list(`Alarm Engineer` = head(mtcars), `Backend Engineer` = head(VADeaths),
             `Analyst 1` = head(diamonds), `Analyst 2` = head(ChickWeight),
             `Water 1` = head(state.x77), `Water 2` = head(iris),
             `Waste 1` = head(CO2), `Waste 2` = head(cbind(letters, LETTERS)),
             `Waste 3` = state.abb)

输出

lapply(lst1, function(x) lapply(lst2[x], function(.x) lst3[.x]))
$IT
$IT$Training
$IT$Training$`Alarm Engineer`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

$IT$Training$`Backend Engineer`
      Rural Male Rural Female Urban Male Urban Female
50-54       11.7          8.7       15.4          8.4
55-59       18.1         11.7       24.3         13.6
60-64       26.9         20.3       37.0         19.3
65-69       41.0         30.9       54.6         35.1
70-74       66.0         54.3       71.1         50.0


$IT$Helpdesk
$IT$Helpdesk$`Analyst 1`
# A tibble: 6 × 10
  carat       cut color clarity depth table price     x     y     z
  <dbl>     <ord> <ord>   <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1  0.23     Ideal     E     SI2  61.5    55   326  3.95  3.98  2.43
2  0.21   Premium     E     SI1  59.8    61   326  3.89  3.84  2.31
3  0.23      Good     E     VS1  56.9    65   327  4.05  4.07  2.31
4  0.29   Premium     I     VS2  62.4    58   334  4.20  4.23  2.63
5  0.31      Good     J     SI2  63.3    58   335  4.34  4.35  2.75
6  0.24 Very Good     J    VVS2  62.8    57   336  3.94  3.96  2.48

$IT$Helpdesk$`Analyst 2`
  weight Time Chick Diet
1     42    0     1    1
2     51    2     1    1
3     59    4     1    1
4     64    6     1    1
5     76    8     1    1
6     93   10     1    1