converting NULL to numeric and taking the sum of lists - r
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,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, 1, 1, 2,
2, 2, 2, 1), c(1, 1, 1, 1, 2, 1, 1), c(1, 3, 3, 3), c(3,
1, 1, 2), c(1, 1, 1, 1, 1), c(2, 2, 2, 2, 2, 3, 2), c(2,
1, 2, 1, 3), c(2, 2), c(3, 1, 3, 5, 2, 2, 2, 2, 2), c(3,
4, 4, 2, 3, 2, 2, 4, 2), 1, c(4, 2, 4, 2, 4), c(1, 1, 3),
c(2, 2, 2), 2, c(3, 2, 2), c(1, 2), c(1, 1, 1, 1, 1),
c(3, 2, 2, 2, 4, 2, 2, 1), c(2, 1, 2, 1, 1), c(1, 1,
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1), c(1, 1, 2, 1, 1),
c(1, 1, 1, 1, 1), c(1, 1, 1, 1, 1, 1, 1), c(2, 4, 3,
2, 1, 6, 2, 3, 1, 2), c(1, 1, 2, 1, 1, 1, 2, 1, 2, 1),
c(1, 1, 1, 1, 1, 2, 1, 1, 1), c(1, 1, 1, 2, 1, 1, 1,
1, 1), c(2, 1, 2, 1, 2, 1, 2, 1), c(1, 1, 1, 1, 2, 1,
1, 1, 2)), Greenfinch = list(2, c(2, 2, 2, 2, 2, 2, 2
), 1, c(1, 1, 2), NULL, NULL, NULL, c(2, 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, 9, 3, 6, 3, 2, 3, 5), c(5,
2, 3, 1, 3, 5, 2, 1), c(3, 3, 4, 1, 2, 3, 3, 2, 3, 4,
2), c(4, 3, 3, 5, 4, 5, 5, 4, 3, 3, 5, 3), c(11, 7, 5,
4, 11, 11, 5), c(2, 4, 2, 3, 5, 6, 3, 3), c(3, 3, 3,
3, 4, 4, 3, 3), c(3, 3, 2, 2, 2, 3, 4, 3, 2), c(5, 13,
3, 5, 7, 4, 3, 7), c(5, 8, 6, 5, 5, 6, 3, 5, 10), c(4,
3, 8, 4, 3, 6, 3), c(5, 5, 5, 2, 5, 3, 3, 3), c(3, 3,
3, 3, 5, 5, 4, 4, 5, 4, 3), c(3, 3, 4, 5, 4, 5, 5, 2),
c(5, 5, 1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 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)))
Related
Error using aggregate to find length with missing values
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)
Plotly does not plot a non-quadratic surface
I want to produce a 3D scatterplot and add a surface fitted with a linear regression, using plotly. My data: structure(list(political_trust = c(1, 6, 7, 5, 0, 2, 1, 3, 5, 0, 2, 5, 5, 6, 6, 3, 3, 2, 5, 8, 3, 7, 3, 4, 5, 4, 5, 0, 0, 4, 6, 1, 0, 4, 0, 5, 5, 6, 7, 3, 5, 4, 5, 2, 4, 4, 7, 6, 7, 5, 4, 6, 7, 5, 7, 3, 3, 3, 2, 5, 2, 7, 3, 2, 7, 2, 3, 0, 7, 5, 7, 3, 0, 7, 2, 6, 3, 8, 7, 2, 2, 5, 0, 1, 6, 3, 6, 5, 1, 3, 4, 4, 5, 3, 3, 0, 2, 4, 9, 6, 3, 3, 2, 3, 4, 5, 8, 0, 4, 1, 5, 0, 4, 0, 5, 6, 3, 2, 7, 5, 4, 3, 8, 3, 4, 0, 3, 6, 7, 7, 2, 3, 5, 5, 5, 0, 3, 2, 1, 7, 5, 0, 4, 0, 2, 7, 3, 0, 8, 3, 2, 4, 5, 5, 3, 2, 3, 8, 6, 5, 6, 7, 0, NA, 7, 7, 2, 0, 3, 4, 7, 2, 1, 2, 0, 0, 4, 3, 3, 6, 6, 1, 4, 0, 4, 0, 0, 7, 6, 4, 4, 6, 5, 4, 3, 3, 0, NA, 2, 5), political_interest = c(2, 0, 3, 3, 2, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 3, 2, 1, 2, 2, 2, 2, 0, 2, 1, 3, 1, 1, 1, 1, 1, 2, 3, 2, 2, 2, 1, 3, 3, 2, 3, 2, 1, 3, 2, 0, 3, 1, 1, 2, 1, 2, 2, 1, 3, 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 5, 1)), row.names = c(NA, -200L), class = "data.frame") I start by defining a list of relevant variables - this is not necessary but basically a consequence of using the code in a Shiny up: input <- list() input$x <- "education_level" input$y <- "political_trust" input$z <- "political_interest" Next, creating the surface data: # Regressing "political_interest" on "education_level" and "political_trust": lm <- lm(as.formula(paste0(input$z, " ~ ", input$x, " + ", input$y)), data) # Defining range of values that outcome will be predicted for axis_x <- seq(min(data[, input$x], na.rm = T), max(data[, input$x], na.rm = T), by = 0.2) axis_y <- seq(min(data[, input$y], na.rm = T), max(data[, input$y], na.rm = T), by = 0.2) # Predicting outcome, and getting data into surface format lm_surface <- expand.grid(x = axis_x, y = axis_y, KEEP.OUT.ATTRS = F) colnames(lm_surface) <- c(input$x, input$y) lm_surface <- acast(lm_surface, as.formula(paste0(input$x, " ~ ", input$y)), value.var = input$z) Last, plotting this with plotly: data %>% filter(!is.na(get(input$z))) %>% filter(!is.na(get(input$x))) %>% filter(!is.na(get(input$y))) %>% plot_ly(., x = ~jitter(get(input$x), factor = 2.5), y = ~jitter(get(input$y), factor = 2.5), z = ~jitter(get(input$z), factor = 2.5), type = "scatter3d", mode = "markers", marker = list(size = 2, color = "#cccccc")) %>% add_surface(., z = lm_surface, x = axis_x, y = axis_y, type = "surface") This gives me the following. As you can see, the surface does not cover the full range of the y-dimension. Note also that the surface plotted is "quadratic" - i.e. same length in x and y - although it should have non-quadratic dimensions. I can bring plotly to draw larger surface area, e.g. by changing the range of values like below, but it always stays quadratic. axis_x <- seq(0, 10, by = 0.2) axis_y <- seq(0, 10, by = 0.2)
Ok, question solved. It's important which dimension of the surface matrix (lm_surface) is which. Swapping x and y when applying acast fixes the issue: lm_surface <- acast(lm_surface, as.formula(paste0(input$y, " ~ ", input$x)), value.var = input$z)
Add legend to graph in R
For a sample dataframe: df <- structure(list(antibiotic = c(0.828080341411847, 1.52002304506738, 1.31925434545302, 1.66681722567074, 1.17791610945551, 0.950096368502059, 1.10507733691997, 1.0568193215304, 1.03853131016669, 1.02313195567946, 0.868629787234043, 0.902126485349154, 1.12005679002801, 1.88261441540084, 0.137845900627507, 1.07040656448604, 1.41496470588235, 1.30978543173373, 1.16931780610558, 1.05894439450366, 1.24805122785724, 1.21318238007025, 0.497310305098053, 0.872362356327429, 0.902584749481137, 0.999731895498823, 0.907560340983954, 1.05930840957587, 1.40457554864091, 1.09747179272879, 0.944219456216072, 1.10363111431903, 0.974649273935516, 0.989983064420841, 1.14784471036171, 1.17232858907798, 1.44675812720393, 0.727078405331282, 1.36341361598635, 1.06120293299474, 1.06920290856811, 0.711007267992205, 1.39034247642439, 0.710873996527168, 1.30529753573398, 0.781191310196629, 0.921788181250106, 0.932214675722466, 0.752289683770589, 0.942392026874501 ), year = c(3, 1, 4, 1, 2, 4, 1, 3, 4, 3, 4, 1, 2, 3, 4, 1, 1, 4, 1, 1, 1, 1, 4, 1, 3, 3, 1, 4, 1, 4, 2, 1, 1, 1, 3, 4, 3, 2, 2, 2, 3, 3, 1, 2, 3, 2, 3, 4, 4, 1), imd.decile = c(8, 2, 5, 5, 4, 3, 2, 8, 6, 4, 3, 6, 9, 2, 5, 3, 5, 6, 4, 2, 9, 11, 2, 8, 3, 5, 7, 8, 7, 4, 9, 7, 6, 4, 8, 10, 5, 6, 6, 11, 6, 4, 2, 4, 10, 8, 2, 8, 4, 3)), .Names = c("antibiotic", "year", "imd.decile" ), row.names = c(17510L, 6566L, 24396L, 2732L, 13684L, 28136L, 1113L, 15308L, 28909L, 21845L, 23440L, 1940L, 8475L, 22406L, 27617L, 4432L, 3411L, 27125L, 6891L, 6564L, 1950L, 5683L, 25240L, 5251L, 20058L, 18068L, 5117L, 29066L, 2807L, 24159L, 12309L, 6044L, 7629L, 2336L, 16583L, 23921L, 17465L, 14911L, 8879L, 13929L, 17409L, 19421L, 7239L, 11570L, 15283L, 8283L, 16246L, 27950L, 23723L, 4411L), class = "data.frame") I am trying to graph imd.decile by antibiotic for each year library(ggplot2) p <- ggplot(df, aes(x = imd.decile, y = antibiotic, group = factor(year))) + stat_summary(geom = "line", fun.y = mean) p How do I add the wave to colour the corresponding graph and add a legend (I can't seem to use the aes command correctly).
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.
dplyr: Add variable as function of all variables in each row
I am trying to add a new variable in a dataframe using dplyr but I find it difficult. The new variable should be the number of runs with length 2 (of all the variable values in each line). Using apply I would do this: tmp$rle = apply(tmp,1,function(x) sum(rle(x)$lengths==2)) How can I perform this action using dplyr and mutate (without defining all variable names) ? tmp <- structure(list(X1 = c(3, 1, 1, 4, 4, 1, 3, 2, 2, 2, 1, 3, 3, 2, 3, 1, 4, 2, 3, 2), X2 = c(2, 4, 2, 2, 3, 2, 1, 1, 3, 1, 3, 1, 4, 4, 4, 1, 3, 1, 2, 1), X3 = c(2, 4, 3, 3, 3, 2, 4, 3, 4, 4, 2, 3, 3, 3, 1, 3, 1, 4, 4, 2), X4 = c(1, 3, 3, 1, 1, 3, 2, 4, 4, 1, 4, 4, 1, 1, 1, 3, 1, 3, 1, 1), X5 = c(4, 2, 4, 2, 1, 4, 1, 2, 2, 4, 3, 4, 1, 1, 4, 4, 2, 4, 4, 3), X6 = c(3, 1, 4, 3, 4, 4, 4, 1, 1, 3, 4, 2, 2, 2, 3, 2, 3, 2, 2, 3), X7 = c(4, 2, 1, 1, 2, 1, 3, 3, 3, 3, 2, 2, 4, 4, 2, 4, 4, 3, 3, 4), X8 = c(1, 3, 2, 4, 2, 3, 2, 4, 1, 2, 1, 1, 2, 3, 2, 2, 2, 1, 1, 4)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8"), row.names = c(NA, 20L), class = "data.frame")
Rather than dplyr, you might consider using the purrr package which RStudio has fairly recently introduced as a complement to dplyr to, among other things, better handle vectors and lists. In your case, tmp is a numeric data frame where you want to treat each row as a vector. The code could look like: library(purrr) tmp <- tmp %>% by_row(..f=function(x) sum(rle(x)$lengths==2), .to = "rle", .collate = "cols")
In dplyr: tmp <- mutate(tmp, rle = apply(tmp, 1, function(x) sum(rle(x)$lengths==2))) I am having a difficult time QA'ing this as I am unfamiliar with what results I should expect out of the rle function. I tried comparing results with your apply version of the code, and it seems that set.seed() is perhaps important for replicability? Am I understanding this correctly? Here is the QA attempt I made: (original tmp should be exactly the same: I just wrapped the lines at the list() and structure() arguments.) set.seed(1) tmp <- structure(list(X1 = c(3, 1, 1, 4, 4, 1, 3, 2, 2, 2, 1, 3, 3, 2, 3, 1, 4, 2, 3, 2), X2 = c(2, 4, 2, 2, 3, 2, 1, 1, 3, 1, 3, 1, 4, 4, 4, 1, 3, 1, 2, 1), X3 = c(2, 4, 3, 3, 3, 2, 4, 3, 4, 4, 2, 3, 3, 3, 1, 3, 1, 4, 4, 2), X4 = c(1, 3, 3, 1, 1, 3, 2, 4, 4, 1, 4, 4, 1, 1, 1, 3, 1, 3, 1, 1), X5 = c(4, 2, 4, 2, 1, 4, 1, 2, 2, 4, 3, 4, 1, 1, 4, 4, 2, 4, 4, 3), X6 = c(3, 1, 4, 3, 4, 4, 4, 1, 1, 3, 4, 2, 2, 2, 3, 2, 3, 2, 2, 3), X7 = c(4, 2, 1, 1, 2, 1, 3, 3, 3, 3, 2, 2, 4, 4, 2, 4, 4, 3, 3, 4), X8 = c(1, 3, 2, 4, 2, 3, 2, 4, 1, 2, 1, 1, 2, 3, 2, 2, 2, 1, 1, 4)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8"), row.names = c(NA, 20L), class = "data.frame") tmpApply <- tmp tmpApply$rle = apply(tmp, 1, function(x) sum(rle(x)$lengths==2)) tmpDplyr <- tmp %>% mutate(rle = apply(tmp, 1, function(x) sum(rle(x)$lengths==2))) tmpApply tmpDplyr