Error using aggregate to find length with missing values - r

I am trying to use the aggregate function in R to summarise a data using the length function. My data has some NA's and I have tried using 'na.rm = T' or 'na.omit' however none sees to work. I keep getting this error
'Error in FUN(X[[i]], ...) :
2 arguments passed to 'length' which requires 1'
data10 <- structure(list(Group = c(1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 2, 2,
1, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2,
1), SUBJECT = c(1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 10,
11, 12, 14, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23,
23, 24, 25), test = c(1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2,
1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 1
), trial = c(1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7,
1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3, 5, 7, 1, 3), Condition = c(1,
2, 3, 1, 3, 1, 2, 3, 2, 3, 1, 2, 1, 2, 3, 1, 3, 1, 2, 3, 2, 3,
1, 2, 1, 2, 3, 1, 3, 1, 2, 3, 2, 3), Sac2 = c(1, 1, 1, NA, 2,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1,
1, 1, 1, 1, 2, 2, 1, 1), Sac = c(1, 1, 1, NA, 3, 1, 1, 1, 1, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 1, 1, 1, 7, 1, 1, 1, 1, 1, 1, 3,
3, 1, 1), Saccade...8 = c(1, 1, 1, NA, 2, 1, 1, 1, 1, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1,
1), T_APPEAR = c(9.236, 17.85, 28.942, 63.724, 9.463, 22.963,
52.068, 57.021, 15.344, 19.783, 37.825, 46.17, 4.339, 21.241,
29.179, 31.823, 12.164, 22.84, 23.954, 73.663, 27.269, 22.131,
30.361, 62.674, 6.928, 16.413, 47.555, 48.893, 7.291, 15.796,
31.788, 54.946, 10.117, 28.83)), row.names = c(NA, -34L), class = c("tbl_df",
"tbl", "data.frame"))
data14 = aggregate(data10,
by = list(data10$SUBJECT,data10$Condition, data10$Group, data10$test),
FUN = length(), na.rm=TRUE)

Related

Output list of variables with significant p-values from several regressions in R

I have a dataframe that looks like the following.
consistent admire trust judge
3 3 2 4
5 1 3 6
2 4 5 1
I can run the regressions I need simultaneously using the following code. In the actual dataset, there are many more than 3 variables.
variables <- c("admire", "trust", "judge")
form <- paste("consistent ~ ",variables,"")
model <- form %>%
set_names(variables) %>%
map(~lm(as.formula(.x), data = df))
map(model, summary)
This yields the output for the 3 following regressions.
summary(lm(consistent ~ admire, df))
summary(lm(consistent ~ trust, df))
summary(lm(consistent ~ judge, df))
I would like a list of the variables with significant p-values at p < 0.05. For example, if "admire" was significant and "judge" was significant, the output I am looking for would be something like:
admire, judge
Is there a way to do this that allows me to also run several regressions simultaneously? This question offers a similar answer, but I don't know how to apply it when I have several regressions.
Data:
structure(list(consistent = c(1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1,
1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1,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 5, 5, 1, 2, 2, 5,
6, 5, 1, 1, 1, 4, 1, 5, 4, 3, 6, 1, 4, 1, 3, 4, 6, 1, 2, 4, 3,
3, 4, 7, 1, 3, 1, 2, 2, 3, 2, 3, 5, 3, 4, 2, 6, 3, 1, 1, 1, 1,
4, 2, 2, 4, 4, 5, 4, 2, 1, 6, 7, 5, 2, 2, 4, 5, 6, 1, 5, 2, 4,
5, 5, 2, 2, 3, 4, 5, 2, 2, 4, 1, 3, 4, 4, 4, 2, 3, 1, 4, 4, 3,
2, 3, 1, 4, 2, 4, 4, 1, 5, 4, 4, 4, 4, 6, 1, 3, 5, 7, 2, 6, 1,
5, 7, 5, 4, 2, 3, 6, 3, 1, 1, 2, 2, 5, 5, 2, 5, 4, 4, 5, 4, 4,
3, 7, 4, 4, 4, 2, 5, 3, 6, 5, 4, 4, 4, 6, 4, 5, 5, 1, 5, 2, 6,
4, 4, 1, 1, 4, 6, 1, 7, 1, 5, 2, 5, 4, 2, 3, 2, 6, 3, 2, 2, 1,
1, 5, 4, 1, 1, 4, 1, 5, 1, 4, 3, 2, 3, 4, 1, 6, 1, 2, 1, 3, 5,
5, 2, 1, 3, 4, 2, 4, 5, 4, 6, 3, 4, 6, 7, 6, 2, 4, 6, 2, 4, 5,
1, 4, 1, 3, 2, 4, 1, 6, 4, 3, 1, 3, 4, 5, 1, 6, 1, 5, 1, 3, 3,
1, 3, 4, 2, 4, 1, 1, 2, 2, 2, 3, 1, 6, 5, 4, 1, 7, 5, 6, 5, 2,
3, 5, 4, 3, 4, 5, 7, 1, 5, 2, 5, 1, 3, 4, 3, 5, 1, 4, 2, 3, 4,
1, 7, 5, 5, 2, 1, 2, 5, 6, 5, 5, 3, 1, 3, 1, 4, 1, 5, 2, 3, 5,
6, 4, 4, 3, 2, 4, 1, 3, 4, 3, 4, 4, 1, 5)), row.names = c(NA,
-450L), class = c("tbl_df", "tbl", "data.frame"))
To fit many many simple linear regression models, I recommend Fast pairwise simple linear regression between variables in a data frame. Hmm... looks like I need to collect those functions in an R package...
## suppose your data frame is `df`
## response variable (LHS) in column 1
## independent variable (RHS) in other columns
out <- general_paired_simpleLM(df[1], df[-1])
# LHS RHS alpha beta beta.se beta.tv beta.pv
#1 consistent admire -0.1458455 0.18754326 0.008324192 22.529906 1.040756e-75
#2 consistent trust -0.2211250 0.19565589 0.007721387 25.339475 1.531499e-88
#3 consistent judge 0.3484851 0.04824981 0.014182420 3.402086 7.287372e-04
# sig R2 F.fv F.pv
#1 0.3430602 0.53118295 507.59665 1.040756e-75
#2 0.3212008 0.58902439 642.08902 1.531499e-88
#3 0.4946862 0.02518459 11.57419 7.287372e-04
To get what you want:
with(out, RHS[beta.pv < 0.05])
#[1] "admire" "trust" "judge"

Bootstrapping multiple regression error: number of items to replace is not a multiple of replacement length

I want to bootstrap my dataset for multiple regression. Unfortunately I get this error message:
"number of items to replace is not a multiple of replacement length"
I suspect that the factors in my regression formula may be problematic.
What could I do to solve my problem?
My code is as following (I read Andy FieldĀ“s Discovering Statistics using R):
BootReg <- function(data, indices, formula) {
d <- data[indices,]
fit <- lm(formula, data=d)
return(coef(fit))
}
bootResults <-boot(statistic = BootReg, formula = TICS_Skala1 ~HSPhoch + HSPhoch*extra.c
+ psy + sex + age.c, data = mod.reg.data, R = 2000)
psy (psychiatric disease), sex and HSPhoch (high sensory-processing sensitivity) are factors. TICS_Skala1, extra.c, age.c are continuos variables.
my sample data:
> dput(head(mod.reg.data, 20))
structure(list(neo_01 = c(3, 4, 3, 0, 4, 4, 3, 2, 3, 1, 4, 2,
3, 3, 1, 2, 3, 4, 0, 2), neo_03 = c(1, 1, 1, 3, 1, 2, 0, 0, 0,
0, 0, 0, 1, 3, 1, 1, 1, 1, 3, 1), neo_04 = c(2, 4, 3, 0, 4, 3,
4, 3, 2, 3, 3, 3, 3, 4, 2, 4, 3, 4, 3, 3), neo_08 = c(3, 0, 1,
2, 3, 3, 4, 3, 2, 1, 2, 4, 0, 3, 1, 1, 3, 1, 3, 1), neo_12 = c(3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 3, 4, 3, 1, 4, 1, 1, 3, 4, 2, 3, 2, 5, 3), sex =
structure(c(2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L), .Label = c("m", "w", "d"), class = "factor"), Bildung =
structure(c(6L,
5L, 5L, 6L, 6L, 6L, 5L, 6L, 5L, 6L, 6L, 4L, 6L, 5L, 5L, 6L, 6L,
5L, 5L, 6L), .Label = c("kein", "Haupt", "mittlereR", "Fachabi",
"Abi", "Studium"), class = "factor"), job = structure(c(6L, 2L,
2L, 2L, 2L, 6L, 2L, 6L, 5L, 2L, 2L, 1L, 6L, 2L, 2L, 2L, 6L, 2L,
2L, 6L), .Label = c("hausl", "Student", "Azubi", "Suchend", "Rente",
"berufstaetig"), class = "factor"), age = c(23, 24, 21, 70, 25,
29, 22, 25, 57, 24, 25, 30, 31, 20, 28, 27, 26, 21, 24, 53),
VPN = 1:20, consent = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("ja",
"nein"), class = "factor"), psy = c(0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0), HSPS = c(86, 75,
69, 102, 85, 82, 97, 59, 100, 68, 80, 106, 68, 73, 105, 79,
91, 63, 119, 59), neuro = c(16, 3, 4, 10, 10, 11, 12, 5,
5, 5, 5, 16, 5, 18, 4, 3, 8, 5, 19, 7), extra = c(15, 17,
19, 7, 19, 17, 18, 17, 16, 10, 17, 14, 15, 19, 11, 13, 16,
18, 9, 13), TICS_Skala1 = c(23, 1, 22, 11, 14, 16, 22, 25,
2, 11, 1, 29, 9, 20, 10, 19, 16, 9, 18, 16), TICS_Skala2 = c(14,
12, 11, 9, 11, 10, 4, 10, 5, 8, 5, 24, 13, 5, 6, 6, 14, 2,
1, 13), TICS_Skala3 = c(21, 6, 10, 5, 12, 14, 11, 20, 3,
11, 4, 27, 20, 13, 7, 13, 20, 11, 11, 18), TICS_Skala4 = c(13,
14, 13, 2, 16, 23, 10, 9, 3, 13, 15, 18, 14, 11, 13, 10,
7, 9, 17, 6), TICS_Skala5 = c(12, 2, 6, 5, 3, 5, 8, 3, 4,
6, 0, 18, 3, 7, 1, 6, 6, 1, 13, 3), TICS_Skala6 = c(10, 2,
3, 4, 4, 6, 3, 0, 0, 5, 2, 15, 10, 5, 2, 1, 5, 2, 8, 3),
TICS_Skala7 = c(15, 5, 9, 13, 4, 8, 4, 9, 1, 6, 2, 11, 2,
12, 3, 2, 1, 3, 2, 7), TICS_Skala8 = c(8, 10, 3, 0, 11, 7,
2, 1, 2, 2, 7, 20, 7, 2, 2, 2, 1, 1, 2, 3), TICS_Skala9 = c(12,
3, 4, 8, 8, 6, 9, 5, 2, 6, 5, 11, 3, 11, 1, 5, 9, 3, 7, 5
), TICS_Skala10 = c(32, 5, 18, 16, 19, 18, 21, 16, 5, 17,
7, 39, 12, 24, 3, 15, 20, 6, 25, 14), neuro.c = c(6.08921933085502,
-6.91078066914498, -5.91078066914498, 0.089219330855018,
0.089219330855018, 1.08921933085502, 2.08921933085502,
-4.91078066914498,
-4.91078066914498, -4.91078066914498, -4.91078066914498,
6.08921933085502, -4.91078066914498, 8.08921933085502,
-5.91078066914498,
-6.91078066914498, -1.91078066914498, -4.91078066914498,
9.08921933085502, -2.91078066914498), extra.c = c(5.21003717472119,
7.21003717472119, 9.21003717472119, -2.78996282527881,
9.21003717472119,
7.21003717472119, 8.21003717472119, 7.21003717472119,
6.21003717472119,
0.21003717472119, 7.21003717472119, 4.21003717472119,
5.21003717472119,
9.21003717472119, 1.21003717472119, 3.21003717472119,
6.21003717472119,
8.21003717472119, -0.78996282527881, 3.21003717472119), age.c =
c(-15.4460966542751,
-14.4460966542751, -17.4460966542751, 31.5539033457249,
-13.4460966542751,
-9.4460966542751, -16.4460966542751, -13.4460966542751,
18.5539033457249,
-14.4460966542751, -13.4460966542751, -8.4460966542751,
-7.4460966542751,
-18.4460966542751, -10.4460966542751, -11.4460966542751,
-12.4460966542751, -17.4460966542751, -14.4460966542751,
14.5539033457249), HSP.c = c(-1.92936802973978, -12.9293680297398,
-18.9293680297398, 14.0706319702602, -2.92936802973978,
-5.92936802973978,
9.07063197026022, -28.9293680297398, 12.0706319702602,
-19.9293680297398,
-7.92936802973978, 18.0706319702602, -19.9293680297398,
-14.9293680297398,
17.0706319702602, -8.92936802973978, 3.07063197026022,
-24.9293680297398,
31.0706319702602, -28.9293680297398), HSPhoch = c(1, 0, 0,
1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0)), row.names =
c(NA, 20L), class = "data.frame")

converting NULL to numeric and taking the sum of lists

I have a BTO dataset, which I converted from long to wide format to prepare it for diversity measurements using the diversity function from the vegan package.
To achieve this I used this code:
diversity <- pivot_wider(bird_case, names_from = ENGLISH_NAME, values_from = HOW_MANY)
The results comes up with list elements as I converted the months into seasons with a previous code. I wish to take the sums of all the lists, so only a single sum remains in each cell. As for the NULL values I want these converted to 0.
I have tried to replace the NULL values to zero using this
diversity[diversity == "NULL"] <- 0
it won't work.
As for converting the list elements and taking the sum, I have tried aggregate to no avail.
Heres a reproducible code:
structure(list(year = c(2018, 2019, 2017, 2015, 2014, 2015, 2017,
2017, 2016, 2019, 2018, 2016, 2016, 2016, 2019, 2019, 2018, 2017,
2015, 2018, 2015, 2017, 2015, 2016, 2016, 2016, 2018, 2018, 2017,
2014, 2015, 2017, 2014, 2014, 2017, 2019, 2010, 2011, 2011, 2012,
2019, 2012, 2013, 2019, 2017, 2011, 2017, 2016, 2016, 2010),
Season = c("Winter", "Winter", "Summer", "Winter", "Winter",
"Autumn", "Autumn", "Winter", "Spring", "Autumn", "Spring",
"Winter", "Summer", "Autumn", "Summer", "Spring", "Summer",
"Spring", "Spring", "Autumn", "Summer", "Summer", "Autumn",
"Summer", "Autumn", "Winter", "Spring", "Winter", "Winter",
"Summer", "Winter", "Autumn", "Autumn", "Winter", "Spring",
"Winter", "Summer", "Spring", "Summer", "Autumn", "Winter",
"Winter", "Winter", "Spring", "Summer", "Winter", "Autumn",
"Winter", "Spring", "Winter"), POSTCODE = c("NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA",
"NR29 5QA", "NR29 5QA", "NR29 5QA", "NR29 5QA", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS",
"NR15 1TS", "NR15 1TS", "NR15 1TS", "NR15 1TS", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL", "PE32 1TL",
"PE32 1TL", "PE32 1TL", "PE32 1TL"), LOC_ID = c("LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC568364", "LOC568364", "LOC568364", "LOC568364", "LOC568364",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128", "LOC1163128",
"LOC569508", "LOC569508", "LOC569508", "LOC569508", "LOC569508",
"LOC569508", "LOC569508", "LOC569508", "LOC569508", "LOC569508",
"LOC569508", "LOC569508", "LOC569508", "LOC569508"), Wren = list(
c(1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1), c(1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1), 1, c(1,
1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1, 1,
1, 3, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1,
1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 2, 1, 1), c(1,
1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 2, 1), c(2, 1, 2, 3,
1, 1, 1), c(1, 1), c(1, 1, 1), c(1, 1, 1, 1, 1), c(1,
1), c(1, 1), c(1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1),
NULL, 1, c(2, 1, 1, 2, 1), NULL, NULL, c(1, 1, 1), NULL,
1, NULL, NULL, c(1, 1), c(1, 1, 1, 1, 1, 1), c(1, 1,
1, 1), c(1, 1), NULL, NULL, c(1, 1), 1, c(1, 1, 1), NULL,
c(1, 1, 1, 1)), Dunnock = list(c(2, 2, 1, 2, 1, 1, 1,
2, 2, 2), c(2, 1, 2, 2, 2, 2, 1), c(1, 1, 2, 1, 3, 1, 2),
c(1, 2, 2, 2, 2, 2, 2, 1, 1), 2, c(1, 1, 1, 2, 1, 1,
2), c(1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1), c(2, 2, 2, 2,
2, 1, 2, 2, 1, 2, 2), c(2, 5, 2, 1, 3, 2, 2, 3, 2, 3,
1), c(2, 1, 1, 2, 2), c(3, 2, 3, 2, 2, 3, 3, 2, 2), c(1,
1, 1, 1, 1, 1), c(1, 2, 1, 2, 2, 2, 2), c(1, 1, 2, 1,
2, 1, 2, 2, 1, 2), c(3, 4, 2, 5, 3, 5, 4, 2), c(2, 2,
2, 2, 1, 2, 3, 2, 2), c(3, 3, 3, 3, 3, 2, 1, 2, 3, 1),
c(2, 3, 2, 2, 2, 2, 2, 2, 2, 5, 4, 2), c(2, 2, 2, 1,
2, 1, 2, 2, 2, 2), c(1, 2, 1, 1, 2, 2, 1), c(1, 1, 1,
2, 1, 1), c(3, 4, 6, 3, 3, 3), c(1, 1, 2, 1), c(2, 1),
c(2, 2, 1, 2, 1), c(2, 1, 2, 1, 2, 2), c(2, 2), c(1,
1, 1, 2), c(2, 3, 2, 2, 2, 3, 3, 2, 2, 2), 2, c(2, 2,
3, 2, 2), c(2, 1, 2, 2, 2, 2), 1, NULL, c(3, 2), c(1,
1), c(1, 2, 1, 1, 1, 1, 1, 2), c(2, 2, 1, 1, 1, 1), c(3,
3, 2, 1, 2, 2, 2, 1, 1, 1), c(2, 1, 1, 1, 1), c(1, 1,
1, 1, 1, 2, 2, 2), c(1, 1, 2, 1, 1, 2, 1, 1), c(1, 1,
2, 1, 1), c(3, 2, 1, 5, 2, 1, 2, 2, 2), c(3, 3, 1, 1,
1, 3, 2, 1), c(1, 1, 2, 1), c(1, 2, 2, 2, 1, 2, 1, 2,
2, 2, 1, 1), c(1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1),
c(1, 1, 2, 1, 4, 4, 1, 2, 2, 2), c(2, 1, 1, 4, 1, 1,
1, 2, 1, 1)), `Blue Tit` = list(c(1, 1, 2, 3), c(2, 2,
3, 2, 2), 4, c(4, 2, 3, 4), 2, c(2, 2), c(1, 2, 2), c(2,
2), c(2, 2, 1, 2, 2), NULL, c(2, 2, 2, 5, 2, 2, 2, 2, 2),
c(2, 1, 2, 2, 3, 2), 2, NULL, 7, c(2, 2, 2, 2, 2, 2,
2, 2, 2), NULL, c(1, 1, 2, 2, 2, 2, 2), c(4, 2, 4, 3,
7, 3, 2), 1, c(2, 2, 3), c(8, 10, 10, 12, 10, 8, 5, 12
), c(6, 4, 4, 6, 4), c(12, 6, 6, 6, 6), c(4, 4, 5, 5,
8), c(10, 6, 6, 4, 6, 6, 4), 4, c(10, 4, 4, 8, 6), c(4,
6, 4, 10, 6, 6, 8, 7, 6), c(12, 12, 6), c(12, 8, 12,
12, 12, 10, 10), c(10, 5, 10, 5, 10), c(12, 12, 6), c(6,
6), c(4, 2, 2, 2), c(2, 6), c(3, 2, 2, 1, 2, 1, 2), c(2,
2, 2, 1, 2, 1), c(2, 4, 1, 2, 1, 2, 2, 1, 2), c(4, 3,
1, 2, 2, 2, 2, 3, 5, 4), c(2, 4, 3, 3, 1, 2, 2), c(2,
4, 2, 2, 1, 2, 1, 1, 3), c(3, 3, 2, 2, 3, 2, 3, 2), c(1,
2, 1, 2, 2, 2, 2, 1, 2), c(5, 3, 9, 4, 4, 3, 9, 5), c(1,
2, 1, 2, 3, 2, 1, 2, 3, 3, 2), c(4, 3, 5, 2, 3, 4, 3,
3, 4, 5, 2), c(3, 3, 3, 3, 4, 2, 3, 4, 3, 5, 3), c(2,
2, 2, 1, 1, 2, 1, 2, 1, 2, 4), c(2, 2, 2, 3, 2, 2, 2,
1)), `Pied/White Wagtail` = list(c(1, 2, 2, 2, 1, 1,
1, 2, 2, 2), c(2, 1, 1, 1, 2, 1, 1, 2, 2), c(1, 1, 1, 1),
NULL, NULL, NULL, 2, c(2, 2, 2, 2), c(1, 1, 1, 1, 1),
c(2, 2, 2, 1), c(2, 2, 2, 2, 2, 2, 2, 2, 2), NULL, c(2,
2), NULL, c(2, 2, 1, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 1,
2, 2, 2), c(2, 2, 2, 2, 1, 2, 3), c(1, 2, 2, 2, 2, 2),
NULL, c(1, 1), 1, 1, NULL, NULL, 1, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, NULL, 1, c(1, 1, 1, 1, 1), c(2, 1, 2, 1), c(1, 1),
c(1, 1), 1, 1, 1, 1, c(1, 1), c(1, 1, 1, 1)), `Collared Dove` = list(
c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 3, 2, 2, 2, 2,
3, 2, 2), c(2, 3, 2, 2, 2, 2, 2, 3, 3), c(1, 1, 2, 2),
NULL, c(2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2,
3), c(2, 2, 2, 1, 2, 2, 2, 1, 1), c(2, 2, 2, 2, 2, 2,
2), c(2, 2, 4, 4, 2, 2, 22, 2), c(2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2,
2), c(2, 3, 3, 4, 2, 2, 2, 2, 2), c(2, 3, 3, 3, 2, 3,
3, 3, 3, 3, 2), c(2, 1, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3),
c(2, 2, 2, 2, 2, 2), c(1, 2, 2, 1, 2), c(2, 2, 2, 2,
2, 2, 2, 2, 2), c(1, 2, 2, 1, 2), c(2, 2, 2, 2, 2, 2,
1), c(1, 1), c(1, 1), c(1, 2, 2, 2, 1), c(2, 2, 1, 2),
2, c(2, 1), c(3, 1, 1, 1, 1, 2, 2), NULL, c(2, 1, 1),
c(2, 2), 1, 1, c(2, 2), NULL, c(9, 9, 17, 8, 19), c(6,
3, 2, 3, 3, 5, 3), c(16, 9, 12, 3, 7, 5), c(4, 4, 3,
3, 5, 3, 2), c(2, 2, 3, 3, 2, 3, 4, 2), c(2, 2, 2, 3,
4, 4, 2, 12, 3, 5, 4), c(2, 2, 3, 3, 2, 3, 2, 3, 3),
c(3, 3, 3, 3, 2, 5, 3, 1, 3), c(4, 2, 3, 2, 7, 2, 3),
c(3, 1, 12, 3, 4, 4, 2, 5, 5, 12), c(3, 2, 1, 5, 3, 2,
2, 1, 2, 3, 2), c(3, 2, 2, 5, 3, 3, 2, 2, 10), c(2, 2,
1, 1, 3, 2, 1, 1, 2), c(6, 2, 6, 2, 5, 3, 2, 2, 4, 11,
3, 2)), `Great Tit` = list(c(1, 2, 1, 1, 1, 1, 1, 1),
c(1, 2, 1, 2, 1, 2, 1, 1), NULL, c(1, 3, 2, 5, 3, 3,
4, 1), NULL, c(1, 2, 1, 1), c(1, 1), NULL, c(1, 1, 1,
2, 1, 1), 1, c(1, 1), c(1, 1, 1, 1), 1, NULL, c(2, 2,
1, 1), c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 1, 3, c(2, 2,
2, 1), c(4, 2, 1, 2), c(2, 1), c(8, 8, 12, 6, 8), c(2,
2, 3, 2), c(8, 3, 6, 4, 6), c(2, 2, 4, 2), c(1, 1, 2),
c(2, 2, 2), c(1, 2, 1, 2, 1, 2), c(2, 2, 2, 2, 2, 2,
2), c(4, 4, 6), c(2, 4, 2, 2, 4, 2, 2), c(3, 4, 2, 2,
3), 6, c(2, 2), c(1, 2), 2, c(2, 1, 1, 1, 1, 2), c(1,
1, 2, 2, 1, 2, 1), c(1, 1, 1, 2, 1, 2, 1, 2), c(2, 2,
3, 1, 2, 4, 1, 3), c(3, 1, 1, 2), c(1, 2, 2, 1, 1, 2,
2, 2, 1, 2), c(2, 1, 2, 1, 1, 1), c(2, 1, 2, 1), c(2,
3, 2, 3, 2, 1), c(1, 1, 1, 2, 2, 2, 1), c(1, 2, 2, 1,
1, 2, 3, 1, 2, 1, 3, 3), c(2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2), c(2, 2, 2, 8, 1, 2, 2, 2, 1, 2, 2, 2), c(2, 1,
1, 1, 1, 1, 1, 2)), Robin = list(c(1, 1, 3, 1, 3, 1,
1, 3, 3, 2), c(2, 2, 1, 2, 2), c(1, 2, 1, 1, 1, 1, 1), c(1,
1, 2, 1, 1, 1, 1, 1, 1, 2), 1, c(1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2, 2), c(3, 1, 2,
3, 3), 2, c(2, 5, 2, 2, 2, 2, 2, 5, 2, 2), NULL, c(2, 1,
3, 2), NULL, c(1, 2, 1, 2, 1, 1), c(2, 2), 2, 1, c(2, 2,
2, 1, 2, 2, 1, 2, 2, 2), NULL, c(3, 1, 3), c(4, 2, 4), 1,
c(2, 2, 4, 3, 2, 2), c(2, 2, 1, 2, 4, 2, 2, 2), c(2,
2, 3, 2, 3), c(3, 1), c(2, 2, 2, 2, 3), c(2, 6, 4, 2,
2, 2), 4, c(5, 5, 5, 5), c(2, 2, 1, 4, 2, 4, 4), 4, c(2,
2), c(4, 1, 4), 2, c(7, 2, 3, 2, 2, 3, 4, 4, 3), c(4,
3, 2, 1, 2, 2, 2), c(6, 1, 3, 2, 1, 2, 2), c(3, 1, 2,
3), 1, c(1, 1, 3, 3, 1, 5, 2, 1, 1, 3, 1), c(1, 2, 2,
2, 3, 1, 3), c(1, 1, 3, 1, 1, 3, 1), c(1, 4, 1, 3, 4),
c(2, 2, 1, 1, 1), c(2, 2, 5, 2, 1, 2, 1, 1), c(7, 2,
6, 1, 2), c(2, 1, 2, 1, 1), c(4, 2, 1, 1, 2, 1)), `House Sparrow` = list(
NULL, c(2, 2, 2, 2, 2, 2), NULL, c(2, 2, 4, 6, 3, 4,
3, 3), 3, c(3, 2, 2, 2), NULL, NULL, NULL, c(2, 2, 2),
c(1, 2, 2), c(2, 2), NULL, NULL, c(3, 5), c(2, 2, 2,
2, 2, 2, 2, 2), NULL, NULL, c(3, 3, 3, 3, 2, 3, 5, 3,
3, 3), NULL, c(2, 2, 1, 1, 1, 2), NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, c(20, 14, 12, 10, 8, 14, 21), c(6, 5, 9,
9, 9, 6), c(13, 12, 5, 21, 11, 12, 16, 10, 15), c(3,
2, 7, 3, 1), c(10, 11, 15, 8, 12, 15, 5, 16), c(1, 5,
5, 5, 5, 4, 5, 2, 6, 4, 4), c(2, 4, 1, 4, 3, 3, 7, 7,
3, 5, 3), c(9, 10, 10, 7, 8, 10, 10, 6, 8, 6, 12, 9),
c(10, 5, 13, 14, 4, 5, 9, 9, 10, 8, 9), c(10, 9, 10,
7, 9, 10, 8, 7, 9, 14), c(3, 7, 5, 10, 2, 6, 14, 6, 3,
7, 3), c(7, 9, 11, 5, 5, 7, 7, 6, 6, 10, 5, 7, 16), c(5,
7, 5, 5, 6, 8, 7, 4, 5), c(15, 10, 12, 9, 3, 9, 10, 11
)), `Coal Tit` = list(1, c(1, 1, 1), NULL, c(2, 3, 2,
2, 4, 2, 2), NULL, 2, NULL, NULL, c(1, 1, 1), NULL, c(2,
1), 1, 1, NULL, 2, NULL, 1, NULL, 2, 1, 2, c(1, 1, 1), c(2,
2, 2, 2, 2, 1), c(1, 1, 1, 1, 1), c(1, 1, 1, 1), c(1, 1,
2), 1, c(2, 1, 1, 1, 1, 1), c(2, 2, 1, 2, 1, 2, 2), c(2,
2), c(2, 2, 2), c(2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2), 2,
c(1, 1), NULL, 1, NULL, c(1, 1, 1), c(2, 1, 1, 2, 2,
1, 1, 3, 1), c(1, 1, 1, 1), c(2, 1, 1, 1, 1, 2), c(1,
1, 1, 2, 1, 1), NULL, c(2, 1, 1, 1), c(1, 1), c(1, 2,
1, 1, 2, 1, 2, 1, 2), c(1, 2, 1, 2, 1, 1, 1), c(1, 1,
1, 1, 1, 1, 1, 1, 1), 1), Woodpigeon = list(c(2, 3, 3,
3, 3, 3, 3, 5, 3), c(3, 4, 3, 3, 3, 5, 3, 3, 2, 4, 3), c(2,
1, 3, 3, 3, 3, 1, 3), c(3, 3, 3, 4, 1, 1, 5, 5, 5), 2, c(3,
4, 1, 3, 3, 3, 1, 5, 3), c(2, 6, 5, 3, 7, 5, 2, 1, 3, 2,
2), c(3, 3, 3, 3, 2, 2, 3, 4, 3, 3, 5, 5, 1), c(5, 5, 5,
3, 5, 4, 4, 5, 7), c(5, 4, 3, 4, 5, 4), c(3, 3, 3, 3, 3,
3, 5), c(3, 2, 3, 3, 3, 5, 6, 3, 3, 5, 5), c(5, 3, 2, 5,
3, 5, 3, 3, 3), c(2, 3, 3, 3, 4, 5, 3, 5, 5), c(3, 3, 3,
3, 5, 3, 3, 2, 3), c(3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2), c(3,
2, 3, 3, 3, 3, 3, 3, 3, 3, 3), c(3, 4, 3, 3, 5, 3, 3, 3),
c(3, 3, 4, 5, 5, 3, 3, 3), c(3, 3, 3, 3, 2, 3, 3, 3),
c(2, 2, 3, 3, 3, 2, 3, 3), c(4, 4, 4, 4), c(10, 8, 10
), c(5, 5, 5), c(6, 4, 6), c(6, 6, 6, 10, 6), c(6, 10
), c(20, 10, 10, 10, 4, 10, 8), c(6, 6, 4, 4, 6, 4, 4,
6, 6), NULL, c(8, 8, 8), c(3, 4, 4, 6, 3, 6, 3), NULL,
NULL, c(6, 4, 6, 4), 1, c(3, 3, 2, 3, 1, 3, 2, 1), c(5,
3, 4, 4, 2, 3, 3), c(1, 2, 5, 1, 4, 4, 3, 4, 2, 5, 2),
c(3, 2, 2, 1, 3, 2, 2, 1), c(5, 6, 2, 6, 2), c(1, 6,
2, 6, 2, 3, 3, 3), c(5, 3, 5, 4, 4, 2, 2, 5), c(5, 5,
3, 4, 2, 3, 5, 4), c(3, 2, 2, 2, 3, 2, 5), c(2, 2, 5,
3, 3, 5, 3, 4), c(2, 2, 1, 1, 5, 6, 2, 7, 5, 2, 3), c(5,
2, 3, 5, 2, 1, 5, 6, 4, 2), c(2, 3, 4, 3, 3, 4, 3, 3,
3, 3), c(7, 5, 3, 2, 5, 9, 2, 3, 3, 4, 3)), Blackbird = list(
c(3, 3, 1, 3, 3, 3, 3, 5, 5), c(3, 3, 3, 3, 3, 3, 5,
3, 3, 3), c(2, 1, 3, 3, 3, 3, 3), c(5, 5, 11, 7, 3, 11,
15, 10, 5, 3), NULL, c(7, 2,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, 1, 1, 1, 2, 1, 1), c(1, 1, 1, 1, 1), c(1, 1, 1, 1,
1, 1, 1, 2), c(1, 1, 1, 1, 1, 1), 1, c(1, 1, 11), c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1), c(1, 1, 1, 1), 1, c(1, 1, 1, 1, 1, 1, 1), c(1, 2, 1,
1, 1, 1, 1, 1), c(1, 1, 1, 1, 2, 1, 1, 1), c(1, 1, 1, 1,
1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), c(1,
1, 1, 1, 1, 1), c(2, 1, 1, 1, 2, 2, 2, 1, 1, 1), c(1, 1,
1, 1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1), c(1, 1, 1, 1,
1, 1), c(2, 1, 1, 1, 1, 1), c(2, 2, 1, 1), NULL, 1, 1, c(1,
1, 1), 1, c(1, 1, 2, 1), c(1, 2, 1, 2, 3, 1), NULL, c(1,
1), NULL, NULL, NULL, c(1, 2, 2), NULL, 1, c(1, 1), NULL,
NULL, NULL, NULL, NULL, NULL, 1, c(1, 1), 1, 1, c(1,
1, 2, 1, 2, 1, 1, 1, 1), NULL), Chaffinch = list(c(2,
1, 3, 3), c(2, 2, 2, 1, 2, 1), c(1, 2), c(1, 1, 3, 2, 2,
2, 2, 2, 1, 2), NULL, c(3, 3, 3, 2, 2, 2, 1), c(3, 1, 3,
2), NULL, c(5, 2, 2, 2, 4), c(2, 2), c(2, 2, 2, 2, 2, 3,
2, 2, 2, 2), c(5, 1, 3, 2, 3), c(2, 1, 1, 3), c(3, 2, 2,
2, 2, 3, 1), c(2, 2), c(2, 2, 2, 2, 2, 2, 2, 2, 2), c(2,
2, 3), c(2, 1, 2, 2, 2), c(4, 3, 2, 5, 2, 2), c(1, 3), c(3,
3, 1, 5, 1), c(2, 4, 5, 2, 2), c(2, 4, 2, 4, 1), c(6, 4,
4), c(5, 2, 4, 4, 5, 4, 4), c(6, 3, 4, 5, 4, 4, 3), c(4,
4, 4), c(4, 2, 6, 2), c(7, 6, 8, 8, 8, 4, 6, 4, 4, 4), c(10,
6), c(2, 6, 6, 4), c(4, 4, 5, 4, 4, 4, 5), c(10, 10, 10),
NULL, c(2, 2, 2, 4, 4, 4), NULL, c(6, 6, 5, 7, 3, 2),
c(4, 4, 2, 3, 10), c(1, 5, 3, 5, 4, 5, 3, 2, 4, 2), c(5,
4, 4, 2, 7, 6, 10, 2, 7, 2), c(2, 4, 2, 3, 4, 1, 4, 3,
1, 1), c(13, 7, 3, 6, 13, 9, 5, 7, 7, 11), c(10, 7, 9,
7, 9, 17, 11, 8, 4), c(1, 3, 3), c(1, 3, 4, 1, 1, 1,
2, 6, 4), c(5, 8, 6, 9, 9, 3, 11, 2, 5), c(2, 3, 3, 3,
2, 3), c(4, 3, 3, 5, 3, 4, 4, 4, 6, 3, 3, 3), c(3, 2,
3, 2, 3, 2, 4, 3, 2, 1, 2, 5, 3), c(12, 5, 12, 8, 18,
6, 3, 4, 9, 15, 7, 10)), Starling = list(c(1, 3), 1,
3, c(5, 5, 5, 5, 7, 7, 5), NULL, NULL, NULL, NULL, c(5,
9, 7, 5, 7, 7), NULL, c(1, 1, 2, 2, 1, 2), 3, NULL, NULL,
1, c(1, 3), c(1, 1), c(2, 2, 2, 2), c(5, 2, 1, 3, 7,
13, 1, 2, 2, 3), c(1, 2), NULL, NULL, NULL, NULL, NULL,
NULL, c(1, 1), NULL, 4, NULL, NULL, NULL, NULL, NULL,
c(4, 12), NULL, c(2, 28, 9, 2, 3, 9), c(3, 7, 8, 2, 3,
12, 3), c(2, 1, 6, 9, 18), c(11, 1, 5, 30, 10), c(25,
9, 8, 39, 20, 18, 30), c(15, 10, 9, 27, 14, 15, 30, 30,
19, 12), c(3, 8, 14, 2, 21, 19, 35), c(13, 8, 9, 21,
9, 28, 1, 5, 16), c(1, 2, 2, 1, 1, 8, 1), c(6, 27, 6,
25, 16, 10, 3, 40, 5, 30), c(2, 1, 3, 2, 3, 2, 1), c(6,
4, 24, 6, 8, 7, 9, 10), c(17, 3, 1, 11, 5, 5, 2, 6, 6,
5, 2, 3), c(2, 4, 1, 5, 3, 3, 14, 7, 5, 2, 6)), Goldfinch = list(
c(1, 3, 5, 1, 1), NULL, 2, NULL, NULL, NULL, c(2, 2,
2), c(3, 3, 3), c(2, 1), NULL, c(2, 3, 3, 2, 2, 3, 2),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 2, 2),
c(5, 3), 2, c(6, 10, 6, 6, 6, 8, 4), c(2, 6, 3), c(6,
4, 2), c(10, 10, 8, 8, 10, 10), c(1, 6, 6, 6, 1, 2, 6
), c(2, 2), c(2, 2, 4, 6), c(1, 4, 4, 4, 4, 8, 4, 6),
c(10, 8, 8), c(6, 6, 6, 2, 1), c(7, 8, 5, 8, 4), 10,
4, c(4, 3, 6, 4), 3, c(3, 5, 4, 2, 2, 6, 3), c(2, 4,
7, 6, 6, 6), c(10, 4, 6, 4, 5, 5, 5, 6), c(11, 15, 12,
9, 15, 8, 25), c(2, 1, 1, 1, 1, 1, 2, 1), c(23, 24, 12,
14, 20, 17, 13, 6, 18), c(18, 13, 19, 42, 10, 12, 21,
27, 7, 7), c(2, 2, 2, 1, 4), c(1, 5, 1, 7, 3, 3), c(6,
6, 18, 8, 6, 14, 16, 3, 7, 5, 4), c(8, 3, 1, 2, 2, 1,
1, 3, 1), c(1, 1, 1, 2, 6, 2), c(1, 1, 2, 2, 2, 1, 1),
c(12, 3, 6, 9, 9, 4)), Brambling = list(c(2, 2), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2,
2, 2, 2), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, c(1, 2, 2, 1), NULL, NULL, 1,
NULL, NULL, NULL, 1), Blackcap = list(NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, 1, NULL, c(1, 2, 2), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, NULL, c(1, 1, 1), NULL, NULL, 1, 1, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL,
NULL, 1, NULL, NULL, NULL, NULL, NULL, 1, NULL), Jackdaw = list(
2, c(1, 2), NULL, NULL, NULL, NULL, NULL, NULL, c(1,
1, 1, 2), NULL, c(1, 4, 1, 1, 4, 1, 1), 1, NULL, NULL,
c(6, 5, 5, 5, 5, 5), c(4, 2, 4, 1, 5, 1, 5, 1), c(7,
2, 5), c(1, 1, 1, 1), 1, NULL, NULL, NULL, NULL, c(4,
4), NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, c(2, 1), NULL, c(3, 5, 1, 2, 2), c(3, 2, 1, 1,
2, 4, 3, 3), c(3, 3, 6, 2, 9, 4, 10, 3, 2), c(6, 1, 1,
3, 4, 2, 3, 1), c(5, 3, 5, 4, 5, 4, 4), c(3, 2, 6, 5,
2, 3, 1, 3, 3, 4), c(6, 3, 2, 6, 2, 2, 3, 3, 3, 5, 5,
3), c(6, 5, 6, 5, 5, 8, 5, 4, 7, 6), c(5, 2, 3, 5, 4,
3, 3, 5, 2), c(3, 1, 2, 4, 2, 3, 1, 2), c(3, 5, 9, 4,
3, 5, 5, 5, 6, 5, 4, 5, 5), c(5, 1, 8, 6, 5, 6, 3, 3,
8, 6, 4), c(7, 6, 6, 6, 6, 5, 4, 3), c(3, 4, 2, 4, 2,
2, 2, 7, 11, 3, 6)), Siskin = list(NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 2, 2),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(1, 1), NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, c(2, 3), c(4,
2, 1, 2, 1), NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1, c(2, 2, 2), 1, NULL, c(3, 3), 2, c(4, 1, 2, 3), c(1,
4, 2), c(1, 1, 1, 1, 2), c(3, 3), NULL, c(2, 2, 1), c(5,
1, 2, 2, 2, 2, 2, 2), NULL), `Spotted Flycatcher` = list(
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, 2, NULL, 1, NULL, NULL, NULL, NULL, NULL, c(1,
1), NULL, c(1, 1, 1), NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL)), row.names = c(NA, -50L
), class = c("tbl_df", "tbl", "data.frame"))
Is this what you need?
library(dplyr)
library(purrr) # map_dbl
group_by(zz, year, Season, POSTCODE, LOC_ID) %>%
summarize_all(~ map_dbl(., sum, na.rm = TRUE)) %>%
ungroup()
# # A tibble: 50 x 25
# year Season POSTCODE LOC_ID Wren Dunnock `Blue Tit` `Pied/White Wag~ `Collared Dove` `Great Tit` Robin Greenfinch
# <dbl> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2010 Summer PE32 1TL LOC56~ 1 10 13 0 62 8 5 30
# 2 2010 Winter PE32 1TL LOC56~ 4 15 16 4 48 10 11 11
# 3 2011 Spring PE32 1TL LOC56~ 0 8 10 1 25 10 18 16
# 4 2011 Summer PE32 1TL LOC56~ 0 18 17 0 52 11 7 17
# 5 2011 Winter PE32 1TL LOC56~ 2 5 22 1 51 10 13 7
# 6 2012 Autumn PE32 1TL LOC56~ 2 6 28 1 24 18 4 9
# 7 2012 Winter PE32 1TL LOC56~ 4 10 18 6 43 16 6 22
# 8 2013 Winter PE32 1TL LOC56~ 2 6 20 2 23 8 5 14
# 9 2014 Autumn NR15 1TS LOC11~ 0 1 30 0 1 6 6 4
# 10 2014 Summer NR15 1TS LOC11~ 0 2 30 0 0 14 1 4
# # ... with 40 more rows, and 13 more variables: `House Sparrow` <dbl>, `Coal Tit` <dbl>, Woodpigeon <dbl>,
# # Blackbird <dbl>, `Song Thrush` <dbl>, Chaffinch <dbl>, Starling <dbl>, Goldfinch <dbl>, Brambling <dbl>,
# # Blackcap <dbl>, Jackdaw <dbl>, Siskin <dbl>, `Spotted Flycatcher` <dbl>
(You can do it without purrr::map_dbl, just use sapply in its place.)
We can use summarise with across
library(dplyr)
library(purrr)
zz %>%
group_by(year, Season, POSTCODE, LOC_ID) %>%
summarise(across(everything(), ~ map_dbl(., sum, na.rm = TRUE)))

Percentage histogram with facet_grid: x variable is a factor

I want to split a percentage histogram (that integrates to 100%) into two facets using facet_grid. However, when splitting to facets, each facet by itself doesn't integrate to 100%. This kind of question has been resolved here in the past, but I cannot translate that solution to my current situation where x is a factor, and thus a histogram using stat(density) doesn't work.
My Data
Dataframe with two columns. equipment denotes whether a household has enough equipment for homeschooling, and children_n denotes number of children.
library(tidyverse)
library(magrittr)
df <-
structure(list(equipment = c(1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0,
1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0,
0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1,
0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1,
1, 1, 1, 1, 0, 1, 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2, 3, 3, 4, 4, 2, 3, 2, 1, 3,
2, 3, 3, 2, 3, 3, 2, 3, 2, 3, 3, 1, 1, 2, 4, 3, 4, 3, 1, 3, 4,
2, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 1, 3, 3, 2, 1, 1, 4, 1, 3, 2,
1, 2, 3, 3, 2, 2, 2, 4, 2, 1, 3, 2, 3, 2, 1, 3, 1, 3, 1, 3, 3,
2, 1, 2, 3, 2, 3, 1, 2, 2, 2, 3, 3, 2, 3, 1, 3, 3, 3, 3, 2, 4,
2, 4, 4, 1, 2, 1, 2, 1, 3, 3, 3, 2, 3, 3, 4, 2, 2, 3, 2, 1, 2,
2, 1, 1, 3, 1, 2, 3, 3, 3, 2, 1, 1, 1, 2, 1, 2, 5, 1, 2, 1, 4,
2, 2, 2, 1, 4, 2, 3, 3, 3, 2, 4, 5, 4, 2, 4, 2, 3, 1, 4, 3, 3,
2, 3, 3, 2, 3, 2, 1, 3, 2, 4, 2, 3, 4, 1, 2, 3, 1, 3, 3, 4, 2,
2, 2, 3, 3, 2, 1, 2, 2, 1, 3, 1, 3, 1, 1, 1, 3, 2, 2, 4, 3, 4,
3, 3, 4, 1, 1, 3, 3, 2, 3, 2, 3, 2, 1, 3, 3, 1, 5, 1, 1, 2, 4,
2, 3, 5, 4, 1, 3, 2, 1, 2, 2, 4, 3, 4, 2, 2, 1, 3, 2, 4, 2, 3,
3, 2, 3, 2, 1, 2, 3, 4)), row.names = c(NA, -1059L), class = c("tbl_df",
"tbl", "data.frame"))
df
## # A tibble: 1,059 x 2
## equipment children_n
## <dbl> <dbl>
## 1 1 4
## 2 0 4
## 3 1 2
## 4 1 2
## 5 0 2
## 6 1 1
## 7 1 1
## 8 1 3
## 9 1 2
## 10 1 3
## # ... with 1,049 more rows
In cases where number of children is above 6, I want to collapse those cases to one category of "6+".
df %<>%
mutate_at(vars(children_n), as.character) %>%
mutate_at(vars(children_n), recode, "9" = "6_plus", "8" = "6_plus", "7" = "6_plus", "6" = "6_plus") %>%
mutate_at(vars(children_n), fct_relevel, "1", "2", "3", "4", "5", "6_plus")
glimpse(df)
## Rows: 1,059
## Columns: 2
## $ equipment <dbl> 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, ...
## $ children_n <fct> 4, 4, 2, 2, 2, 1, 1, 3, 2, 3, 3, 6_plus, 3, 2, 1, 2, 1, 1, 3, 3, 3, 2, 3, 3, 3, 2, 4, 3, 1, 2, 3, 4, 4, 1, 2, 5, 2, 6_plus, 1, 2, 1, 2,...
Now I want to plot the proportion of number of children in two separate panels: one panel for families who have enough equipment, and another panel for families who don't have enough equipment:
df %>%
ggplot(data = ., aes(x = children_n, y = equipment)) +
geom_histogram(aes(y = (..count..)/sum(..count..)), stat = "count" , fill = "darkblue") +
geom_text(aes(label = scales::percent(((..count..)/sum(..count..)), accuracy = 1),
y = ((..count..)/sum(..count..)) ), stat= "count", vjust = -.5, color = "darkblue") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ equipment, labeller = as_labeller(c("1" = "have enough equipment",
"0" = "don't have enough equipment")))
This gives two panels that *DON'T* integrate to 100% independently:
Trying to solve the problem
I found this question that describes the same intention and problem. The chosen solution suggests defining the geom_histogram as density so it integrates to 100%. But this won't work in my case because stat(density) asks that the x variable will be continuous, unlike my case where x is a factor.
df %>%
ggplot(data = ., aes(x = children_n, y = equipment)) +
geom_histogram(aes(y = stat(density) * 6), binwidth = 6, fill = "darkblue") +
facet_grid(~ equipment, labeller = as_labeller(c("1" = "have enough equipment",
"0" = "don't have enough equipment")))
Error: StatBin requires a continuous x variable: the x variable is
discrete. Perhaps you want stat="count"?
Other approaches suggest using ..PANEL.. while others are strongly against it.
How can I get the two facets to show percents that independently integrate to 100%, in a proper way?
This could be achieved like so:
Map the facetting variable on the group aes
Use e.g. tapply to get the total number per group or facet
BTW: I have put the code for the normalization inside a helper function to reduce the code duplication and readability
library(tidyverse)
library(magrittr)
df %<>%
mutate_at(vars(children_n), as.character) %>%
mutate_at(vars(children_n), recode, "9" = "6_plus", "8" = "6_plus", "7" = "6_plus", "6" = "6_plus") %>%
mutate_at(vars(children_n), fct_relevel, "1", "2", "3", "4", "5", "6_plus")
help <- function(count, group) {
count / tapply(count, group, sum)[group]
}
df %>%
ggplot(data = ., aes(x = children_n, y = equipment, group = equipment)) +
geom_histogram(aes(y = help(..count.., ..group..)), stat = "count" , fill = "darkblue") +
geom_text(aes(label = scales::percent(help(..count.., ..group..), accuracy = 1),
y = help(..count.., ..group..) ), stat= "count", vjust = -.5, color = "darkblue") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ equipment, labeller = as_labeller(c("1" = "have enough equipment",
"0" = "don't have enough equipment")))
#> Warning: Ignoring unknown parameters: binwidth, bins, pad

R: how to count the number of times two elements have the same ID (perhaps using the outer function)

I have the following three dimensional array:
dput(a)
structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 2, 1, 1, 1, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 6, 2, 7, 6, 2, 7, 6, 2, 7, 4, 2, 4, 4, 2, 6, 4, 2, 4, 6, 2,
7, 4, 2, 6, 4, 2, 6, 4, 2, 6, 4, 2, 4, 4, 2, 6, 4, 2, 4, 4, 2,
6, 4, 2, 6, 4, 2, 6, 6, 2, 7, 4, 2, 6, 4, 2, 6, 4, 2, 4, 2, 3,
1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 7, 2, 3,
7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3,
7, 2, 3, 7, 1, 2, 5, 2, 3, 7, 1, 2, 4, 2, 3, 7, 2, 3, 7, 2, 3,
7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 6, 3, 2, 6, 3, 2, 6,
3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6,
3, 1, 1, 1, 2, 6, 3, 1, 5, 5, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6,
3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 13,
2, 3, 13, 2, 3, 5, 2, 3, 5, 2, 15, 17, 2, 15, 17, 2, 15, 17,
2, 3, 5, 2, 15, 17, 2, 3, 13, 2, 15, 17, 2, 15, 17, 2, 3, 13,
2, 3, 5, 2, 15, 17, 2, 15, 17, 2, 3, 5, 2), .Dim = c(3L, 20L,
6L), .Dimnames = list(c("cl.tmp", "cl.tmp", "cl.tmp"), NULL,
NULL))
The dimension of this array (a) is 3x20x6 (after edits).
I wanted to count the proportion of times that a[,i,] matches a[,j,] element-by-element in the matrix. Basically, I wanted to get mean(a[,i,] == a[,j,]) for all i, j, and I would like to do this fast but in R.
It occurred to me that the outer function might be a possibility but I am not sure how to specify the function. Any suggestions, or any other alternative ways?
The output would be a 20x20 symmetric matrix of nonnegative elements with 1 on the diagonals.
The solution given below works (thanks!) but I have one further question (sorry).
I would like to display the coordinates above in a heatmap. I try the following:
n<-dim(a)[2]
xx <- matrix(apply(a[,rep(1:n,n),]==a[,rep(1:n,each=n),],2,sum),nrow=n)/prod(dim(a)[-2])
image(1:20, 1:20, xx, xlab = "", ylab = "")
This gives me the following heatmap.
However, I would like to display (reorder the coordinate) such that I get all the coordinates that have high-values amongst each other together. However, I would not like to bias the results by deciding on the number of groups myself. I tried
hc <- hclust(as.dist(1-xx), method = "single")
but I can not decide how to cut the resulting tree to decide on bunching the coordinates together. Any suggestions? Bascically, in the figure, I would like the coordinate pairs in the top left (and bottom right off-diagonal blocks) to be as low-valued (in this case as red) as possible.
Looking around on SO, I found that there exists a function heatmap which might do this,
heatmap(xx,Colv=T,Rowv=T, scale='none',symm = T)
and I get the following:
which is all right, but I can not figure out how to get rid of the dendrograms on the sides or the axes labels. It does work if I extract out and do the following:
yy <- heatmap(xx,Colv=T,Rowv=T, scale='none',symm = T,keep.dendro=F)
image(1:20, 1:20, xx[yy$rowInd,yy$colInd], xlab = "", ylab = "")
so I guess that is what I will stick with. Here is the result:
Try this:
n<-dim(a)[2]
matrix(apply(a[,rep(1:n,n),]==a[,rep(1:n,each=n),],2,sum),nrow=n)/prod(dim(a)[-2])
It has to be stressed that the memory usage of this method goes with n^2 so you might have trouble to use it with larger arrays.

Resources