title: What is wrong with my lm function using lapply()?
body: <p>I'm having a dataset and want to create a linear regression model for each state (column called fylke). However, when using lapply together with a function, it does say: <code>unexpected '}'</code> I created first a list and used then lapply to loop through the list saving a regression model for each state. I cannot see why my code is wrong since I need to close the &quot;{&quot; with a &quot;}&quot;.</p>
  <p>Can someone tell me what is wrong with my code?</p>
  <pre><code>fylker &lt;- list(&quot;Agder&quot;, &quot;Innlandet&quot;, &quot;Møre og Romsdal&quot;, &quot;Nordland&quot;, &quot;Rogaland&quot;, &quot;Troms og Finnmark&quot;,
                 &quot;Trøndelag&quot;, &quot;Vestfold og Telemark&quot;, &quot;Vestland&quot;, &quot;Viken&quot;)
  
  reg_model &lt;- lapply(fylker, function(x) {
    lm(df$hoyre ~ df$befolkningstetthet, subset = (df$fylke == x)
  })
  </code></pre>
  <p>Here is a reproducible example:</p>
  <pre><code>structure(list(kommuner = c(&quot;Halden&quot;, &quot;Moss&quot;, &quot;Sarpsborg&quot;, &quot;Fredrikstad&quot;, 
  &quot;Drammen&quot;, &quot;Kongsberg&quot;, &quot;Ringerike&quot;, &quot;Hvaler&quot;, &quot;Aremark&quot;, &quot;Marker&quot;, 
  &quot;Indre �\u0098stfold&quot;, &quot;Skiptvet&quot;, &quot;Rakkestad&quot;, &quot;Råde&quot;, &quot;Våler (Viken)&quot;, 
  &quot;Vestby&quot;, &quot;Nordre Follo&quot;, &quot;�\u0085s&quot;, &quot;Frogn&quot;, &quot;Nesodden&quot;, &quot;Bærum&quot;, 
  &quot;Asker&quot;, &quot;Aurskog-Høland&quot;, &quot;Rælingen&quot;, &quot;Enebakk&quot;, &quot;Lørenskog&quot;, 
  &quot;Lillestrøm&quot;, &quot;Nittedal&quot;, &quot;Gjerdrum&quot;, &quot;Ullensaker&quot;, &quot;Nes&quot;, &quot;Eidsvoll&quot;, 
  &quot;Nannestad&quot;, &quot;Hurdal&quot;, &quot;Hole&quot;, &quot;Flå&quot;, &quot;Nesbyen&quot;, &quot;Gol&quot;, &quot;Hemsedal&quot;, 
  &quot;�\u0085l&quot;, &quot;Hol&quot;, &quot;Sigdal&quot;, &quot;Krødsherad&quot;, &quot;Modum&quot;, &quot;�\u0098vre Eiker&quot;, 
  &quot;Lier&quot;, &quot;Flesberg&quot;, &quot;Rollag&quot;, &quot;Nore og Uvdal&quot;, &quot;Jevnaker&quot;, &quot;Lunner&quot;, 
  &quot;Oslo&quot;, &quot;Kongsvinger&quot;, &quot;Hamar&quot;, &quot;Lillehammer&quot;, &quot;Gjøvik&quot;, &quot;Ringsaker&quot;, 
  &quot;Løten&quot;, &quot;Stange&quot;, &quot;Nord-Odal&quot;, &quot;Sør-Odal&quot;, &quot;Eidskog&quot;, &quot;Grue&quot;, 
  &quot;�\u0085snes&quot;, &quot;Våler (Innlandet)&quot;, &quot;Elverum&quot;, &quot;Trysil&quot;, &quot;�\u0085mot&quot;, 
  &quot;Stor-Elvdal&quot;, &quot;Rendalen&quot;, &quot;Engerdal&quot;, &quot;Tolga&quot;, &quot;Tynset&quot;, &quot;Alvdal&quot;, 
  &quot;Folldal&quot;, &quot;Os&quot;, &quot;Dovre&quot;, &quot;Lesja&quot;, &quot;Skjåk&quot;, &quot;Lom&quot;, &quot;Vågå&quot;, 
  &quot;Nord-Fron&quot;, &quot;Sel&quot;, &quot;Sør-Fron&quot;, &quot;Ringebu&quot;, &quot;�\u0098yer&quot;, &quot;Gausdal&quot;, 
  &quot;�\u0098stre Toten&quot;, &quot;Vestre Toten&quot;, &quot;Gran&quot;, &quot;Søndre Land&quot;, 
  &quot;Nordre Land&quot;, &quot;Sør-Aurdal&quot;, &quot;Etnedal&quot;, &quot;Nord-Aurdal&quot;, &quot;Vestre Slidre&quot;, 
  &quot;�\u0098ystre Slidre&quot;, &quot;Vang&quot;, &quot;Horten&quot;, &quot;Holmestrand&quot;), fylker = c(&quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, &quot;Viken&quot;, 
  &quot;Viken&quot;, &quot;Oslo&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Innlandet&quot;, 
  &quot;Innlandet&quot;, &quot;Innlandet&quot;, &quot;Vestfold og Telemark&quot;, &quot;Vestfold og Telemark&quot;
  ), arbeidsledighet = c(2.9, 2.7, 3.2625, 2.975, 2.5125, 1.8375, 
  1.9375, 1.4125, 1.5375, 2.075, 2.5, 1.65, 2.7125, 2.0625, 2.0625, 
  1.65, 2.0125, 2.1875, 1.6, 1.95, 1.6375, 1.7625, 1.9625, 1.9375, 
  2.125, 2.6875, 2.275, 1.725, 1.7125, 3.2125, 1.7625, 2.1, 2.425, 
  2, 1.6625, 1.21666666666667, 1.0625, 0.825, 1.075, 0.675, 1.275, 
  0.825, 1.9875, 2, 1.4625, 1.65, 0.925, 1.275, 0.8625, 1.2875, 
  1.4375, 2.4, 1.575, 2, 1.875, 1.125, 1.75, 1.4125, 1.9625, 1.7, 
  2.025, 1.5875, 2.2625, 2.8125, 1.2, 2.575, 1.8125, 2.6125, 1.8375, 
  1.6, 0.9625, 1.225, 1.325, 0.775, 0.7375, 0.9, 1.2, 1.1625, 0.78, 
  0.775, 1.24285714285714, 1.525, 1.5, 1.8125, 0.95, 1.225, 1.3375, 
  0.95, 1.525, 1.6, 1.6125, 2.025, 1.575, 0.7125, 1.4, 1.45, 0.95, 
  0.975, 3.1, 2.2125), utdanningsniva = c(28.9, 32, 26.4, 31.4, 
  32.8, 39.1, 28.4, 30.8, 20.4, 21, 23.8, 20, 20.2, 27.4, 26.4, 
  35.5, 42.5, 46.3, 39.2, 48.6, 54.6, 46.2, 21, 34.4, 25.7, 36.8, 
  33.5, 38.2, 32.2, 29.3, 24.1, 27.5, 24.8, 22.6, 40.7, 22.1, 24.3, 
  23.6, 33.6, 27.5, 27.7, 18.4, 23.9, 23.3, 26.9, 37.6, 22.7, 23.6, 
  20.1, 24.8, 26.8, 54, 25.1, 40.5, 42.6, 32.2, 25.8, 23.6, 30.7, 
  18.2, 21.6, 17.8, 19.2, 20.4, 19.6, 31.7, 23.2, 24.2, 25.1, 21.1, 
  20.7, 33.1, 31.2, 24, 26.6, 27.5, 21, 24.8, 22.1, 23.9, 22.3, 
  21.7, 21.3, 21.5, 21.7, 26.3, 23, 25.3, 23, 23.6, 21.3, 19.6, 
  21.4, 22.1, 25.2, 23.7, 26.6, 28, 33.9, 29.2), befolkningstetthet = c(53, 
  388, 155, 293, 334, 37, 22, 52, 5, 10, 60, 41, 20, 72, 24, 136, 
  306, 202, 188, 325, 678, 261, 17, 334, 57, 638, 212, 137, 86, 
  162, 38, 67, 45, 11, 50, 2, 4, 9, 4, 4, 3, 4, 7, 31, 47, 96, 
  5, 3, 1, 35, 33, 1636, 19, 93, 63, 48, 31, 21, 33, 11, 17, 10, 
  6, 7, 5, 18, 2, 3, 1, 1, 1, 1, 3, 3, 1, 2, 2, 1, 1, 1, 3, 5, 
  6, 4, 4, 8, 5, 31, 58, 21, 8, 7, 3, 3, 7, 5, 4, 1, 399, 61), 
      innvandrere = c(15.74, 21.02, 21.31, 19.51, 28.5, 17.21, 
      16.74, 5.94, 4.83, 10.05, 19.85, 11.84, 15.39, 14.81, 16.73, 
      19, 19.85, 24.34, 13.76, 15.7, 22.74, 20.79, 16.07, 27.11, 
      20.12, 32.85, 27.79, 19.74, 17.86, 30.22, 16.36, 19.25, 25.74, 
      11.21, 15.55, 16.21, 12.54, 17.77, 26.32, 11.21, 18.09, 9.81, 
      14.47, 13.23, 16.05, 23.16, 11.39, 8.37, 10.45, 12.12, 13.04, 
      33.75, 14.4, 14.55, 14.01, 15.98, 11.22, 10.11, 12.46, 5.32, 
      9.62, 9.33, 9.46, 8.04, 8.75, 12.12, 13.07, 8.94, 12.66, 
      8.1, 9.04, 14.78, 12.84, 8.48, 6.32, 12.89, 8.84, 7.73, 6.32, 
      5.44, 6.23, 8.33, 6.97, 11.98, 11.34, 15.06, 7.72, 10.36, 
      10.78, 11.32, 9.95, 8.02, 8.23, 8.43, 10.82, 10.09, 12.3, 
      17.1, 16.78, 14.78), hoyre = c(16.8, 23.1, 16.4, 18.7, 24.6, 
      19.8, 19.8, 23.1, 10.1, 12, 19.1, 12.9, 12.3, 22.2, 18.6, 
      24.5, 28.6, 19.2, 33.7, 20, 40.3, 33.2, 12.8, 22.1, 20.6, 
      24.4, 22.6, 24.1, 24.1, 22.1, 13.4, 17, 17.1, 12.6, 31.6, 
      17.8, 15.1, 20.1, 20.5, 13.8, 23, 14.1, 17.3, 14.3, 19.8, 
      29.6, 13.2, 7.5, 11.4, 17.4, 16.9, 23.5, 12.6, 14.8, 16.4, 
      15.9, 11.7, 6.8, 9.2, 6.5, 11.2, 10.3, 8.6, 7.1, 5.5, 11.6, 
      10.6, 8.8, 7, 4.9, 8.3, 3.9, 6.3, 8.5, 4.3, 3, 7.2, 5, 4.3, 
      4.6, 5.2, 9.2, 7.8, 5.7, 7, 9.1, 6.2, 11.7, 12.9, 17, 10.7, 
      9.2, 9.7, 9, 14.9, 10.8, 13.1, 11.5, 21.5, 21.9)), row.names = c(NA, 
  -100L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>Thank you!</p>