Hello I am writing because I am trying to place a group_by and summarise function through a loop tied to variables in a second dataset. I tried to do this through both a for loop and an apply loop.
I have one dataset that is a list of Species and attributes. d1 looks like
Species Height
Cenjac 67
Cirarv 24
d2 is patch data that I normally summarise which has the presence absence of the species in each patch, the nearest patch (Target), and the size of the patch.
Patch Target Size Cenjac Cirarv
a c 250 0 1
b a 18 1 0
c a 20 1 0
My normal method of summarising is manually through group_by and summarise to create a new variable which is the Height from d1 the Size and presence/absence from d2. I need to write the Height in each time. (Note:This is not my real equation)
DfullCJ<- group_by(d2, Patch, Target) %>% summarise(Cenjacmax=(67*Size*Cenjac))
I would then need to re-write the code each time for each species
DfullCA<- group_by(d2, Patch, Target) %>% summarise(Cirarvmax=(24*Size*Cirarv))
Ideally, I would be able to automate this process through either a for loop or apply. Is there no way to set the Species name as a variable and then pull from d1 both the Height and the corresponding Species name (which is also the name of presence absence column in d2) to plug into the group_by summarise function. Or or run the function through a loop with d1 as a list.
Thanks to any one who can help me.
DfullCJ<- group_by(d2, Patch, Target) %>% summarise(Cenjacmax=(67*Size*Cenjac), Cirarvmax=(24*Size*Cirarv))You can summarise multiple in one command, would this help you out? Note that you would need to have merged your dataframes into one. - Bartsummarise. Did you just needmutate? - Parfait