I'm struggling a bit with conditional subsetting of information to average the subset
I have 2 datasets:
type<-c("flesh","wholefish","wholefish","wholefishdelip")
group<-c("two","four",'five','five')
N<-c(10.2,11.1,10.7,11.3)
prey <- cbind(type,group,N)
sample<-c('plasma','wholeblood','redbloodcell')
group1<-c('four','four','two')
group2<-c('','five','four')
group3<-c('','','five')
avgN<-c("","","")
penguin<-cbind(sample,group1,group2,group3,avgN)
I want to output to look like this
sample | group1 | group2 | group3 | avgNwf
plasma | four | | | 11.1 #made up by (11.1/1)
wholeblood | four | five | | 10.9 #(11.1+10.7)/2
redbloodcell | two | four | five | 10.9 #(11.1+10.7)/2
I want to calculate a value for penguin$avgN according to conditions per row. I want to calculate the average prey$N if prey$Type == "wholefish" & prey$group matches penguin$group1, penguin$group2 and penguin$group3. Not all penguin groups have entries so I was running into a problem with excel where I couldn't make it ignore the #N/A. (And excel doesn't have a function for conditional standard deviations)
IE for the first row in the penguin dataframe, I want to average N (of the prey df) for all wholefish in groups four and five. I have tried the following with fewer conditions just to see if I am on track but to no avail:
avgN <-mean(ifelse(prey$group==penguin$group1,prey$N, "nope"))
avgN <-mean(prey$N[prey$group==penguin$group1,])
The following is not what I want to achieve:
avgN = summaryBy(N ~group+type, data=prey, FUN=c(mean, sd), na.rm=T)
as it brings back a summary version of information instead of an individual result for each entry with its own conditions.
avgN <-mean(prey$N)
as it lacks the conditions for each individual sample.
In excel I would use cell references to work with conditions unique to a row.