What are Lists in R?
At its core, a list in R is a ordered collection of objects. These objects can be of any type — vectors, matrices, data frames, functions, or even other lists. This heterogeneous nature of lists makes them incredibly flexible and useful for storing complex, hierarchical data structures.
Let’s start with the basics of creating lists:
# Creating a simple list
my_list <- list(1, "hello", TRUE)
> my_list
[[1]]
[1] 1
[[2]]
[1] "hello"
[[3]]
[1] TRUE
# Creating a named list
named_list <- list(number = 1, greeting = "hello", flag = TRUE)
> named_list
$number
[1] 1
$greeting
[1] "hello"
$flag
[1] TRUE
# Creating a nested list
nested_list <- list(
a = 1:3,
b = list(x = "inner", y = 1:5),
c = data.frame(col1 = 1:3, col2 = c("a", "b", "c"))
)
> nested_list
$a
[1] 1 2 3
$b
$b$x
[1] "inner"
$b$y
[1] 1 2 3 4 5
$c
col1 col2
1 1 a
2 2 b
3 3 c
Accessing List Elements:
There are several ways to access elements in a list:
# Using square brackets returns a list
my_list[1] # Returns a list containing the first element
> nested_list[1]
$a
[1] 1 2 3
# Using double square brackets returns the actual element
my_list[[1]] # Returns the first element
> nested_list[[1]]
[1] 1 2 3
# For named lists, you can use the $ operator
named_list$greeting
> named_list$greeting
[1] "hello"
# For nested lists, you can chain these methods
nested_list$b$x
> nested_list$b$x
[1] "inner"
nested_list[[2]][[1]]
> nested_list[[2]][[1]]
[1] "inner"
Use Cases for Lists:
- Heterogeneous Data Storage:
Lists are perfect for storing different types of data together. This is particularly useful when working with complex data structures that don’t fit neatly into a data frame.
customer <- list(
id = 1001,
name = "John Doe",
purchases = c(100, 200, 150),
active = TRUE
)
> customer
$id
[1] 1001
$name
[1] "John Doe"
$purchases
[1] 100 200 150
$active
[1] TRUE
2.Function Output:
Many R functions return lists, especially when there are multiple outputs to consider.
model <- lm(mpg ~ wt, data = mtcars)
> model # model is a list containing various components of the linear regression
Call:
lm(formula = mpg ~ wt, data = mtcars)
Coefficients:
(Intercept) wt
37.285 -5.344
3. Hierarchical Data:
Lists are excellent for representing hierarchical or nested data structures.
org_structure <- list(
CEO = "Alice",
departments = list(
HR = list(head = "Bob", staff = c("Charlie", "David")),
Finance = list(head = "Eve", staff = c("Frank", "Grace"))
)
)
> org_structure
$CEO
[1] "Alice"
$departments
$departments$HR
$departments$HR$head
[1] "Bob"
$departments$HR$staff
[1] "Charlie" "David"
$departments$Finance
$departments$Finance$head
[1] "Eve"
$departments$Finance$staff
[1] "Frank" "Grace"
4. Grouping Related Objects:
Lists can be used to group related objects together, making code more organized.
analysis_results <- list(
data = my_data,
summary_stats = summary(my_data),
plot = ggplot(my_data, aes(x, y)) + geom_point()
)
list(x = c(1.77243399663911, -0.957171783570236, -1.30781987157622, 0.975621346128874, 0.672415372296508, -1.69469623186671, 1.77119196103008, 1.01364881714182, 0.600605690122753, 0.561082121250972, 0.849556211887386, 0.548824532410027, -1.85308028040855, 0.528746868837219, 0.210915386692753, -1.99066438712621, -0.29450197905857, -0.924791227355562, 0.813006474942448, 0.474325931011057, 1.20467355773074, 0.137030923234252, 1.42128004563605, 2.06977487010915, 0.57222286392311, 0.209261632419258, 1.08877027331851,
1.92736373284175, -0.543508012958017, -0.64591677330739, 1.12870000357808, 1.21751351121052, -0.403486775605523, 1.00729065855514, -0.373174732246605, -0.470241022551513, 0.269282920255229, -0.800815043721378, 0.0483349298587044, 2.16148205177631, -0.515810111451257, -0.268537788431312, -0.00820014774468701, -0.31486543884096, -1.09408586250704, -0.941624209170282, 0.192233400379333, -0.308881242444631, -0.0253770780061488, -0.740313183787542, 1.24925895435034, 0.368225350113779, 0.597230111324093,
-0.898878606396072, -0.315058679250616, -1.49267370042434, -0.426819873234305, 0.571066384994836, 1.04506418983373, 0.331041863908888, -0.572980236558582, 0.57101313160017, 0.515244457517438, 0.637079520331072, 0.811287430327011, -0.0567869362456877, 0.875301028528586, 0.699965685571004, -2.0446308780908, -0.452650489250927, -2.32292272575982, 0.857408345420088, -0.285509995069787, -1.12872450085999, 0.492860993960119, -1.012399221897, -1.93744410614283, 0.263483625649209, 0.0211420630511362, 1.39733613882549,
0.120528783852613, -0.911882405974291, -1.19108257696864, -0.507592565744539, 1.3969982560065, 0.147458817487752, -0.206198717001761, 1.35158147942523, 1.47792288752332, 0.0252974703735057, -0.165550859746341, -2.14583100057112, -0.606322499214174, -0.724925102967893, -0.0238270929022796, -2.19566902004949, 0.531401204117166, -0.651870078700225, 0.884154955159433, -1.85082622372844, -0.442923407237248, 0.623358275459698, 2.05342211230634, 0.486798990370746, -1.76549754030673, 0.0160997279953694,
-3.51305257522904, -0.0049834353905531, 0.87089390220189, -0.0604833389100862, -0.535999891075349, -0.0395963385505113, -0.456031012748786, 0.234936386141955, 0.246954501207653, 0.951685647993676, 1.3864504679316, -0.130138112095399, 0.768605751424176, 0.0165901240994742, -0.683812951041049, -0.864433792451902, -0.888750201120109, 0.24185150712306, -0.472325268594831, -0.237464638986539, 0.376572431620225, 3.32925544592545, 0.415062680912624, -0.804834183716468, 0.785195035798557, 0.163082168561247,
0.2118691015112, 1.13675132054326, 1.11529542213161, 2.54862031204969, -0.955726209500125, 1.72891192455201, 0.538898790640644, -0.761259101844833, -0.0557977093617337, -2.0067927183057, 1.32181809726097, 0.690012961854672, 1.37963678416962, -0.462282541449904, 0.705166992555259, 1.89857014262161, -1.8849867255339, -0.820876416108461, -0.769093485449387, -2.34555108450502, 0.678431550529362, 1.36176168909342, 0.538796105200303, 1.29573057479735, 0.843817452826147, 0.581994025364985, 1.6406583531516,
0.986962201108519, -0.21333283893143, 1.50942134751824, 0.296589615152507, 1.40638201904109, -0.384904406830545, 0.472471659330873, -2.34133096371568, 0.954681289258098, 0.638926752684448, -0.795264978595347, 1.1236984350335, -1.26753372425505, -1.0703379437325, 2.23483711999654, 0.621041214732421, 1.23759119642934, 0.537804320286468, -0.61127808364256, 0.748691298449444, -1.28343701086066, -0.716528660703459, -1.54476609862428, -0.383583931174982, 0.190232028831775, 0.0188215237398464, -0.272249181278223,
0.725523448591963, 1.00005072781144, 1.11772706332611, 0.896311803838196, 3.10113420257182, 1.30067161240035, -1.12231424083927, -0.335797544618453, 0.175750344968409, -1.32611736228213, 0.890218764541144, 0.755063892042729, 0.0113889262969505, -0.082875121797806, -0.42117206235754, 0.0519216967932141, -1.28632937996257, 2.18250288926137, -1.65177794134772, 1.06474033289398, -0.0177077760604067, 0.903527050634917, 1.50468887197157, 2.02878347017762, 1.10402363330563, -1.48305656222203, -0.577088953727368,
-1.05344395338196, 0.957123518393137, -0.000756682904552264, -0.92962782114527, 0.639463526416009, -0.569366804711616, 0.144822925921756, 0.0282255879360549, 0.164463516218601, 0.565107388865772, 1.01977790416146, 0.41036462460009, -0.561524981763001, -0.729048554480159, 0.612212178998272, 1.96896209126205, 0.604447270099741, 0.875031148235442, 1.67987435554669, 0.66455658463937, 0.0800959187514061, 0.049069732526192, -0.897254230509069, 0.973102544225293, 0.849105488148745, -1.08918622418388, -0.3508628098643,
-1.21826784580921, 0.55832886174004, -1.04334101703623, 0.229340198577267, 0.338627025879642, -2.70723436139084, 0.700224071114113, 1.51786222842573, 0.399461338975629, 1.44680798674749, 0.639689895392517, -0.740745226243263, -0.946350167485638, -0.424402543302575, 0.359712404120917, -0.438593624630303, -0.80371937638882, -1.41558201201697, -1.37007844769737, 1.20355881338804, -0.253465587940196, 1.42177353296082, 1.34919844608897, -1.71997842975565, -0.670560107511332, -0.364049706451806, -0.183559506234407,
-0.995066906359394, -1.79051249383453, -0.798738179003635, 1.39121339159743, 1.17985327374427, 1.25186320926168, -0.0936543975925202, 1.93066966109682, 0.414979848327651, 1.42772747855975, -1.04338899713024, -0.986273176136756, 1.06013832021079, 1.35730096041565, 0.323670404274197, -1.01582599091034, -0.834973808085207, 0.247510082217372, 0.959328504308508, 0.928296176532029, 1.20963321738269, -0.0543907437749215, 0.491307838669514, -1.83037658538239, 1.12415195815802, -0.487597517262986, -0.701283845211302,
0.00596027523703735, -1.75767121620954, 0.210028950230481, 0.294983942543545, -0.67480897436032, -0.390654255112459), y = c(2.55238065240742, 1.34275516625689, 2.01486940808158, 2.18870780442822, 1.73530242143489, 1.52175640516392, 2.96348449620573, 1.99331949111728, 2.63347300562219, 1.52875359499735, 2.53319495862497, 1.72411699050073, 0.719591810563097, 1.83119844674792, 2.04914551170397, 1.64576093317262, 1.92360358761136, 2.65975750518858, 3.22372848128784, 1.94317548305707, 2.34236904548796,
1.77944614239984, 2.382358419378, 2.47690742036895, 2.14307552259864, 2.25833689669148, 2.10822244157616, 2.68793229942293, 1.5340868832141, 2.19882903420348, 2.98253448688018, 2.68794296606118, 2.03256297825725, 1.76421417381822, 2.22239915656641, 1.95608505956407, 1.999123257638, 2.3072160299512, 1.3743393413178, 2.91723000744516, 1.44134816255541, 2.11554823191613, 1.4639901763364, 1.62419785610498, 1.60099762064143, 1.99147741060282, 0.930395667830877, 2.24295448507088, 1.66393293407321, 1.87841790534215,
3.38960152834914, 2.62609753873541, 2.14592933235248, 2.24610119821543, 1.92411012497171, 1.49517184659155, 1.61153960506946, 1.82233003066057, 2.16197099736792, 2.56789096693365, 1.94980732755905, 2.14920250520038, 1.84618498737272, 2.10088069858679, 3.28922725493802, 1.84041361724407, 2.66283247651503, 2.47961106474713, 1.67329352012405, 2.40066584654588, 1.75453913150543, 2.89540873763089, 1.86151952441689, 1.36075784919348, 2.12242288155482, 1.45067958793874, 1.80000839746914, 1.38328871608674,
2.45576774409317, 3.20714160405229, 2.75034557968962, 1.36712460091011, 1.66907779025364, 1.8582718457452, 2.84662571080594, 2.06520966816866, 2.30539289924819, 1.9041841746132, 2.73654098056023, 2.38578658460933, 2.11752279055929, 1.95362994268694, 1.58410539497798, 0.549105886090498, 2.1713844279386, 1.46077610397925, 1.73018659172316, 2.07890055685363, 2.11680890541479, 1.44810242445947, 1.36902410265914, 1.82554698977673, 2.29185889419372, 2.20725068398207, 1.28139348997934, 1.83636676818531,
1.54200337688352, 2.24436933571425, 2.52840996376397, 3.34955255081168, 2.276768650876, 1.73069578329067, 1.81157038417197, 2.69720453233923, 2.16986163560469, 2.51084645911381, 2.30849875096077, 2.24083459458542, 2.95397053555053, 2.05993964678748, 2.16062570734203, 1.77512133037204, 1.77255633131803, 2.12185463585164, 0.912663092187497, 2.57250441457544, 1.21273012427133, 2.64428083136876, 2.01951341987271, 1.84520323160736, 2.04204237125828, 1.96328695699625, 2.959571687007, 2.23654539682942,
1.55225173645058, 2.20982866243537, 1.96634015772749, 2.21244513366298, 1.68793848336134, 1.44556395065559, 2.11933979867584, 1.85373606713551, 2.66979776047476, 1.49015586611641, 2.7999310821391, 2.3549635348954, 2.07311439880699, 2.43181884666409, 1.55300169977328, 1.86020786110773, 1.96715195984862, 0.112132609777493, 2.41943710035543, 2.00975467990418, 2.03870575516794, 2.41365331214186, 2.96516380443934, 2.75823189056874, 2.58021601203772, 2.0104460548142, 2.78827918031317, 2.53588316243594,
2.3679484087208, 3.22576476487256, 2.10438519462464, 2.12666010320628, 1.94672581871713, 2.13269976344659, 2.68508594511609, 1.07741064924446, 3.27607177599181, 1.20096752339716, 1.08176580923758, 3.3074541585076, 1.17557593722764, 2.43984023254975, 2.2047785799287, 1.75632793827827, 2.46875287351905, 0.804880923344775, 2.13535302918422, 1.60318425533026, 2.09378447095641, 1.5385060455118, 1.88310066257928, 2.35450956107931, 1.3000285151256, 1.45395583177101, 1.95795224776583, 2.26107415962664, 2.53594243626331,
2.85876342921463, 2.20196264623883, 1.82978786421579, 2.19028649398764, 0.709278247616624, 2.21448707286345, 1.64975552606043, 2.57605736620347, 1.91313423543998, 1.00396956205884, 1.10434935751197, 2.44068357458278, 1.98246212131513, 1.24947577378669, 1.77863318719625, 2.05298862360105, 2.78316733824525, 2.12267103006227, 2.38239524426416, 1.37564014863022, 1.1808805558027, 1.80977563983533, 1.87598098302548, 2.11544495758846, 2.66706530590878, 1.98625104205577, 1.58082863833455, 1.88060304476562,
2.29033521905895, 2.74428084132896, 2.14844589222292, 2.7667099944885, 2.76876431195877, 1.67205037987272, 2.41657027484847, 2.23346583105237, 1.87706060883448, 3.59470079908278, 2.43405080037039, 2.71602903135312, 2.88481993335182, 3.12026165230965, 2.31836895039151, 1.94174559886245, 1.39829293442718, 2.28695055092547, 2.22519636635801, 1.01798766856519, 0.477241310763485, 2.01732406631202, 2.11358660598443, 1.51963593143459, 2.33710431991472, 2.11682490598062, 2.0933891986507, 1.70853726232108,
2.02253041721988, 1.64765933544284, 3.23349899745457, 2.429036798516, 2.2041744763681, 1.17228454727752, 1.25742187105871, 2.42256715750602, 0.96042088953715, 1.10299052224847, 2.1994719916841, 2.30325134620398, 2.38003980159917, 2.34314482671983, 2.95751633735133, 2.3025817788974, 0.981626168090022, 2.1683219739829, 1.60242038904648, 1.51150225514767, 1.93157850108869, 2.09155085336837, 0.70876013134783, 2.93706952065504, 1.98048806664559, 2.48836286775539, 1.34211151118149, 3.2741641082747, 3.02112941116738,
2.21078734111001, 1.63821609753285, 1.47897809430963, 2.45497004051452, 2.10537143567229, 2.31043499263398, 2.55100713864394, 1.49313501130444, 1.75104592545868, 2.48488196674593, 3.38389107485361, 2.31460759774735, 2.98168877497308, 1.69723678780889, 1.61155836668668, 2.10073708033082, 2.17418462324605, 2.61509094988716, 1.76514373144799, 1.49219624222801, 1.09352342752441, 1.73506881491551, 2.01836115499055, 2.39258444167077))
c("Min. :-3.5131 ", "1st Qu.:-0.6474 ", "Median : 0.1701 ", "Mean : 0.1041 ", "3rd Qu.: 0.8751 ", "Max. : 3.3293 ", "Min. :0.1121 ", "1st Qu.:1.7057 ", "Median :2.0973 ", "Mean :2.0602 ", "3rd Qu.:2.4144 ", "Max. :3.5947 ")
list(data = list(x = c(1.77243399663911, -0.957171783570236, -1.30781987157622, 0.975621346128874, 0.672415372296508, -1.69469623186671, 1.77119196103008, 1.01364881714182, 0.600605690122753, 0.561082121250972, 0.849556211887386, 0.548824532410027, -1.85308028040855, 0.528746868837219, 0.210915386692753, -1.99066438712621, -0.29450197905857, -0.924791227355562, 0.813006474942448, 0.474325931011057, 1.20467355773074, 0.137030923234252, 1.42128004563605, 2.06977487010915, 0.57222286392311, 0.209261632419258,
1.08877027331851, 1.92736373284175, -0.543508012958017, -0.64591677330739, 1.12870000357808, 1.21751351121052, -0.403486775605523, 1.00729065855514, -0.373174732246605, -0.470241022551513, 0.269282920255229, -0.800815043721378, 0.0483349298587044, 2.16148205177631, -0.515810111451257, -0.268537788431312, -0.00820014774468701, -0.31486543884096, -1.09408586250704, -0.941624209170282, 0.192233400379333, -0.308881242444631, -0.0253770780061488, -0.740313183787542, 1.24925895435034, 0.368225350113779,
0.597230111324093, -0.898878606396072, -0.315058679250616, -1.49267370042434, -0.426819873234305, 0.571066384994836, 1.04506418983373, 0.331041863908888, -0.572980236558582, 0.57101313160017, 0.515244457517438, 0.637079520331072, 0.811287430327011, -0.0567869362456877, 0.875301028528586, 0.699965685571004, -2.0446308780908, -0.452650489250927, -2.32292272575982, 0.857408345420088, -0.285509995069787, -1.12872450085999, 0.492860993960119, -1.012399221897, -1.93744410614283, 0.263483625649209, 0.0211420630511362,
1.39733613882549, 0.120528783852613, -0.911882405974291, -1.19108257696864, -0.507592565744539, 1.3969982560065, 0.147458817487752, -0.206198717001761, 1.35158147942523, 1.47792288752332, 0.0252974703735057, -0.165550859746341, -2.14583100057112, -0.606322499214174, -0.724925102967893, -0.0238270929022796, -2.19566902004949, 0.531401204117166, -0.651870078700225, 0.884154955159433, -1.85082622372844, -0.442923407237248, 0.623358275459698, 2.05342211230634, 0.486798990370746, -1.76549754030673, 0.0160997279953694,
-3.51305257522904, -0.0049834353905531, 0.87089390220189, -0.0604833389100862, -0.535999891075349, -0.0395963385505113, -0.456031012748786, 0.234936386141955, 0.246954501207653, 0.951685647993676, 1.3864504679316, -0.130138112095399, 0.768605751424176, 0.0165901240994742, -0.683812951041049, -0.864433792451902, -0.888750201120109, 0.24185150712306, -0.472325268594831, -0.237464638986539, 0.376572431620225, 3.32925544592545, 0.415062680912624, -0.804834183716468, 0.785195035798557, 0.163082168561247,
0.2118691015112, 1.13675132054326, 1.11529542213161, 2.54862031204969, -0.955726209500125, 1.72891192455201, 0.538898790640644, -0.761259101844833, -0.0557977093617337, -2.0067927183057, 1.32181809726097, 0.690012961854672, 1.37963678416962, -0.462282541449904, 0.705166992555259, 1.89857014262161, -1.8849867255339, -0.820876416108461, -0.769093485449387, -2.34555108450502, 0.678431550529362, 1.36176168909342, 0.538796105200303, 1.29573057479735, 0.843817452826147, 0.581994025364985, 1.6406583531516,
0.986962201108519, -0.21333283893143, 1.50942134751824, 0.296589615152507, 1.40638201904109, -0.384904406830545, 0.472471659330873, -2.34133096371568, 0.954681289258098, 0.638926752684448, -0.795264978595347, 1.1236984350335, -1.26753372425505, -1.0703379437325, 2.23483711999654, 0.621041214732421, 1.23759119642934, 0.537804320286468, -0.61127808364256, 0.748691298449444, -1.28343701086066, -0.716528660703459, -1.54476609862428, -0.383583931174982, 0.190232028831775, 0.0188215237398464, -0.272249181278223,
0.725523448591963, 1.00005072781144, 1.11772706332611, 0.896311803838196, 3.10113420257182, 1.30067161240035, -1.12231424083927, -0.335797544618453, 0.175750344968409, -1.32611736228213, 0.890218764541144, 0.755063892042729, 0.0113889262969505, -0.082875121797806, -0.42117206235754, 0.0519216967932141, -1.28632937996257, 2.18250288926137, -1.65177794134772, 1.06474033289398, -0.0177077760604067, 0.903527050634917, 1.50468887197157, 2.02878347017762, 1.10402363330563, -1.48305656222203, -0.577088953727368,
-1.05344395338196, 0.957123518393137, -0.000756682904552264, -0.92962782114527, 0.639463526416009, -0.569366804711616, 0.144822925921756, 0.0282255879360549, 0.164463516218601, 0.565107388865772, 1.01977790416146, 0.41036462460009, -0.561524981763001, -0.729048554480159, 0.612212178998272, 1.96896209126205, 0.604447270099741, 0.875031148235442, 1.67987435554669, 0.66455658463937, 0.0800959187514061, 0.049069732526192, -0.897254230509069, 0.973102544225293, 0.849105488148745, -1.08918622418388, -0.3508628098643,
-1.21826784580921, 0.55832886174004, -1.04334101703623, 0.229340198577267, 0.338627025879642, -2.70723436139084, 0.700224071114113, 1.51786222842573, 0.399461338975629, 1.44680798674749, 0.639689895392517, -0.740745226243263, -0.946350167485638, -0.424402543302575, 0.359712404120917, -0.438593624630303, -0.80371937638882, -1.41558201201697, -1.37007844769737, 1.20355881338804, -0.253465587940196, 1.42177353296082, 1.34919844608897, -1.71997842975565, -0.670560107511332, -0.364049706451806, -0.183559506234407,
-0.995066906359394, -1.79051249383453, -0.798738179003635, 1.39121339159743, 1.17985327374427, 1.25186320926168, -0.0936543975925202, 1.93066966109682, 0.414979848327651, 1.42772747855975, -1.04338899713024, -0.986273176136756, 1.06013832021079, 1.35730096041565, 0.323670404274197, -1.01582599091034, -0.834973808085207, 0.247510082217372, 0.959328504308508, 0.928296176532029, 1.20963321738269, -0.0543907437749215, 0.491307838669514, -1.83037658538239, 1.12415195815802, -0.487597517262986, -0.701283845211302,
0.00596027523703735, -1.75767121620954, 0.210028950230481, 0.294983942543545, -0.67480897436032, -0.390654255112459), y = c(2.55238065240742, 1.34275516625689, 2.01486940808158, 2.18870780442822, 1.73530242143489, 1.52175640516392, 2.96348449620573, 1.99331949111728, 2.63347300562219, 1.52875359499735, 2.53319495862497, 1.72411699050073, 0.719591810563097, 1.83119844674792, 2.04914551170397, 1.64576093317262, 1.92360358761136, 2.65975750518858, 3.22372848128784, 1.94317548305707, 2.34236904548796,
1.77944614239984, 2.382358419378, 2.47690742036895, 2.14307552259864, 2.25833689669148, 2.10822244157616, 2.68793229942293, 1.5340868832141, 2.19882903420348, 2.98253448688018, 2.68794296606118, 2.03256297825725, 1.76421417381822, 2.22239915656641, 1.95608505956407, 1.999123257638, 2.3072160299512, 1.3743393413178, 2.91723000744516, 1.44134816255541, 2.11554823191613, 1.4639901763364, 1.62419785610498, 1.60099762064143, 1.99147741060282, 0.930395667830877, 2.24295448507088, 1.66393293407321, 1.87841790534215,
3.38960152834914, 2.62609753873541, 2.14592933235248, 2.24610119821543, 1.92411012497171, 1.49517184659155, 1.61153960506946, 1.82233003066057, 2.16197099736792, 2.56789096693365, 1.94980732755905, 2.14920250520038, 1.84618498737272, 2.10088069858679, 3.28922725493802, 1.84041361724407, 2.66283247651503, 2.47961106474713, 1.67329352012405, 2.40066584654588, 1.75453913150543, 2.89540873763089, 1.86151952441689, 1.36075784919348, 2.12242288155482, 1.45067958793874, 1.80000839746914, 1.38328871608674,
2.45576774409317, 3.20714160405229, 2.75034557968962, 1.36712460091011, 1.66907779025364, 1.8582718457452, 2.84662571080594, 2.06520966816866, 2.30539289924819, 1.9041841746132, 2.73654098056023, 2.38578658460933, 2.11752279055929, 1.95362994268694, 1.58410539497798, 0.549105886090498, 2.1713844279386, 1.46077610397925, 1.73018659172316, 2.07890055685363, 2.11680890541479, 1.44810242445947, 1.36902410265914, 1.82554698977673, 2.29185889419372, 2.20725068398207, 1.28139348997934, 1.83636676818531,
1.54200337688352, 2.24436933571425, 2.52840996376397, 3.34955255081168, 2.276768650876, 1.73069578329067, 1.81157038417197, 2.69720453233923, 2.16986163560469, 2.51084645911381, 2.30849875096077, 2.24083459458542, 2.95397053555053, 2.05993964678748, 2.16062570734203, 1.77512133037204, 1.77255633131803, 2.12185463585164, 0.912663092187497, 2.57250441457544, 1.21273012427133, 2.64428083136876, 2.01951341987271, 1.84520323160736, 2.04204237125828, 1.96328695699625, 2.959571687007, 2.23654539682942,
1.55225173645058, 2.20982866243537, 1.96634015772749, 2.21244513366298, 1.68793848336134, 1.44556395065559, 2.11933979867584, 1.85373606713551, 2.66979776047476, 1.49015586611641, 2.7999310821391, 2.3549635348954, 2.07311439880699, 2.43181884666409, 1.55300169977328, 1.86020786110773, 1.96715195984862, 0.112132609777493, 2.41943710035543, 2.00975467990418, 2.03870575516794, 2.41365331214186, 2.96516380443934, 2.75823189056874, 2.58021601203772, 2.0104460548142, 2.78827918031317, 2.53588316243594,
2.3679484087208, 3.22576476487256, 2.10438519462464, 2.12666010320628, 1.94672581871713, 2.13269976344659, 2.68508594511609, 1.07741064924446, 3.27607177599181, 1.20096752339716, 1.08176580923758, 3.3074541585076, 1.17557593722764, 2.43984023254975, 2.2047785799287, 1.75632793827827, 2.46875287351905, 0.804880923344775, 2.13535302918422, 1.60318425533026, 2.09378447095641, 1.5385060455118, 1.88310066257928, 2.35450956107931, 1.3000285151256, 1.45395583177101, 1.95795224776583, 2.26107415962664, 2.53594243626331,
2.85876342921463, 2.20196264623883, 1.82978786421579, 2.19028649398764, 0.709278247616624, 2.21448707286345, 1.64975552606043, 2.57605736620347, 1.91313423543998, 1.00396956205884, 1.10434935751197, 2.44068357458278, 1.98246212131513, 1.24947577378669, 1.77863318719625, 2.05298862360105, 2.78316733824525, 2.12267103006227, 2.38239524426416, 1.37564014863022, 1.1808805558027, 1.80977563983533, 1.87598098302548, 2.11544495758846, 2.66706530590878, 1.98625104205577, 1.58082863833455, 1.88060304476562,
2.29033521905895, 2.74428084132896, 2.14844589222292, 2.7667099944885, 2.76876431195877, 1.67205037987272, 2.41657027484847, 2.23346583105237, 1.87706060883448, 3.59470079908278, 2.43405080037039, 2.71602903135312, 2.88481993335182, 3.12026165230965, 2.31836895039151, 1.94174559886245, 1.39829293442718, 2.28695055092547, 2.22519636635801, 1.01798766856519, 0.477241310763485, 2.01732406631202, 2.11358660598443, 1.51963593143459, 2.33710431991472, 2.11682490598062, 2.0933891986507, 1.70853726232108,
2.02253041721988, 1.64765933544284, 3.23349899745457, 2.429036798516, 2.2041744763681, 1.17228454727752, 1.25742187105871, 2.42256715750602, 0.96042088953715, 1.10299052224847, 2.1994719916841, 2.30325134620398, 2.38003980159917, 2.34314482671983, 2.95751633735133, 2.3025817788974, 0.981626168090022, 2.1683219739829, 1.60242038904648, 1.51150225514767, 1.93157850108869, 2.09155085336837, 0.70876013134783, 2.93706952065504, 1.98048806664559, 2.48836286775539, 1.34211151118149, 3.2741641082747, 3.02112941116738,
2.21078734111001, 1.63821609753285, 1.47897809430963, 2.45497004051452, 2.10537143567229, 2.31043499263398, 2.55100713864394, 1.49313501130444, 1.75104592545868, 2.48488196674593, 3.38389107485361, 2.31460759774735, 2.98168877497308, 1.69723678780889, 1.61155836668668, 2.10073708033082, 2.17418462324605, 2.61509094988716, 1.76514373144799, 1.49219624222801, 1.09352342752441, 1.73506881491551, 2.01836115499055, 2.39258444167077)), layers = list(<environment>), scales = <environment>, guides = <environment>,
mapping = list(x = ~x, y = ~y), theme = list(), coordinates = <environment>, facet = <environment>, plot_env = <environment>, layout = <environment>, labels = list(x = "x", y = "y"))
List Manipulation:
R provides several functions for working with lists:
lapply()
: Apply a function to each element of a list.
lapply(nested_list, class)
> lapply(nested_list, class)
$a
[1] "integer"
$b
[1] "list"
$c
[1] "data.frame"
sapply()
: Similar to lapply()
, but tries to simplify the output.
sapply(nested_list, length)
> sapply(nested_list, length)
a b c
3 2 2
unlist()
: Flatten a list into a vector.
> unlist(nested_list)
a1 a2 a3 b.x b.y1 b.y2 b.y3 b.y4 b.y5 c.col11
"1" "2" "3" "inner" "1" "2" "3" "4" "5" "1"
c.col12 c.col13 c.col21 c.col22 c.col23
"2" "3" "a" "b" "c"
do.call()
: Construct and execute a function call from a list of arguments.
do.call(sum, list(1, 2, 3, 4))
[1] 10
Advantages of Lists:
- Flexibility: Lists can store any type of R object, making them incredibly versatile.
- Hierarchical Structure: Lists can be nested, allowing for complex data structures.
- Named Elements: List elements can be named, improving readability and access.
- Preservation of Data Types: Unlike data frames, lists preserve the original data types of their elements.
- Function Compatibility: Many R functions work well with lists or return lists.
Disadvantages of Lists:
- Complexity: The flexibility of lists can make them more complex to work with compared to simpler data structures.
- Memory Usage: Lists can be less memory-efficient than more specialized data structures for certain tasks.
- Printing: Large or complex lists can be difficult to view or print in a readable format.
- Performance: Operations on lists can be slower than on more specialized data structures for certain tasks.
Happy listing!