Olivia Olivia - 1 month ago 14
R Question

R Turning list characters into other lists

First I have small nested list of length 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"


Then I have a second nested list (length 250):

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


Then I have a final list, this list is a list of data frames all with one common variable but with sifferent numbers of observations:

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

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


Now you may have spotted that in the first list, under IT we see Helpdesk. And under Construction, we see Mechanical and Electrical

I want to turn these from characters, into the lists in list two.

Then from there we see in list two under Mechanical and Electrical we have Electricianand under Helpdesk we see 1st Line Helpdesk Support. I'd then want those characters to be turned into the data.frames in list 3.

It may have been stupid of me the make the lists I made and go from there. I can obviously make simple lists of 35 and 250 and I do also have a data frame like:

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


And then a folder full of csv's that are each named after each norm row above and contain data.frames which were used to make the list of data.frames in list 3.

What I would like is:

>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

Answer

We can nest a subset.

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

Data

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)

Output

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