I have used melt
to combine all my 32 columns into a single column, their values into a single column, and the independent variable into a single column.
I then wanted to use lapply
to generate a lm
matching the rows ofYears Species Farmland
There were two ways of me wanting to do this;
1. To take the lm
of one variable name i.e. Starling values across all years (1994:2013)
2. To take the lm
of all variables names I.e. Starling, Skylark, Lapwing .... Farmland values together across each year.
An example of my data:
structure(list(Years = c(1994L, 1994L, 1995L, 1996L, 1997L, 1998L
), Species = structure(1:6, .Label = c("Starling", "Skylark",
"YellowWagtail", "Kestrel", "Yellowhammer", "Greenfinch"), class = "factor"),
Farmland = c(13260L, 13520L, 8129L, 15575L, 18686L, 18541L
)), row.names = c(1L, 20L, 40L, 60L, 80L, 100L), class = "data.frame")
A further example:
'data.frame': 570 obs. of 3 variables:
$ Years : int 1994 1995 1996 1997 1998 1999 2000 2002 2003 2004 ...
$ Species : Factor w/ 30 levels "Starling","Skylark",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Farmland: int 13260 15551 16335 18997 18571 18376 15770 16054 15101 16276 ...
Code for lm
for Q.1:
df_try <- lapply(1:n, function(x) lm(Farmland ~ Years + Species, work_practice))
The output:
Call:
lm(formula = Farmland ~ Years + Species, data = work_practice)
Coefficients:
(Intercept) Years SpeciesSkylark
-708278.6 363.0 578.8
SpeciesYellowWagtail SpeciesKestrel SpeciesYellowhammer
-9329.8 -744.4 -238.7
SpeciesGreenfinch SpeciesSwallow SpeciesHousemartin
246.3 506.6 -3928.5
SpeciesLinnet SpeciesGreyPartridge SpeciesTurtleDove
-680.2 -5825.1 -5417.4
SpeciesCornbunting SpeciesBullfinch SpeciesSongthrush
-12187.9 -5688.7 -279.1
SpeciesBlackbird SpeciesDunnock SpeciesWhitethroat
490.2 299.0 231.6
SpeciesRook SpeciesReedBunting SpeciesStockdove
-653.9 -6864.5 -1788.0
SpeciesGoldfinch SpeciesJackdaw SpeciesWren
156.6 -637.3 553.1
SpeciesRobin SpeciesBluetit SpeciesGreatTit
328.7 460.3 384.3
SpeciesLongtailedTit SpeciesChaffinch SpeciesBuzzard
-1359.8 499.7 -6888.2
SpeciesSparrowhawk
-4458.5
The problem with this; Starling is missing (The first variable name), and Years is not necessary for the result (How can this be removed) this is iterated on call 19 times which I assume is because of the dataframe. Is there a way to call this only once?
I have tried doing this when the variable (Species) was in columns but the output only calls for one variable 19 times...