4

I have already checked the other questions with on this issue, but since the problem seems to be very specific they weren't helpful.

I have a dataframe like this (this is just a quick example, example data from dput() is provided below):

year species abundance site county
2005 A       2         SH1  Göttingen
2006 A       0         SH1  Göttingen
2007 A       NA        SH1  Göttingen
2008 A       2         SH1  Göttingen
2009 A       NA        SH1  Göttingen
2010 A       2         SH1  Göttingen
2011 A       NA        SH1  Göttingen
2005 B       2         SH1  Göttingen
2006 B       0         SH1  Göttingen
2007 B       NA        SH1  Göttingen
2008 B       2         SH1  Göttingen
2009 B       NA        SH1  Göttingen
2010 B       2         SH1  Göttingen
2011 B       NA        SH1  Göttingen
2005 A       2         SH1  Göttingen
2006 A       0         SH1  Göttingen
2007 A       NA        SH1  Göttingen
2008 A       2         SH1  Göttingen
2009 A       NA        SH1  Göttingen
2010 A       2         SH1  Göttingen
2011 A       NA        SH1  Göttingen
2005 A       2         SH2  Göttingen
2006 A       0         SH2  Göttingen
2007 A       NA        SH2  Göttingen
2008 A       2         SH2  Göttingen
2009 A       NA        SH2  Göttingen
2010 A       2         SH2  Göttingen
2011 A       NA        SH2  Göttingen

It contains the abundance for 11 species on several different sites per county (more than 1500 sites in over 400ish counties) for each year 2005-2011. for each site, in every county, every year, all species have been accounted for, so there is either an NA, or a number in abundance for every year. The number of sites varies per county.

I would like to run the following loop to put the abundance into several columns: It should create a linear model to calculate population trends over these years and put the output in an additional row. In the end I would like to have a trend for every species on every site over the years:

alldata_lm$slope_abundance_plot <- NA
alldata_lm$p_slope_abundance_plot <- NA

species <- unique(alldata_lm$species)
sites <- unique(alldata_lm$site)

for (i in (1:length(species))) {
  for (k in(1:length(sites))) {        
    print(c(i,k))
    lm1 <-  lm(abundance ~ year, data = alldata_lm[alldata_lm$species == species[i] & alldata_lm$site == sites[k],], na.action=na.omit)
    alldata_lm$slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coefficients(lm1)[2]
    if (nrow(coef(summary(lm1)))>1){ alldata_lm$p_slope_abundance_plot[alldata_lm$species == species[i] & alldata_lm$site == sites[k]] <- coef(summary(lm1))[2,4]}
  }  
}

However, when I do, it returns the following error:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  alle Fälle NA

The same loop works perfectly with a very similar dataframe, the only difference is that the current dataframe contains far more NA's.

Deleting the NA's prior to running the loop does not help. I get the error message no matter if there are any NA's in the abundance column or not. I think the error might occur somewhere else. The year column does never contain any missing values.

I'd greatly appreciate any help! Thanks

EXAMPLE DATA

 structure(list(site = structure(c(700L, 700L, 700L, 700L, 700L, 
700L, 700L), .Label = c("bb1", "bb100", "bb101", "bb104", "bb107", 
"bb108", "bb109", "bb11", "bb111", "bb113", "bb115", "bb116", 
"bb117", "bb118", "bb119", "bb120", "bb121", "bb122", "bb123", 
"bb124", "bb125", "bb126", "bb127", "bb129", "bb130", "bb131", 
"bb132", "bb134", "bb135", "bb138", "bb139", "bb14", "bb140", 
"bb143", "bb147", "bb15", "bb150", "bb152", "bb154", "bb155", 
"bb156", "bb157", "bb158", "bb159", "bb163", "bb164", "bb166", 
"bb167", "bb169", "bb170", "bb171", "bb172", "bb173", "bb174", 
"bb175", "bb176", "bb177", "bb178", "bb179", "bb180", "bb181", 
"bb183", "bb186", "bb187", "bb188", "bb19", "bb191", "bb192", 
"bb193", "bb194", "bb197", "bb198", "bb199", "bb20", "bb200", 
"bb201", "bb202", "bb203", "bb204", "bb205", "bb207", "bb208", 
"bb209", "bb21", "bb210", "bb211", "bb212", "bb213", "bb215", 
"bb216", "bb217", "bb218", "bb219", "bb220", "bb221", "bb224", 
"bb225", "bb228", "bb23", "bb230", "bb232", "bb234", "bb237", 
"bb239", "bb242", "bb27", "bb32", "bb35", "bb37", "bb38", "bb39", 
"bb4", "bb40", "bb41", "bb47", "bb49", "bb53", "bb55", "bb58", 
"bb59", "bb6", "bb60", "bb63", "bb65", "bb66", "bb7", "bb70", 
"bb72", "bb73", "bb76", "bb77", "bb79", "bb8", "bb80", "bb81", 
"bb82", "bb84", "bb85", "bb87", "bb89", "bb9", "bb90", "bb91", 
"bb92", "bb93", "bb94", "bb96", "bb97", "bb98", "be14", "be15", 
"be17", "be30", "bw10", "bw100", "bw104", "bw108", "bw111", "bw112", 
"bw12", "bw120", "bw124", "bw126", "bw13", "bw144", "bw146", 
"bw175", "bw183", "bw192", "bw193", "bw198", "bw199", "bw200", 
"bw202", "bw208", "bw210", "bw211", "bw213", "bw215", "bw219", 
"bw225", "bw226", "bw229", "bw236", "bw243", "bw257", "bw262", 
"bw266", "bw268", "bw283", "bw294", "bw3", "bw30", "bw307", "bw326", 
"bw327", "bw338", "bw339", "bw341", "bw35", "bw36", "bw360", 
"bw368", "bw380", "bw381", "bw397", "bw405", "bw42", "bw53", 
"bw58", "bw6", "bw7", "bw84", "bw89", "bw91", "bw92", "bw96", 
"bw97", "by10", "by103", "by109", "by11", "by110", "by111", "by112", 
"by113", "by114", "by115", "by116", "by117", "by118", "by120", 
"by122", "by125", "by126", "by127", "by128", "by129", "by130", 
"by134", "by137", "by14", "by142", "by144", "by146", "by147", 
"by150", "by151", "by152", "by153", "by154", "by155", "by156", 
"by157", "by158", "by159", "by163", "by164", "by166", "by167", 
"by169", "by170", "by171", "by173", "by175", "by176", "by177", 
"by178", "by18", "by180", "by182", "by186", "by187", "by188", 
"by19", "by192", "by193", "by194", "by197", "by200", "by203", 
"by205", "by210", "by212", "by215", "by22", "by222", "by223", 
"by225", "by226", "by229", "by230", "by231", "by233", "by234", 
"by236", "by238", "by239", "by24", "by240", "by241", "by242", 
"by243", "by247", "by248", "by25", "by250", "by251", "by255", 
"by257", "by267", "by268", "by271", "by272", "by274", "by275", 
"by278", "by279", "by28", "by280", "by283", "by284", "by285", 
"by286", "by287", "by289", "by29", "by290", "by291", "by292", 
"by293", "by294", "by295", "by298", "by30", "by303", "by305", 
"by307", "by308", "by310", "by32", "by321", "by322", "by323", 
"by324", "by326", "by331", "by333", "by334", "by337", "by34", 
"by341", "by346", "by347", "by350", "by352", "by356", "by357", 
"by36", "by368", "by37", "by370", "by376", "by378", "by39", "by4", 
"by40", "by41", "by43", "by451", "by452", "by47", "by5", "by52", 
"by53", "by56", "by63", "by64", "by65", "by66", "by67", "by68", 
"by69", "by7", "by72", "by74", "by76", "by79", "by8", "by80", 
"by87", "by88", "by89", "by90", "by91", "by92", "by96", "by97", 
"by98", "hb16", "hb17", "hb18", "hb21", "hb30", "hb5", "hb6", 
"hb7", "hb9", "he100", "he103", "he106", "he107", "he108", "he109", 
"he110", "he111", "he113", "he114", "he115", "he116", "he119", 
"he120", "he122", "he124", "he13", "he130", "he137", "he14", 
"he144", "he145", "he150", "he154", "he18", "he37", "he42", "he46", 
"he47", "he51", "he52", "he66", "he68", "he7", "he70", "he72", 
"he73", "he75", "he82", "he83", "he84", "he85", "he89", "he9", 
"he91", "he93", "he94", "he96", "he97", "hh10", "hh28", "hh29", 
"hh30", "hh41", "hh44", "mv108", "mv109", "mv110", "mv122", "mv124", 
"mv125", "mv126", "mv141", "mv143", "mv15", "mv153", "mv156", 
"mv160", "mv17", "mv24", "mv29", "mv40", "mv41", "mv50", "mv55", 
"mv61", "mv63", "mv76", "mv82", "ni10", "ni100", "ni101", "ni102", 
"ni11", "ni110", "ni111", "ni119", "ni122", "ni125", "ni126", 
"ni13", "ni131", "ni134", "ni135", "ni136", "ni138", "ni14", 
"ni142", "ni146", "ni147", "ni149", "ni15", "ni150", "ni152", 
"ni162", "ni163", "ni166", "ni167", "ni168", "ni169", "ni170", 
"ni171", "ni172", "ni175", "ni182", "ni187", "ni188", "ni189", 
"ni191", "ni192", "ni193", "ni198", "ni2", "ni20", "ni206", "ni215", 
"ni218", "ni225", "ni226", "ni227", "ni231", "ni236", "ni239", 
"ni240", "ni241", "ni242", "ni243", "ni244", "ni246", "ni252", 
"ni257", "ni26", "ni260", "ni263", "ni272", "ni274", "ni282", 
"ni286", "ni290", "ni291", "ni297", "ni298", "ni299", "ni3", 
"ni303", "ni32", "ni34", "ni37", "ni38", "ni39", "ni4", "ni40", 
"ni41", "ni43", "ni45", "ni453", "ni455", "ni46", "ni47", "ni49", 
"ni50", "ni52", "ni55", "ni6", "ni61", "ni63", "ni66", "ni68", 
"ni71", "ni72", "ni73", "ni76", "ni77", "ni85", "ni87", "ni88", 
"ni89", "ni90", "ni91", "ni92", "ni93", "ni95", "ni97", "nw10", 
"nw108", "nw110", "nw112", "nw126", "nw13", "nw130", "nw140", 
"nw142", "nw143", "nw149", "nw154", "nw156", "nw173", "nw182", 
"nw20", "nw25", "nw38", "nw41", "nw5", "nw50", "nw52", "nw53", 
"nw54", "nw55", "nw6", "nw60", "nw63", "nw7", "nw72", "nw73", 
"nw74", "nw84", "nw86", "nw92", "rp101", "rp102", "rp103", "rp106", 
"rp108", "rp109", "rp116", "rp117", "rp120", "rp130", "rp131", 
"rp139", "rp140", "rp143", "rp144", "rp146", "rp21", "rp22", 
"rp23", "rp24", "rp36", "rp4", "rp84", "rp94", "rp99", "sh100", 
"sh101", "sh102", "sh103", "sh106", "sh109", "sh11", "sh111", 
"sh112", "sh117", "sh12", "sh121", "sh123", "sh125", "sh128", 
"sh130", "sh132", "sh14", "sh140", "sh17", "sh18", "sh19", "sh20", 
"sh25", "sh26", "sh27", "sh30", "sh33", "sh34", "sh35", "sh36", 
"sh37", "sh39", "sh42", "sh43", "sh44", "sh45", "sh46", "sh47", 
"sh51", "sh52", "sh54", "sh55", "sh58", "sh59", "sh60", "sh61", 
"sh62", "sh63", "sh65", "sh66", "sh67", "sh69", "sh70", "sh71", 
"sh72", "sh73", "sh74", "sh75", "sh76", "sh78", "sh79", "sh8", 
"sh84", "sh86", "sh88", "sh89", "sh91", "sh93", "sh95", "sh96", 
"sh98", "sh99", "sn104", "sn112", "sn117", "sn120", "sn123", 
"sn131", "sn141", "sn144", "sn145", "sn15", "sn151", "sn158", 
"sn159", "sn16", "sn162", "sn164", "sn165", "sn167", "sn18", 
"sn25", "sn27", "sn28", "sn30", "sn35", "sn40", "sn44", "sn45", 
"sn5", "sn56", "sn69", "sn7", "sn72", "sn74", "sn79", "sn83", 
"sn87", "sn89", "sn9", "sn91", "sn92", "sn93", "sn99", "st1", 
"st100", "st101", "st103", "st105", "st107", "st108", "st109", 
"st11", "st110", "st111", "st112", "st113", "st114", "st115", 
"st116", "st118", "st119", "st120", "st121", "st125", "st126", 
"st127", "st13", "st130", "st134", "st135", "st137", "st139", 
"st140", "st141", "st143", "st144", "st145", "st146", "st147", 
"st148", "st150", "st151", "st153", "st158", "st159", "st160", 
"st162", "st166", "st21", "st24", "st25", "st26", "st27", "st28", 
"st30", "st33", "st34", "st4", "st43", "st47", "st49", "st50", 
"st55", "st56", "st57", "st59", "st60", "st61", "st64", "st69", 
"st70", "st72", "st75", "st77", "st79", "st84", "st87", "st88", 
"st91", "st92", "st97", "st98", "st99", "th100", "th103", "th104", 
"th105", "th112", "th118", "th121", "th123", "th15", "th16", 
"th18", "th19", "th20", "th22", "th26", "th27", "th31", "th32", 
"th36", "th39", "th4", "th40", "th44", "th45", "th49", "th50", 
"th51", "th52", "th55", "th56", "th58", "th59", "th60", "th61", 
"th63", "th64", "th73", "th76", "th8", "th81", "th83", "th85", 
"th91", "th94", "th95", "th96", "th98", "th99"), class = "factor"), 
    year = 2005:2011, species = structure(c(8L, 8L, 8L, 8L, 8L, 
    8L, 8L), .Label = c("common linnet", "common whitethroat", 
    "corn bunting", "eurasian skylark", "northern lapwing", "red-backed shrike", 
    "tree sparrow", "western yellow wagtail", "whinchat", "woodlark", 
    "yellowhammer"), class = "factor"), abundance = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_), county = structure(c(48L, 48L, 48L, 48L, 48L, 
    48L, 48L), .Label = c("Aichach-Friedberg, Landkreis", "Alb-Donau-Kreis", 
    "Altmarkkreis Salzwedel", "Alzey-Worms, Landkreis", "Amberg-Sulzbach, Landkreis", 
    "Ammerland, Landkreis", "Anhalt-Bitterfeld, Landkreis", "Ansbach, Landkreis", 
    "Aschaffenburg, Landkreis", "Augsburg, Landkreis", "Aurich, Landkreis", 
    "Böblingen, Landkreis", "Börde, Landkreis", "Bad Dürkheim, Landkreis", 
    "Bad Kissingen, Landkreis", "Bad Kreuznach, Landkreis", "Bad Tölz-Wolfratshausen, Landkreis", 
    "Barnim, Landkreis", "Bautzen, Landkreis", "Bayreuth", "Bayreuth, Landkreis", 
    "Bergstraße, Landkreis", "Berlin", "Biberach, Landkreis", 
    "Bodenseekreis", "Borken, Kreis", "Brandenburg an der Havel, Kreisfreie Stadt", 
    "Breisgau-Hochschwarzwald, Landkreis", "Bremen, Kreisfreie Stadt", 
    "Bremerhaven, Kreisfreie Stadt", "Burgenlandkreis", "Calw, Landkreis", 
    "Celle, Landkreis", "Cham, Landkreis", "Chemnitz, Stadt", 
    "Cloppenburg, Landkreis", "Coburg, Landkreis", "Cottbus, Kreisfreie Stadt", 
    "Cuxhaven, Landkreis", "Dachau, Landkreis", "Dahme-Spreewald, Landkreis", 
    "Darmstadt-Dieburg, Landkreis", "Deggendorf, Landkreis", 
    "Dessau-Roßlau, Kreisfreie Stadt", "Diepholz, Landkreis", 
    "Dillingen a.d.Donau, Landkreis", "Dingolfing-Landau, Landkreis", 
    "Dithmarschen, Landkreis", "Donau-Ries, Landkreis", "Donnersbergkreis", 
    "Dresden, Stadt", "Ebersberg, Landkreis", "Eichsfeld, Kreis", 
    "Eichstätt, Landkreis", "Eisenach, krsfr. Stadt", "Elbe-Elster, Landkreis", 
    "Emmendingen, Landkreis", "Emsland, Landkreis", "Ennepe-Ruhr-Kreis", 
    "Enzkreis", "Erding, Landkreis", "Erfurt, krsfr. Stadt", 
    "Erlangen-Höchstadt, Landkreis", "Erzgebirgskreis", "Esslingen, Landkreis", 
    "Fürstenfeldbruck, Landkreis", "Fürth, Landkreis", "Forchheim, Landkreis", 
    "Frankfurt (Oder), Kreisfreie Stadt", "Frankfurt am Main, Kreisfreie Stadt", 
    "Freising, Landkreis", "Freudenstadt, Landkreis", "Freyung-Grafenau, Landkreis", 
    "Friesland, Landkreis", "Fulda, Landkreis", "Göppingen, Landkreis", 
    "Görlitz, Landkreis", "Göttingen, Landkreis", "Garmisch-Partenkirchen, Landkreis", 
    "Gießen, Landkreis", "Gifhorn, Landkreis", "Goslar, Landkreis", 
    "Gotha, Kreis", "Grafschaft Bentheim, Landkreis", "Greiz, Kreis", 
    "Höxter, Kreis", "Haßberge, Landkreis", "Halle (Saale), Kreisfreie Stadt", 
    "Hamburg", "Hameln-Pyrmont, Landkreis", "Hamm, Kreisfreie Stadt", 
    "Harburg, Landkreis", "Harz, Landkreis", "Havelland, Landkreis", 
    "Heidekreis, Landkreis", "Heidelberg, Kreisfreie Stadt", 
    "Heidenheim, Landkreis", "Heilbronn, Landkreis", "Heinsberg, Kreis", 
    "Hersfeld-Rotenburg, Landkreis", "Herzogtum Lauenburg, Landkreis", 
    "Hildburghausen, Kreis", "Hildesheim, Landkreis", "Hochsauerlandkreis", 
    "Hochtaunuskreis", "Hof, Landkreis", "Holzminden, Landkreis", 
    "Ilm-Kreis", "Ingolstadt", "Jerichower Land, Landkreis", 
    "Köln, Kreisfreie Stadt", "Kaiserslautern, Landkreis", "Karlsruhe, Kreisfreie Stadt", 
    "Karlsruhe, Landkreis", "Kassel, Landkreis", "Kelheim, Landkreis", 
    "Kitzingen, Landkreis", "Kleve, Kreis", "Konstanz, Landkreis", 
    "Kronach, Landkreis", "Kulmbach, Landkreis", "Kusel, Landkreis", 
    "Kyffhäuserkreis", "Lörrach, Landkreis", "Lüchow-Dannenberg, Landkreis", 
    "Lüneburg, Landkreis", "Lahn-Dill-Kreis", "Landkreis Ludwigslust-Parchim", 
    "Landkreis Mecklenburgische Seenplatte", "Landkreis Nordwestmecklenburg", 
    "Landkreis Rostock", "Landkreis Vorpommern-Greifswald", "Landkreis Vorpommern-Rügen", 
    "Landsberg am Lech, Landkreis", "Landshut, Landkreis", "Leer, Landkreis", 
    "Leipzig, Landkreis", "Lichtenfels, Landkreis", "Limburg-Weilburg, Landkreis", 
    "Lippe, Kreis", "Ludwigsburg, Landkreis", "Märkisch-Oderland, Landkreis", 
    "Märkischer Kreis", "Mühldorf a.Inn, Landkreis", "München, Landeshauptstadt", 
    "München, Landkreis", "Main-Spessart, Landkreis", "Main-Tauber-Kreis", 
    "Main-Taunus-Kreis", "Mainz-Bingen, Landkreis", "Mannheim, Universitätsstadt, Kreisfreie Stadt", 
    "Mansfeld-Südharz, Landkreis", "Marburg-Biedenkopf, Landkreis", 
    "Mayen-Koblenz, Landkreis", "Meißen, Landkreis", "Mettmann, Kreis", 
    "Minden-Lübbecke, Kreis", "Mittelsachsen, Landkreis", "Nürnberg", 
    "Nürnberger Land, Landkreis", "Neckar-Odenwald-Kreis", "Neu-Ulm, Landkreis", 
    "Neuburg-Schrobenhausen, Landkreis", "Neumarkt i.d.OPf., Landkreis", 
    "Neustadt a.d.Aisch-Bad Windsheim, Landkreis", "Neustadt a.d.Waldnaab, Landkreis", 
    "Neustadt an der Weinstraße, Kreisfreie Stadt", "Neuwied, Landkreis", 
    "Nienburg (Weser), Landkreis", "Nordfriesland, Landkreis", 
    "Nordhausen, Kreis", "Nordsachsen, Landkreis", "Northeim, Landkreis", 
    "Oberallgäu, Landkreis", "Oberhavel, Landkreis", "Oberspreewald-Lausitz, Landkreis", 
    "Odenwaldkreis", "Oder-Spree, Landkreis", "Offenbach, Landkreis", 
    "Oldenburg, Landkreis", "Olpe, Kreis", "Ortenaukreis", "Osnabrück, Landkreis", 
    "Ostalbkreis", "Ostallgäu, Landkreis", "Osterholz, Landkreis", 
    "Osterode am Harz, Landkreis", "Ostholstein, Landkreis", 
    "Ostprignitz-Ruppin, Landkreis", "Passau, Landkreis", "Peine, Landkreis", 
    "Pfaffenhofen a.d.Ilm, Landkreis", "Pinneberg, Landkreis", 
    "Plön, Landkreis", "Potsdam-Mittelmark, Landkreis", "Potsdam, Kreisfreie Stadt", 
    "Prignitz, Landkreis", "Rastatt, Landkreis", "Ravensburg, Landkreis", 
    "Regen, Landkreis", "Regensburg, Landkreis", "Region Hannover, Landkreis", 
    "Rems-Murr-Kreis", "Rendsburg-Eckernförde, Landkreis", "Reutlingen, Landkreis", 
    "Rhön-Grabfeld, Landkreis", "Rhein-Kreis Neuss", "Rhein-Neckar-Kreis", 
    "Rhein-Sieg-Kreis", "Rheingau-Taunus-Kreis", "Rheinisch-Bergischer Kreis", 
    "Rosenheim, Landkreis", "Rotenburg (Wümme), Landkreis", 
    "Roth, Landkreis", "Rottweil, Landkreis", "Sächsische Schweiz-Osterzgebirge, Landkreis", 
    "Sömmerda, Kreis", "Südliche Weinstraße, Landkreis", "Südwestpfalz, Landkreis", 
    "Saale-Holzland-Kreis", "Saale-Orla-Kreis", "Saalekreis", 
    "Saalfeld-Rudolstadt, Kreis", "Salzlandkreis", "Schaumburg, Landkreis", 
    "Schleswig-Flensburg, Landkreis", "Schmalkalden-Meiningen, Kreis", 
    "Schwabach", "Schwandorf, Landkreis", "Schweinfurt", "Schweinfurt, Landkreis", 
    "Segeberg, Landkreis", "Siegen-Wittgenstein, Kreis", "Sigmaringen, Landkreis", 
    "Soest, Kreis", "Sonneberg, Kreis", "Spree-Neiße, Landkreis", 
    "Stade, Landkreis", "Starnberg, Landkreis", "Steinburg, Landkreis", 
    "Steinfurt, Kreis", "Stendal, Landkreis", "Stormarn, Landkreis", 
    "Straubing-Bogen, Landkreis", "Stuttgart, Landeshauptstadt, Kreisfreie Stadt", 
    "Tübingen, Landkreis", "Teltow-Fläming, Landkreis", "Traunstein, Landkreis", 
    "Uckermark, Landkreis", "Uelzen, Landkreis", "Unstrut-Hainich-Kreis", 
    "Unterallgäu, Landkreis", "Vechta, Landkreis", "Verden, Landkreis", 
    "Viersen, Kreis", "Vogelsbergkreis", "Würzburg, Landkreis", 
    "Waldeck-Frankenberg, Landkreis", "Wartburgkreis", "Weißenburg-Gunzenhausen, Landkreis", 
    "Weilheim-Schongau, Landkreis", "Weimar, krsfr. Stadt", "Weimarer Land, Kreis", 
    "Werra-Meißner-Kreis", "Wesel, Kreis", "Wesermarsch, Landkreis", 
    "Westerwaldkreis", "Wetteraukreis", "Wiesbaden, Landeshauptstadt, Kreisfreie Stadt", 
    "Wittenberg, Landkreis", "Wittmund, Landkreis", "Wolfenbüttel, Landkreis", 
    "Wuppertal, Kreisfreie Stadt", "Zollernalbkreis", "Zwickau, Landkreis"
    ), class = "factor"), slope_abundance_plot = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), p_slope_abundance_plot = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("site", 
"year", "species", "abundance", "county", "slope_abundance_plot", 
"p_slope_abundance_plot"), row.names = c(61L, 75L, 76L, 91L, 
92L, 93L, 134L), class = "data.frame")
  • How would you translate "alle Fälle NA" in english? "NA in every cases" ? – Pop Oct 17 '14 at 10:38
  • my apologies, yes it means 0 non-na cases in english. – thecrashlandingdodo Oct 17 '14 at 10:42
  • From the error message it seems like there is a species/site combination where all abundance values are `NA`. – Roland Oct 17 '14 at 10:42
  • 1
    @Roland, yes, I read in the other questions that this could cause an issue, however, if I run complete.cases on the abundance before and delete all NA's I still get this message. – thecrashlandingdodo Oct 17 '14 at 10:45
  • You need to provide a [minimal reproducible example](http://stackoverflow.com/a/5963610/1412059). – Roland Oct 17 '14 at 10:51
  • @Roland I provided some example data an changed some values, but it will still reproduce the problem. – thecrashlandingdodo Oct 17 '14 at 11:15
  • Please read the linked FAQ. This is not how you provide reproducible data. – Roland Oct 17 '14 at 11:26
  • Anyway, have you looked at the values of `i` and `k` after the error occured in order to identify the subset that exhibits the problem? – Roland Oct 17 '14 at 11:30
  • I'm sorry, not very good at r but thought that the provided example counts as FAQ. "worst case scenario". The Error occurs at i 1L and k 2L – thecrashlandingdodo Oct 17 '14 at 11:34
  • Well, then add the output of `dput(alldata_lm[alldata_lm$species == species[1] & alldata_lm$site == sites[2],])` to your question and shorten your code to the one line that does the `lm` fit. – Roland Oct 17 '14 at 11:39
  • @Roland thank you, I have added the output of the code line you provided. It seems quite a lot although I only applied it onthe subset I created for this question... – thecrashlandingdodo Oct 17 '14 at 12:05

1 Answers1

2

If I print your subset, I see this:

    site year                species abundance                  county slope_abundance_plot p_slope_abundance_plot
61  sh47 2005 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
75  sh47 2006 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
76  sh47 2007 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
91  sh47 2008 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
92  sh47 2009 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
93  sh47 2010 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA
134 sh47 2011 western yellow wagtail        NA Dithmarschen, Landkreis                   NA                     NA

As you see, all abundance values are NA, which is what the error message was telling you. You should use tryCatch to handle these subsets.

(Btw, the dput output is so large because it includes all factor levels.)

Roland
  • 127,288
  • 10
  • 191
  • 288
  • thanks for the info on the output. yes, I was expecting this for some species, but even if I apply complete.cases on abundance before running the loop I will still receive the same error. – thecrashlandingdodo Oct 17 '14 at 12:21
  • Use `alldata_lm <- na.omit(alldata_lm)` before assigning `slope_abundance_plot`. – Roland Oct 17 '14 at 12:24
  • I tried, it goes on step further to i 1L and k 3L but returns the same error unfortunately. – thecrashlandingdodo Oct 17 '14 at 12:38
  • Do some basic debugging! *Look* at the subset that causes the error. You have the data and can run your code. I can't. – Roland Oct 17 '14 at 13:03
  • I already showed you in a comment: `alldata_lm[alldata_lm$species == species[i] & alldata_lm$site == sites[k],]` – Roland Oct 17 '14 at 13:23