Looping to create a new column of counts from specific rows - r

I'd like to make a new column here in my dataset. I want to pick counts out from the full_name column for the species Stegastes planifrons and make a new column with counts (including transects where the count is 0). I used this code:
Fish1$Stegastes_planifrons <- rep(0, nrow(Fish1))
for (i in 1:nrow(Fish1)) {
if (Fish1$Full_name[i] == "Stegastes planifrons") {
Fish1$Stegastes_planifrons[i] == Fish1$count[i]
}
}
EDIT: Data in dput form:
structure(list(Year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2019), Location = c("Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela", "Tela",
"Tela"), Site = c("AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD",
"AD", "AD", "AD", "AD"), Depth = c(10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10), Transect = 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, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Family = c("Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Serranidae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Haemulidae", "Pomacentridae", "Pomacentridae",
"Acanthuridae", "Haemulidae", "Haemulidae", "Haemulidae", "Scaridae",
"Carangidae", "Scaridae", "Labridae", "Scaridae", "Scaridae",
"Haemulidae", "Scaridae", "Scaridae", "Scaridae", "Scaridae",
"Scaridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Scaridae", "Pomacentridae", "Pomacentridae",
"Haemulidae", "Pomacentridae", "Pomacentridae", "Haemulidae",
"Lutjanidae", "Scaridae", "Pomacentridae", "Pomacentridae", "Pomacanthidae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Labridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Pomacentridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Chaetodontidae", "Haemulidae", "Haemulidae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Pomacentridae",
"Labridae", "Pomacentridae", "Pomacentridae", "Haemulidae", "Haemulidae",
"Pomacentridae"), Genus = c("Stegastes", "Anisotremus", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Serranus", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Microspathodon", "Abudefduf", "Microspathodon",
"Haemulon", "Microspathodon", "Abudefduf", "Acanthurus", "Haemulon",
"Anisotremus", "Anisotremus", "Sparisoma", "Caranx", "Scarus",
"Halichoeres", "Scarus", "Sparisoma", "Anisotremus", "Sparisoma",
"Scarus", "Scarus", "Scarus", "Scarus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Scarus", "Microspathodon", "Stegastes",
"Stegastes", "Microspathodon", "Microspathodon", "Stegastes",
"Stegastes", "Scarus", "Stegastes", "Stegastes", "Haemulon",
"Stegastes", "Stegastes", "Anisotremus", "Lutjanus", "Scarus",
"Stegastes", "Stegastes", "Holacanthus", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Halichoeres", "Stegastes",
"Stegastes", "Microspathodon", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Stegastes", "Stegastes",
"Stegastes", "Stegastes", "Stegastes", "Thalassoma", "Chaetodon",
"Haemulon", "Haemulon", "Stegastes", "Thalassoma", "Stegastes",
"Stegastes", "Stegastes", "Halichoeres", "Stegastes", "Chromis",
"Haemulon", "Haemulon", "Stegastes"), Species = c("planifrons",
"virginicus", "adustus", "partitus", "partitus", "adustus", "partitus",
"adustus", "adustus", "adustus", "variabilis", "partitus", "adustus",
"adustus", "tortugarum", "adustus", "partitus", "adustus", "partitus",
"chrysurus", "saxatilis", "chrysurus", "carbonarium", "chrysurus",
"saxatilis", "coeruleus", "chrysargyreum", "virginicus", "virginicus",
"viride", "ruber", "vetula", "bivittatus", "iseri", "viride",
"virginicus", "viride", "iseri", "iseri", "iseri", "vetula",
"partitus", "adustus", "adustus", "adustus", "iseri", "chrysurus",
"adustus", "adustus", "chrysurus", "chrysurus", "adustus", "adustus",
"iseri", "diencaeus", "adustus", "sciurus", "adustus", "adustus",
"virginicus", "mahogoni", "vetula", "partitus", "adustus", "tricolor",
"partitus", "adustus", "adustus", "partitus", "partitus", "maculipinna",
"partitus", "variabilis", "chrysurus", "adustus", "variabilis",
"adustus", "partitus", "adustus", "adustus", "partitus", "adustus",
"adustus", "diencaeus", "partitus", "bifasciatum", "capistratus",
"carbonarium", "carbonarium", "adustus", "bifasciatum", "adustus",
"partitus", "diencaeus", "bivittatus", "adustus", "insolata",
"sciurus", "carbonarium", "adustus"), Full_name = c("Stegastes planifrons",
"Anisotremus virginicus", "Stegastes adustus", "Stegastes partitus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Stegastes variabilis", "Stegastes partitus", "Stegastes adustus",
"Stegastes adustus", "Serranus tortugarum", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes partitus",
"Microspathodon chrysurus", "Abudefduf saxatilis", "Microspathodon chrysurus",
"Haemulon carbonarium", "Microspathodon chrysurus", "Abudefduf saxatilis",
"Acanthurus coeruleus", "Haemulon chrysargyreum", "Anisotremus virginicus",
"Anisotremus virginicus", "Sparisoma viride", "Caranx ruber",
"Scarus vetula", "Halichoeres bivittatus", "Scarus iseri", "Sparisoma viride",
"Anisotremus virginicus", "Sparisoma viride", "Scarus iseri",
"Scarus iseri", "Scarus iseri", "Scarus vetula", "Stegastes partitus",
"Stegastes adustus", "Stegastes adustus", "Stegastes adustus",
"Scarus iseri", "Microspathodon chrysurus", "Stegastes adustus",
"Stegastes adustus", "Microspathodon chrysurus", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes adustus", "Scarus iseri", "Stegastes diencaeus",
"Stegastes adustus", "Haemulon sciurus", "Stegastes adustus",
"Stegastes adustus", "Anisotremus virginicus", "Lutjanus mahogoni",
"Scarus vetula", "Stegastes partitus", "Stegastes adustus", "Holacanthus tricolor",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes partitus", "Halichoeres maculipinna",
"Stegastes partitus", "Stegastes variabilis", "Microspathodon chrysurus",
"Stegastes adustus", "Stegastes variabilis", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes partitus", "Stegastes adustus", "Stegastes adustus",
"Stegastes diencaeus", "Stegastes partitus", "Thalassoma bifasciatum",
"Chaetodon capistratus", "Haemulon carbonarium", "Haemulon carbonarium",
"Stegastes adustus", "Thalassoma bifasciatum", "Stegastes adustus",
"Stegastes partitus", "Stegastes diencaeus", "Halichoeres bivittatus",
"Stegastes adustus", "Chromis insolata", "Haemulon sciurus",
"Haemulon carbonarium", "Stegastes adustus"), Guild = c("Omnivore",
"Invertivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Planktivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Invertivore", "Herbivore", "Invertivore", "Herbivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Carnivore",
"Herbivore", "Invertivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Invertivore", "Herbivore", "Herbivore", "Invertivore", "Carnivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Herbivore", "Omnivore", "Herbivore", "Herbivore", "Omnivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Herbivore",
"Herbivore", "Herbivore", "Herbivore", "Herbivore", "Invertivore",
"Invertivore", "Invertivore", "Invertivore", "Herbivore", "Invertivore",
"Herbivore", "Herbivore", "Herbivore", "Invertivore", "Herbivore",
"Planktivore", "Invertivore", "Invertivore", "Herbivore"), count = c(13,
4, 41, 10, 10, 41, 10, 41, 41, 41, 2, 10, 41, 41, 2, 41, 10,
41, 10, 4, 3, 4, 1, 4, 3, 1, 1, 4, 4, 3, 1, 1, 1, 2, 3, 4, 3,
6, 6, 6, 2, 2, 40, 40, 40, 6, 6, 40, 40, 6, 6, 40, 40, 6, 1,
40, 1, 40, 40, 1, 1, 2, 14, 52, 1, 14, 52, 52, 14, 14, 1, 14,
5, 4, 52, 5, 52, 14, 52, 52, 14, 52, 52, 2, 14, 2, 2, 3, 3, 52,
2, 52, 14, 2, 1, 52, 1, 2, 3, 52), `Transect ID` = c("2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_1_10", "2019_Tela_AD_1_10", "2019_Tela_AD_1_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_2_10", "2019_Tela_AD_2_10",
"2019_Tela_AD_2_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10",
"2019_Tela_AD_3_10", "2019_Tela_AD_3_10", "2019_Tela_AD_3_10"
), Stegastes_planifrons = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 104L, 105L, 106L, 107L, 108L,
109L, 110L), class = "data.frame")

Without your data (I can't extract your data from the image you posted), I can't test/troubleshoot this. The best way to share data is to dput(Fish1) and copy/paste the value returned in the console as an edit to your original question. If that is too cumbersome because the dataset is large, you could share a subset of your data or an example dummy dataset. This will get more eyes on your question and will lead to a higher quality answer that's more likely to solve your problem.
Anyways, here is a demonstration of how to summarize counts by group using dplyr. If you still have issues after working through this example, please let me know. I'll help the best I can. Thanks!
data(iris)
library(dplyr)
# Dummy transect and count variable for demo
iris$transect <- sample.int(n = 3, size = nrow(iris), replace = TRUE)
iris$count <- iris$Sepal.Length
virginica _counts <- iris %>%
filter(Species == "virginica") %>%
group_by(transect) %>%
summarize(counts = sum(count))
virginica_counts
#> # A tibble: 3 x 2
#> transect counts
#> <int> <dbl>
#> 1 1 112.
#> 2 2 130.
#> 3 3 87.9
Created on 2022-04-05 by the reprex package (v2.0.1)
EDIT
Using your data:
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
filter(Full_name == "Stegastes planifrons") %>%
summarize(counts = sum(count))
Transect_ID counts
<chr> <dbl>
1 2019_Tela_AD_1_10 13
2 2019_Tela_AD_2_10 0
3 2019_Tela_AD_3_10 0
EDIT 2
Based on the conversation in the comments, I think this is what you have described:
Fish1_Splaniforms <- Fish1 %>%
group_by(Transect_ID, .drop = FALSE) %>%
mutate(S.planiforms_counts = sum(count[Full_name == "Stegastes planifrons"]))
However, if the ultimate plan is for each species to get its own column that indicates counts (of that species) and rows that indicate transects, you could convert from long to wide:
library(reshape2)
Fish1$Transect_ID <- Fish1$`Transect ID`
Fish1_wide <- dcast(Fish1, Transect_ID ~ Full_name, value.var="count", fun.aggregate = sum)
head(Fish1_wide)
#> Transect_ID Abudefduf saxatilis Acanthurus coeruleus
#> 1 2019_Tela_AD_1_10 6 1
#> 2 2019_Tela_AD_2_10 0 0
#> 3 2019_Tela_AD_3_10 0 0
#> Anisotremus virginicus Caranx ruber Chaetodon capistratus Chromis insolata
#> 1 16 1 0 0
#> 2 1 0 0 0
#> 3 0 0 2 1
#> Haemulon carbonarium Haemulon chrysargyreum Haemulon sciurus
#> 1 1 1 0
#> 2 0 0 1
#> 3 9 0 2
#> Halichoeres bivittatus Halichoeres maculipinna Holacanthus tricolor
#> 1 1 0 0
#> 2 0 0 0
#> 3 1 1 1
#> Lutjanus mahogoni Microspathodon chrysurus Scarus iseri Scarus vetula
#> 1 0 12 2 1
#> 2 1 18 30 4
#> 3 0 4 0 0
#> Serranus tortugarum Sparisoma viride Stegastes adustus Stegastes diencaeus
#> 1 2 9 369 0
#> 2 0 0 400 1
#> 3 0 0 676 4
#> Stegastes partitus Stegastes planifrons Stegastes variabilis
#> 1 60 13 2
#> 2 2 0 0
#> 3 126 0 10
#> Thalassoma bifasciatum
#> 1 0
#> 2 0
#> 3 4

Related

Piecewise regression with multiple covariables with segmented package

I want to make some piecewise regression with the segmented package on R, but I'm a beginner with this package and I don't find how to do what I want.
Here, I have a plot with 6 curves that shows the performance (axis y) depending of 3 parameters (quantity on x-axis; quality, distinguished by colors ; temperature, distinguished by a gradient of color).
(color is a combination of temperature and quality to distinguish them on my ggplot, but every variable exists independently and in combination).
I want to make a piecewise regression for each curve to compare breaking points of these curves, but with my code, I only get one .
Here is the code for that :
fitJ4 = lm(growth_rate ~ quantity, data = donnees_tot_g_J4)
segmented.fitJ4 = segmented(fitJ4, seg.Z = ~quantity)
fit=numeric(length(donnees_tot_g_J4$quantity))*NA
fit[complete.cases(rowSums(cbind(donnees_tot_g_J4$growth_rate,donnees_tot_g_J4$quantity)))] = broken.line(segmented.fitJ4)$fit
ggplot(donnees_tot_g_J4, aes(x=quantity, y=growth_rate, col=color))+
geom_point(size=3,aes(col=color))+
geom_line (aes (x = quantity, y = fit, color= color), alpha=0.2)+
scale_x_continuous(breaks=c(0.1,0.3,0.6,0.9,1.5), limits=c(0.1,1.5))+
scale_y_continuous(limits=c(0,0.8))+
scale_colour_manual(values=c("20S" = "aquamarine1","25S" = "aquamarine3","28S" = "aquamarine4","20Y" = "darkgoldenrod1","25Y" = "darkgoldenrod3", "28Y" = "darkgoldenrod4"))+
scale_fill_manual(values=c("20S" = "aquamarine1","25S" = "aquamarine3","28S" = "aquamarine4","20Y" = "darkgoldenrod1", "25Y" = "darkgoldenrod3","28Y" = "darkgoldenrod4"))+
theme_minimal()+
xlab("quantity") + ylab("perf")+
theme_grey(base_size = 22)
Here, I understood that the problem was that I only write "quantity" for the seg.z, but when I tried to write that :
segmented.fitJ4 = segmented(fitJ4, seg.Z = ~quantity+quality+temperature)
It doesn't work because "Length or names of Z and psi do not match". So, I know that I need to define the psi, but I don't know what to put in for the quality and temperature...
I hope it is clear. I give you some of my data to try. Thank you!
structure(list(name = c("J4_S03AC", "J4_S03BC", "J4_S03CC", "J4_S03DC",
"J4_S06BC", "J4_S06CC", "J4_S06DC", "J4_S09AC", "J4_S09BC", "J4_S09CC",
"J4_S09DC", "J4_Y03AC", "J4_Y03BC", "J4_Y03CC", "J4_Y06AC", "J4_Y06BC",
"J4_Y06CC", "J4_Y06DC", "J4_Y09AC", "J4_Y09BC", "J4_Y09CC", "J4_Y09DC",
"J4_S01AM", "J4_S01BM", "J4_S01CM", "J4_S01DM", "J4_S15AM", "J4_S15BM",
"J4_S15CM", "J4_Y01AM", "J4_Y01BM", "J4_Y01CM", "J4_Y01DM", "J4_Y15AM",
"J4_Y15BM", "J4_Y15CM", "J4_S01AC", "J4_S01CC", "J4_S01EC", "J4_S01FC",
"J4_S03BC", "J4_S03CC", "J4_S03DC", "J4_S03EC", "J4_S03FC", "J4_S06AC",
"J4_S06DC", "J4_S06EC", "J4_S06FC", "J4_S06KC", "J4_S06MC", "J4_S06NC",
"J4_S09AC", "J4_S09BC", "J4_S09CC", "J4_S09DC", "J4_Y01AC", "J4_Y01BC",
"J4_Y01CC", "J4_Y06AC", "J4_Y06BC", "J4_Y06CC", "J4_Y06DC", "J4_Y09AC",
"J4_Y09BC", "J4_Y09CC", "J4_Y09DC", "J4_S03AM", "J4_S03BM", "J4_S03CM",
"J4_S15AM", "J4_S15BM", "J4_S15CM", "J4_S15DM", "J4_Y03AM", "J4_Y03BM",
"J4_Y03CM", "J4_Y03DM", "J4_Y15AM", "J4_Y15BM", "J4_Y15CM", "J4_Y15DM",
"J4_Y15AC", "J4_Y15BC", "J4_Y15CC", "J4_Y15DC", "J4_Y15EC", "J4_Y15FC",
"J4_Y15GC", "J4_Y15HC", "J4_Y15IC", "J4_Y15JC", "J4_Y15KC", "J4_Y15LC",
"J4_Y15MC", "J4_Y15NC", "J4_Y15OC", "J4_Y15PC", "J4_Y15QC", "J4_Y15RC",
"J4_Y15SC", "J4_Y15TC", "J4_Y15UC", "J4_Y15VC", "J4_Y15WC", "J4_S01AAC",
"J4_S01AC", "J4_S01BC", "J4_S01CC", "J4_S06AC", "J4_S06BC", "J4_S06CC",
"J4_S06DC", "J4_S09AC", "J4_S09BC", "J4_S09CC", "J4_S09DC", "J4_Y01AC",
"J4_Y01CC", "J4_Y06AC", "J4_Y06BC", "J4_Y06CC", "J4_Y06DC", "J4_Y09AC",
"J4_Y09BC", "J4_Y09CC", "J4_Y09DC", "J4_S03AM", "J4_S03BM", "J4_S03CM",
"J4_S15AM", "J4_S15BM", "J4_S15CM", "J4_Y03AM", "J4_Y03BM", "J4_Y03CM",
"J4_Y03DM", "J4_Y15AM", "J4_Y15BM", "J4_Y15CM", "J4_Y15DM"),
day = c("J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4", "J4",
"J4", "J4"), quality = c("S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "S", "S", "S", "S", "S", "S", "S", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"S", "S", "S", "S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"S", "S", "S", "S", "S", "S", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y"), quantity = c(0.3, 0.3, 0.3, 0.3, 0.6, 0.6, 0.6,
0.9, 0.9, 0.9, 0.9, 0.3, 0.3, 0.3, 0.6, 0.6, 0.6, 0.6, 0.9,
0.9, 0.9, 0.9, 0.1, 0.1, 0.1, 0.1, 1.5, 1.5, 1.5, 0.1, 0.1,
0.1, 0.1, 1.5, 1.5, 1.5, 0.1, 0.1, 0.1, 0.1, 0.3, 0.3, 0.3,
0.3, 0.3, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9,
0.9, 0.1, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9, 0.9,
0.3, 0.3, 0.3, 1.5, 1.5, 1.5, 1.5, 0.3, 0.3, 0.3, 0.3, 1.5,
1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 1.5, 0.1, 0.1, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9,
0.9, 0.9, 0.1, 0.1, 0.6, 0.6, 0.6, 0.6, 0.9, 0.9, 0.9, 0.9,
0.3, 0.3, 0.3, 1.5, 1.5, 1.5, 0.3, 0.3, 0.3, 0.3, 1.5, 1.5,
1.5, 1.5), qual_quant = c("S03", "S03", "S03", "S03", "S06",
"S06", "S06", "S09", "S09", "S09", "S09", "Y03", "Y03", "Y03",
"Y06", "Y06", "Y06", "Y06", "Y09", "Y09", "Y09", "Y09", "S01",
"S01", "S01", "S01", "S15", "S15", "S15", "Y01", "Y01", "Y01",
"Y01", "Y15", "Y15", "Y15", "S01", "S01", "S01", "S01", "S03",
"S03", "S03", "S03", "S03", "S06", "S06", "S06", "S06", "S06",
"S06", "S06", "S09", "S09", "S09", "S09", "Y01", "Y01", "Y01",
"Y06", "Y06", "Y06", "Y06", "Y09", "Y09", "Y09", "Y09", "S03",
"S03", "S03", "S15", "S15", "S15", "S15", "Y03", "Y03", "Y03",
"Y03", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15", "Y15",
"Y15", "S01", "S01", "S01", "S01", "S06", "S06", "S06", "S06",
"S09", "S09", "S09", "S09", "Y01", "Y01", "Y06", "Y06", "Y06",
"Y06", "Y09", "Y09", "Y09", "Y09", "S03", "S03", "S03", "S15",
"S15", "S15", "Y03", "Y03", "Y03", "Y03", "Y15", "Y15", "Y15",
"Y15"), temperature = c(20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25), time = c(105,
105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105,
105, 105, 105, 105, 105, 105, 105, 105, 105, 112, 112, 112,
112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 101,
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101,
101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102,
102, 102, 102, 102, 102, 102, 107.5, 107.5, 107.5, 107.5,
107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5, 107.5,
107.5, 107.5, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108,
108, 108, 109, 104, 104, 104, 104, 104, 104, 104, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112,
112, 112), size = c(1681.616, 1769.388, 1640.182, 1749.236,
1772.734, 1751.192, 1538.992, 1771.498, 1712.612, 1648.414,
1964.914, 1693.468, 1990.782, 1660.296, 2125.122, 2181.51,
1936.8, 2047.176, 1886.724, 2262.896, 2267.56, 2078.214,
1523.922, 1452.24, 1621.674, 1479.096, 1625.708, 1826.186,
1675.434, 1591.866, 1574.092, 1583.592, 1595.3, 2199.116,
2334.592, 2098.526, 1344.366, 1291.962, 1304.811, 1128.264,
1286.151, 1358.421, 1396.671, 1353.076, 1505.565, 1297.17,
0, 1243.029, 1323.85, 1368.364, 1506.396, 0, 1663.735, 1632.28,
2115.303, 1921.46, 1407.825, 1248.956, 1402.668, 1910.167,
2106.313, 2016.868, 2043.982, 2188.033, 1983.409, 2326.42,
2082.324, 1506.581, 1501.196, 1370.99, 1870.489, 1941.425,
1942.186, 1827.395, 1713.149, 1996.395, 2011.623, 1798.349,
2171.226, 2252.35, 2071.279, 2485.113, 2215.084, 2380.252,
2337.003, 2384.119, 2371.255, 2437.183, 2355.166, 2384.359,
2256.135, 2413.416, 2251.848, 2326.666, 2463.171, 2419.457,
2418.257, 2409.51, 2399.095, 2415.043, 2474.581, 2529.032,
2401.006, 2392.16, 2462.465, 1336.588, 1184.353, 1239.748,
1460.383, 1635.684, 1724.768, 1754.185, 1737.566, 1753.176,
1959.044, 1701.156, 1770.752, 1336.1, 1372.907, 2175.332,
1997.874, 1896.37, 2269.925, 2213.498, 2192.229, 2041.227,
2474.221, 1628.119, 1662.795, 1578.399, 1911.944, 2045.866,
2190.013, 1690.419, 1672.256, 2133.311, 1772.034, 2423.857,
2492.047, 2703.446, 2459.673), weight = c(34, 53, 36, 34,
42, 46, 48, 63, 54, 51, 71, 65, 63, 61, 91, 93, 96, 84, 93,
137, 140, 125, 18, 26, 22, 21, 47, 52, 44, 20, 19, 19, 24,
155, 142, 136, 11, 16, 10, 10, 12, 12, 13, 16, 14, 12, 11,
12, 10, 10, 15, 25, 46, 35, 66, 46, 17, 15, 11, 71, 68, 68,
58, 115, 96, 114, 82, 20, 16, 15, 49, 73, 63, 60, 35, 39,
44, 27, 99, 94, 86, 168, 126, 134, 124, 130, 139, 183, 136,
143, 79, 129, 163, 148, 164, 186, 144, 155, 125, 133, 143,
161, 135, 142, 155, 11, 13, 12, 17, 39, 42, 42, 28, 37, 37,
20, 39, 14, 15, 70, 61, 53, 73, 107, 74, 77, 93, 28, 23,
24, 50, 42, 68, 31, 23, 38, 31, 119, 139, 162, 112), fecundity = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 6, 0, 0, 9, 6, 7, 7, 6, 7, 10, 7, 8, 5, 6, 10, 7, 8, 8,
7, 6, 8, 8, 9, 8, 8, 2, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 7, 8, 12, 6), metabolic_rate = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.346,
0.357, 0.3585, 0.512, 1.528, 1.927, 1.431, 0.225, 0.268,
0.471, 0.477, 3.334, 3.103, 3.384, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.382, 0.1435, 0.2405, 1.436, 2.014, 3.464, 2.742,
1.624, 1.909, 2.014, 1.392, 5.058, 4.439, 4.242, 7.193, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), time_maturity = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104,
104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139.52,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110.03, 110.03, 110.03, 110.03
), weight_maturity = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 142.231, 0, 0, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 142.231, 142.231, 142.231, 142.231, 142.231, 142.231,
142.231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA,
NA, NA), aire = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA,
NA, NA), jour = c(4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.375035, 4.375035, 4.375035, 4.375035, 4.375035, 4.375035,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.208367, 4.208367, 4.208367, 4.208367,
4.208367, 4.208367, 4.208367, 4.208367, 4.208367, 4.208367,
4.208367, 4.208367, 4.208367, 4.208367, 4.208367, 4.208367,
4.250034, 4.250034, 4.250034, 4.250034, 4.250034, 4.250034,
4.250034, 4.250034, 4.250034, 4.250034, 4.250034, 4.250034,
4.250034, 4.250034, 4.250034, 4.4792025, 4.4792025, 4.4792025,
4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025,
4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025, 4.4792025,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.500036,
4.500036, 4.500036, 4.500036, 4.500036, 4.500036, 4.541703,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.333368, 4.333368, 4.333368,
4.333368, 4.333368, 4.333368, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704, 4.666704,
4.666704, 4.666704, 4.666704, 4.666704, 4.666704), growth_rate = c(0.303800071834841,
0.405269291837872, 0.316864747623708, 0.303800071834841,
0.352098906853808, 0.37289228981091, 0.382620123855391, 0.444775904433979,
0.409541745203879, 0.396477069415012, 0.472100291701688,
0.451919285802152, 0.444775904433979, 0.437402052060633,
0.52882661034269, 0.533795710392497, 0.541052497667245, 0.510531280665662,
0.533795710392497, 0.622339329512085, 0.627290489167077,
0.601386996896272, 0.148530350448613, 0.22732788723803, 0.191530869757777,
0.181562374726831, 0.354194957377592, 0.375858237686643,
0.34006122020639, 0.17110742318728, 0.160116090892034, 0.160116090892034,
0.210176015665816, 0.609895236463043, 0.591124373918946,
0.58187326824239, 0.0378568479840844, 0.126892202895244,
0.0152090656484838, 0.0152090656484838, 0.0585326533474545,
0.0585326533474545, 0.0775525505364441, 0.126892202895244,
0.0951622248242906, 0.0585326533474545, 0.0378568479840844,
0.0585326533474545, 0.0152090656484838, 0.0152090656484838,
0.111556439370444, 0.232939774941222, 0.374129156018743,
0.309825356331568, 0.459072793066665, 0.374129156018743,
0.139912664472351, 0.110462748788381, 0.037485702415613,
0.476255040318073, 0.466096963496756, 0.466096963496756,
0.428670275938566, 0.589725250514494, 0.547235252341441,
0.587670283432171, 0.510146385710354, 0.169037347727828,
0.119219651092275, 0.104811166292231, 0.369092608582107,
0.458090402952369, 0.425199566970549, 0.414306966296783,
0.293973826472744, 0.318132945119474, 0.34506362927332, 0.236036906790036,
0.526107066406678, 0.514536911258616, 0.494679026731963,
0.644173648408422, 0.577262524996099, 0.590941947338372,
0.573706922030602, 0.584207479104045, 0.599082800695941,
0.660196138391623, 0.594234162286239, 0.605387349621597,
0.473522453920122, 0.582491482588926, 0.634477243690635,
0.613024542636099, 0.635836394376859, 0.66380955856688, 0.606935930414305,
0.623293981181731, 0.575491834795137, 0.589277366937424,
0.605387349621597, 0.63173374656699, 0.592594151562409, 0.603827901506372,
0.623293981181731, 0.0350783637283718, 0.0585003710820993,
0.0400291247735614, 0.120406949659012, 0.312024255660586,
0.329125958574745, 0.329125958574745, 0.235557836017841,
0.299875798966989, 0.299875798966989, 0.157911017972106,
0.312024255660586, 0.0756020739962579, 0.091523378507426,
0.44700785177329, 0.415249326331636, 0.382807597260579, 0.456691820247472,
0.544930205105051, 0.459831560988572, 0.469002332695363,
0.512569846762844, 0.218732276302281, 0.176580407607362,
0.185700252024063, 0.342978102747015, 0.305616961533692,
0.408867154152023, 0.240542679171189, 0.176580407607362,
0.284170677698895, 0.240542679171189, 0.528783863661652,
0.562072977352849, 0.594884658166908, 0.515792977199508),
color = c("20S", "20S", "20S", "20S", "20S", "20S", "20S",
"20S", "20S", "20S", "20S", "20Y", "20Y", "20Y", "20Y", "20Y",
"20Y", "20Y", "20Y", "20Y", "20Y", "20Y", "20S", "20S", "20S",
"20S", "20S", "20S", "20S", "20Y", "20Y", "20Y", "20Y", "20Y",
"20Y", "20Y", "28S", "28S", "28S", "28S", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28S", "28S", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28S", "28S", "28S",
"28S", "28S", "28S", "28S", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y",
"28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28Y", "28S",
"25S", "25S", "25S", "25S", "25S", "25S", "25S", "25S", "25S",
"25S", "25S", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y",
"25Y", "25Y", "25Y", "25S", "25S", "25S", "25S", "25S", "25S",
"25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y", "25Y")), row.names = c(NA,
-141L), class = c("tbl_df", "tbl", "data.frame"))

group_by (months and years) and summarise_if does not leave single months with the mean values

I am sorry for being vague in my title, but I have no idea how I need to address this.
I have a dataframe of government responses per month per year and I would like to get the average of each column per month.
Now I think that I have the right syntax only my results don't leave me with one single month per year but multiple, and I for the life of me cannot grasp what I am doing wrong.
The code I am running is:
test <- ds %>%
group_by(month, year, C1_calculated, C2_calculated, C3_calculated, C4_calculated, C5_calculated, C6_calculated, C7_calculated, C8_calculated, E1_calculated, E2_calculated) %>%
summarise_if(is.integer, mean) %>%
ungroup
And the results are as follows:
test2 <- structure(list(month = c(1, 1, 1, 2, 2, 2), year = c(2020, 2021,
2021, 2020, 2021, 2021), C1_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 7.40740740740741, 11.1111111111111), C2_calculated = c(0,
11.1111111111111, 11.1111111111111, 0, 11.1111111111111, 11.1111111111111
), C3_calculated = c(0, 11.1111111111111, 11.1111111111111, 0,
11.1111111111111, 11.1111111111111), C4_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111), C5_calculated = c(0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556
), C6_calculated = c(0, 3.7037037037037, 7.40740740740741, 0,
7.40740740740741, 7.40740740740741), C7_calculated = c(0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556), C8_calculated = c(0,
9.72222222222222, 9.72222222222222, 0, 9.72222222222222, 9.72222222222222
), E1_calculated = c(0, 50, 50, 0, 50, 50), E2_calculated = c(0,
37.5, 37.5, 0, 37.5, 37.5)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
As you can see I have multiple instances of the same month per year which doesn't really help.
Here is a random sample of 20 rows of my data set:
ds <- structure(list(C1_calculated = c(3.7037037037037, 7.40740740740741,
3.7037037037037, 7.40740740740741, 3.7037037037037, 3.7037037037037,
11.1111111111111, 11.1111111111111, 3.7037037037037, 7.40740740740741,
3.7037037037037, 3.7037037037037, 3.7037037037037, 3.7037037037037,
11.1111111111111, 7.40740740740741, 3.7037037037037, 7.40740740740741,
0, 7.40740740740741), C2_calculated = c(7.40740740740741, 7.40740740740741,
7.40740740740741, 11.1111111111111, 7.40740740740741, 7.40740740740741,
7.40740740740741, 7.40740740740741, 7.40740740740741, 7.40740740740741,
7.40740740740741, 3.7037037037037, 7.40740740740741, 7.40740740740741,
7.40740740740741, 7.40740740740741, 7.40740740740741, 11.1111111111111,
0, 7.40740740740741), C3_calculated = c(0, 11.1111111111111,
11.1111111111111, 11.1111111111111, 5.55555555555556, 2.77777777777778,
11.1111111111111, 11.1111111111111, 5.55555555555556, 11.1111111111111,
11.1111111111111, 5.55555555555556, 0, 11.1111111111111, 11.1111111111111,
11.1111111111111, 11.1111111111111, 11.1111111111111, 0, 11.1111111111111
), C4_calculated = c(5.55555555555556, 11.1111111111111, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111, 11.1111111111111,
0, 11.1111111111111, 11.1111111111111, 0, 5.55555555555556, 11.1111111111111,
11.1111111111111, 8.33333333333333, 11.1111111111111, 11.1111111111111,
0, 8.33333333333333), C5_calculated = c(0, 5.55555555555556,
5.55555555555556, 5.55555555555556, 5.55555555555556, 0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556, 0, 0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556,
0, 0), C6_calculated = c(3.7037037037037, 3.7037037037037, 3.7037037037037,
7.40740740740741, 0, 3.7037037037037, 7.40740740740741, 7.40740740740741,
0, 7.40740740740741, 3.7037037037037, 0, 3.7037037037037, 3.7037037037037,
7.40740740740741, 3.7037037037037, 7.40740740740741, 7.40740740740741,
0, 3.7037037037037), C7_calculated = c(0, 5.55555555555556, 5.55555555555556,
5.55555555555556, 0, 0, 5.55555555555556, 5.55555555555556, 0,
5.55555555555556, 5.55555555555556, 0, 0, 5.55555555555556, 5.55555555555556,
5.55555555555556, 5.55555555555556, 5.55555555555556, 0, 5.55555555555556
), C8_calculated = c(8.33333333333333, 8.33333333333333, 9.72222222222222,
9.72222222222222, 9.72222222222222, 8.33333333333333, 8.33333333333333,
8.33333333333333, 9.72222222222222, 9.72222222222222, 9.72222222222222,
9.72222222222222, 8.33333333333333, 9.72222222222222, 8.33333333333333,
8.33333333333333, 9.72222222222222, 9.72222222222222, 0, 8.33333333333333
), E1_calculated = c(50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 0, 50), E2_calculated = c(62.5,
62.5, 37.5, 37.5, 12.5, 37.5, 62.5, 62.5, 37.5, 37.5, 37.5, 37.5,
62.5, 37.5, 62.5, 62.5, 37.5, 37.5, 0, 62.5), month = c(8, 5,
5, 2, 8, 9, 5, 4, 7, 4, 6, 7, 8, 6, 5, 6, 4, 2, 1, 6), year = c(2020,
2020, 2021, 2021, 2021, 2020, 2020, 2020, 2021, 2021, 2021, 2021,
2020, 2021, 2020, 2020, 2021, 2021, 2020, 2020)), row.names = c(227L,
145L, 488L, 423L, 586L, 272L, 122L, 104L, 565L, 464L, 520L, 555L,
224L, 539L, 124L, 157L, 482L, 417L, 14L, 162L), class = "data.frame")
Thanks in advance for all your help!
Yours data:
test2 <- structure(list(month = c(1, 1, 1, 2, 2, 2), year = c(2020, 2021,
2021, 2020, 2021, 2021), C1_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 7.40740740740741, 11.1111111111111), C2_calculated = c(0,
11.1111111111111, 11.1111111111111, 0, 11.1111111111111, 11.1111111111111
), C3_calculated = c(0, 11.1111111111111, 11.1111111111111, 0,
11.1111111111111, 11.1111111111111), C4_calculated = c(0, 11.1111111111111,
11.1111111111111, 0, 11.1111111111111, 11.1111111111111), C5_calculated = c(0,
5.55555555555556, 5.55555555555556, 0, 5.55555555555556, 5.55555555555556
), C6_calculated = c(0, 3.7037037037037, 7.40740740740741, 0,
7.40740740740741, 7.40740740740741), C7_calculated = c(0, 5.55555555555556,
5.55555555555556, 0, 5.55555555555556, 5.55555555555556), C8_calculated = c(0,
9.72222222222222, 9.72222222222222, 0, 9.72222222222222, 9.72222222222222
), E1_calculated = c(0, 50, 50, 0, 50, 50), E2_calculated = c(0,
37.5, 37.5, 0, 37.5, 37.5)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Summarise:
library(tidyverse)
test2 %>%
group_by(month, year) %>%
summarise_at(vars(starts_with("C")), mean)
output
# A tibble: 4 x 10
# Groups: month [2]
month year C1_calculated C2_calculated C3_calculated C4_calculated C5_calculated C6_calculated C7_calculated C8_calculated
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 2020 0 0 0 0 0 0 0 0
2 1 2021 11.1 11.1 11.1 11.1 5.56 5.56 5.56 9.72
3 2 2020 0 0 0 0 0 0 0 0
4 2 2021 9.26 11.1 11.1 11.1 5.56 7.41 5.56 9.72

Create column based on condition with values from other column

I am trying to create a new variable based on conditions and to take the value of other variables when the condition is met.
Basically, my data.frame looks like this:
df <- data.frame (
party=c("A", "B", "C", "A", "B", "C", "A", "B", "C", "D",
"E", "F", "G", "H", "I","E", "F", "G", "H", "I", "E", "F", "G", "H", "I",
"J", "K", "L", "J", "K", "L", "J", "K", "L"),
edate = c(1991, 1991, 1991, 1995, 1995, 1995, 1998, 1998, 1998, 1998,
2000, 2000, 2000, 2000, 2000, 2005, 2005, 2005, 2005, 2005, 2010, 2010, 2010, 2010, 2010,
1999, 1999, 1999, 2001, 2001, 2001, 2006, 2006, 2006),
RRP = c(1,0,0,1,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,
0,0,0,0,0,0, 0,0,0),
absseats = c(0, 20, 30, 5, 25, 20, 0, 10, 28, 12,
100, 50, 50, 25, 0, 120, 30, 75, 0, 15, 90, 60, 70, 5, 15,
10, 20, 40, 30, 30, 10, 50, 10, 10),
country=c(11,11,11,11,11,11,11,11,11,11,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
43, 43, 43, 43, 43, 43, 43, 43, 43),
treat = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0,0,0,0,0, 1,1,1,1,1,
0, 0, 0,0,0,0,0,0,0))
What I want is a variable that takes the value of edate for which treat changes from 0 to 1 in each observation. And I want to assign value 0 when, for a specific "party", "treat" is always == 0 in the dataset.
Basically I want this:
first.treat <- c( 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998,
2010, 2010, 2010, 2010, 2010,2010, 2010, 2010, 2010, 2010,2010, 2010, 2010, 2010, 2010,
0, 0, 0,0,0,0,0,0,0)
ideal.df<- cbind(df,first.treat)
I tried different things, like different iteration of:
df%>%
group_by(party)%>%
arrange(edate)%>%
mutate(first.treat = if_else(nrow = 1, edate, 0))
Here's a dput of my actual dataframe:
dput(df3[1:100,])
structure(list(country = c(61, 61, 52, 52, 61, 61, 61, 52, 52,
52, 61, 61, 52, 52, 61, 61, 52, 52, 61, 61, 52, 52, 52, 61, 61,
11, 11, 11, 11, 11, 61, 61, 14, 14, 14, 14, 14, 14, 52, 52, 52,
62, 62, 62, 62, 51, 51, 51, 12, 12, 12, 12, 12, 12, 23, 23, 23,
23, 13, 13, 13, 13, 13, 13, 13, 21, 21, 21, 22, 22, 22, 22, 22,
22, 22, 32, 32, 32, 32, 32, 15, 15, 15, 15, 171, 171, 63, 63,
63, 31, 31, 31, 31, 31, 31, 64, 64, 73, 43, 43), countryname = c("United States",
"United States", "Northern Ireland", "Northern Ireland", "United States",
"United States", "United States", "Northern Ireland", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "Northern Ireland", "United States", "United States",
"Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "United States",
"United States", "Finland", "Finland", "Finland", "Finland",
"Finland", "Finland", "Northern Ireland", "Northern Ireland",
"Northern Ireland", "Canada", "Canada", "Canada", "Canada", "United Kingdom",
"United Kingdom", "United Kingdom", "Norway", "Norway", "Norway",
"Norway", "Norway", "Norway", "Luxembourg", "Luxembourg", "Luxembourg",
"Luxembourg", "Denmark", "Denmark", "Denmark", "Denmark", "Denmark",
"Denmark", "Denmark", "Belgium", "Belgium", "Belgium", "Netherlands",
"Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands",
"Netherlands", "Italy", "Italy", "Italy", "Italy", "Italy", "Iceland",
"Iceland", "Iceland", "Iceland", "Mexico", "Mexico", "Australia",
"Australia", "Australia", "France", "France", "France", "France",
"France", "France", "New Zealand", "New Zealand", "Sri Lanka",
"Switzerland", "Switzerland"), oecdmember = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), eumember = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), edate = structure(c(-17957,
-17957, -17897, -17897, -16494, -16494, -16494, -16436, -16436,
-16436, -15031, -15031, -14975, -14975, -13568, -13568, -13514,
-13514, -12112, -12112, -11657, -11657, -11657, -10649, -10649,
-9237, -9237, -9237, -9237, -9237, -9186, -9186, -9055, -9055,
-9055, -9055, -9055, -9055, -8980, -8980, -8980, -8970, -8970,
-8970, -8970, -8946, -8946, -8946, -8851, -8851, -8851, -8851,
-8851, -8851, -8838, -8838, -8838, -8838, -8829, -8829, -8829,
-8829, -8829, -8829, -8829, -8719, -8719, -8719, -8630, -8630,
-8630, -8630, -8630, -8630, -8630, -8614, -8614, -8614, -8614,
-8614, -8586, -8586, -8586, -8586, -8579, -8579, -8496, -8496,
-8496, -8453, -8453, -8453, -8453, -8453, -8453, -8436, -8436,
-8401, -8103, -8103), class = "Date"), date = c(192011, 192011,
192101, 192101, 192411, 192411, 192411, 192501, 192501, 192501,
192811, 192811, 192901, 192901, 193211, 193211, 193301, 193301,
193611, 193611, 193802, 193802, 193802, 194011, 194011, 194409,
194409, 194409, 194409, 194409, 194411, 194411, 194503, 194503,
194503, 194503, 194503, 194503, 194506, 194506, 194506, 194506,
194506, 194506, 194506, 194507, 194507, 194507, 194510, 194510,
194510, 194510, 194510, 194510, 194510, 194510, 194510, 194510,
194510, 194510, 194510, 194510, 194510, 194510, 194510, 194602,
194602, 194602, 194605, 194605, 194605, 194605, 194605, 194605,
194605, 194606, 194606, 194606, 194606, 194606, 194606, 194606,
194606, 194606, 194607, 194607, 194609, 194609, 194609, 194611,
194611, 194611, 194611, 194611, 194611, 194611, 194611, 194701,
194710, 194710), party = c(61320, 61620, 52620, 52710, 61320,
61620, 61621, 52320, 52620, 52710, 61320, 61620, 52620, 52710,
61320, 61620, 52320, 52620, 61320, 61620, 52320, 52620, 52710,
61320, 61620, 11220, 11320, 11420, 11620, 11810, 61320, 61620,
14221, 14320, 14420, 14620, 14810, 14901, 52320, 52620, 52710,
62320, 62420, 62620, 62951, 51320, 51420, 51620, 12220, 12320,
12420, 12520, 12620, 12810, 23220, 23320, 23420, 23520, 13220,
13320, 13410, 13420, 13620, 13952, 13953, 21320, 21420, 21520,
22210, 22320, 22420, 22522, 22523, 22525, 22952, 32220, 32320,
32410, 32420, 32520, 15220, 15320, 15620, 15810, 171301, 171601,
63320, 63620, 63810, 31220, 31320, 31421, 31521, 31621, 31622,
64320, 64620, 73330, 43320, 43321), partyname = c("Democratic Party",
"Republican Party", "Unionist Party", "Nationalist Party", "Democratic Party",
"Republican Party", "La Follette Progressive Party", "Northern Ireland Labour Party",
"Unionist Party", "Nationalist Party", "Democratic Party", "Republican Party",
"Unionist Party", "Nationalist Party", "Democratic Party", "Republican Party",
"Northern Ireland Labour Party", "Unionist Party", "Democratic Party",
"Republican Party", "Northern Ireland Labour Party", "Unionist Party",
"Nationalist Party", "Democratic Party", "Republican Party",
"Communist Party of Sweden", "Social Democratic Labour Party",
"People’s Party", "Right Party", "Agrarian Party", "Democratic Party",
"Republican Party", "Finnish People’s Democratic Union", "Finnish Social Democrats",
"National Progressive Party", "National Coalition", "Agrarian Union",
"Swedish People’s Party", "Northern Ireland Labour Party",
"Unionist Party", "Nationalist Party", "Cooperative Commonwealth Federation",
"Liberal Party of Canada", "Progressive Conservative Party",
"Social Credit", "Labour Party", "Liberal Party", "Conservative Party",
"Norwegian Communist Party", "Norwegian Labour Party", "Liberal Party",
"Christian People’s Party", "Conservative Party", "Farmers’ Party",
"Communist Party of Luxembourg", "Socialist Workers’ Party of Luxembourg",
"Patriotic and Democratic Group", "Christian Social People’s Party",
"Danish Communist Party", "Social Democratic Party", "Danish Social-Liberal Party",
"Liberals", "Conservative People’s Party", "Justice Party",
"Danish Union", "Belgian Socialist Party", "Liberal Party", "Francophone Christian Social Party and Flemish Christian People’s Party",
"Communist Party of the Netherlands", "Labour Party", "Freedom Party",
"Catholic People’s Party", "Anti-Revolutionary Party", "Christian Historical Union",
"Reformed Political Party", "Italian Communist Party", "Italian Socialist Party",
"Italian Republican Party", "Italian Liberal Party", "Christian Democrats",
"United Socialist Party", "Social Democratic Party", "Independence Party",
"Progressive Party", "Institutional Revolutionary Party", "National Action Party",
"Australian Labor Party", "Liberal Party of Australia", "Country Party",
"French Communist Party", "French Section of the Workers' International",
"Radical Socialist Party", "Popular Republican Movement", "Rally for the French People - Gaullists",
"Republican Party of Liberty - Conservatives", "New Zealand Labour Party",
"New Zealand National Party", "United National Party", "Social Democratic Party of Switzerland",
"Independents’ Alliance"), partyabbrev = c("Democrats", "Republicans",
"UP", "NP", "Democrats", "Republicans", "", "NILP", "UP", "NP",
"Democrats", "Republicans", "UP", "NP", "Democrats", "Republicans",
"NILP", "UP", "Democrats", "Republicans", "NILP", "UP", "NP",
"Democrats", "Republicans", "SKP", "SAP", "FP", "", "", "Democrats",
"Republicans", "SKDL", "SSDP", "KE", "KK", "Maal", "RKP/SFP",
"NILP", "UP", "NP", "CCF", "LP", "PCP", "Socred", "Labour", "",
"Conservatives", "NKP", "DnA", "V", "KrF", "H", "", "KPL/PCL",
"LSAP/POSL", "", "CSV/PCS", "DKP", "SD", "RV", "V", "KF", "RF",
"DS", "BSP/PSB", "LP/PL", "PSC/CVP", "CPN", "PvdA", "PvdV", "KVP",
"ARP", "CHU", "SGP", "PCI", "PSI", "PRI", "PLI", "DC", "So",
"A", "Sj", "F", "PRI", "PAN", "ALP", "LPA", "CP", "PCF", "SIFO",
"RRRS", "MRP", "RPF", "PRL", "Labour", "National", "UNP", "SPS/PSS",
"LdU/AdI"), parfam = c(30, 60, 60, 70, 30, 60, 60, 30, 60, 70,
30, 60, 60, 70, 30, 60, 30, 60, 30, 60, 30, 60, 70, 30, 60, 20,
30, 40, 60, 80, 30, 60, 20, 30, 40, 60, 80, 90, 30, 60, 70, 30,
40, 60, 95, 30, 40, 60, 20, 30, 40, 50, 60, 80, 20, 30, 40, 50,
20, 30, 40, 40, 60, 95, 95, 30, 40, 50, 20, 30, 40, 50, 50, 50,
95, 20, 30, 40, 40, 50, 20, 30, 60, 80, 30, 60, 30, 60, 80, 20,
30, 40, 50, 60, 60, 30, 60, 60, 30, 30), coderid = c(101, 101,
120, 120, 101, 101, 101, 120, 120, 120, 101, 101, 120, 120, 101,
101, 120, 120, 101, 101, 120, 120, 120, 101, 101, 117, 117, 117,
117, 117, 101, 101, 209, 208, 998, 208, 209, 209, 120, 120, 120,
105, 105, 105, 105, 102, 102, 102, 116, 116, 116, 116, 116, 116,
998, 104, 998, 104, 107, 107, 107, 107, 107, 107, 107, 104, 104,
104, 326, 113, 113, 113, 113, 113, 326, 111, 111, 111, 111, 111,
212, 212, 212, 212, 262, 262, 104, 104, 104, 106, 106, 106, 106,
998, 106, 104, 104, 109, 229, 998), manual = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 998, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 998, 0, 998, 0, 999, 999, 999, 999, 999,
999, 999, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 0, 0, 999, 999, 999, 999, 998, 999, 0, 0, 999,
1, 998), coderyear = c(1981, 1981, 1980, 1980, 1981, 1981, 1981,
1980, 1980, 1980, 1981, 1981, 1980, 1980, 1981, 1981, 1980, 1980,
1981, 1981, 1980, 1980, 1980, 1981, 1981, 1983, 1983, 1983, 1983,
1983, 1981, 1981, 1995, 1993, NA, 1993, 1995, 1995, 1980, 1980,
1980, 1981, 1981, 1981, 1981, 1982, 1982, 1982, 1990, 1990, 1990,
1990, 1990, 1990, NA, 1983, NA, 1983, 1983, 1983, 1983, 1983,
1983, 1983, 1983, 1982, 1982, 1982, 2016, 1982, 1982, 1982, 1982,
1982, 2016, 1980, 1980, 1980, 1980, 1980, 1991, 1991, 1991, 1991,
1998, 1998, 1981, 1981, 1981, 1982, 1982, 1982, 1982, NA, 1982,
1982, 1982, 1980, 1991, NA), testresult = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.669, 0.341, NA,
0.341, 0.669, 0.669, NA, NA, NA, NA, NA, NA, NA, 0.961, 0.961,
0.961, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.94, NA, NA, NA, NA, NA, 0.94, NA, NA,
NA, NA, NA, 0.975, 0.975, 0.975, 0.975, 0.88, 0.88, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.412, NA), testeditsim = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.471, NA, NA, NA, NA, NA, 0.471, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), voteest = c(0, 0, NA, NA, 0, 0, 0, NA, NA,
NA, 0, 0, NA, NA, 0, 0, NA, NA, 0, 0, NA, NA, NA, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, NA, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0), presvote = c(34.2000007629395,
60.2999992370605, NA, NA, 28.7999992370606, 54.0999984741211,
16.6000003814697, NA, NA, NA, 40.7999992370605, 58.2000007629395,
NA, NA, 57.4000015258789, 39.5999984741211, NA, NA, 60.7999992370605,
36.5, NA, NA, NA, 54.7000007629395, 44.7999992370605, NA, NA,
NA, NA, NA, 53.4000015258789, 45.9000015258789, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
77.869839, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), absseat = c(131, 303, NA, NA, 184, 247, 0, NA, NA, NA,
166, 268, NA, NA, 313, 117, NA, NA, 334, 89, NA, NA, NA, 268,
162, 15, 115, 26, 39, 35, 243, 190, 49, 50, 9, 28, 49, 15, NA,
NA, NA, 28, 125, 67, 13, 393, 12, 199, 11, 76, 20, 8, 25, 10,
5, 11, 9, 25, 18, 48, 11, 38, 26, 3, 4, 69, 17, 92, 10, 29, 6,
32, 13, 8, 2, 104, 115, 25, 41, 207, 10, 9, 20, 13, 141, 4, 43,
17, 12, 166, 90, 55, 158, 5, 70, 42, 38, NA, 48, 8), totseats = c(435,
435, NA, NA, 435, 435, 435, NA, NA, NA, 435, 435, NA, NA, 435,
435, NA, NA, 435, 435, NA, NA, NA, 435, 435, 230, 230, 230, 230,
230, 435, 435, 200, 200, 200, 200, 200, 200, NA, NA, NA, 245,
245, 245, 245, 640, 640, 640, 150, 150, 150, 150, 150, 150, 51,
51, 51, 51, 148, 148, 148, 148, 148, 148, 148, 202, 202, 202,
100, 100, 100, 100, 100, 100, 100, 556, 556, 556, 556, 556, 52,
52, 52, 52, 147, 147, 74, 74, 74, 544, 544, 544, 544, 544, 544,
80, 80, NA, 194, 194), progtype = 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,
1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 1, 3, 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, 1, 1, 1, 1, 1, 1,
3, 1, 1, 1, 1, 1, 3), datasetorigin = c(30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 41, 41, 41, 41, 41, 30, 30, 10, 10, 10, 10, 10, 10,
30, 30, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 110,
10, 10, 10, 10, 10, 110, 10, 10, 10, 10, 10, 10, 10, 10, 10,
30, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 30, 10, 10
), corpusversion = c("", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "2020-1", "", "", "",
"", "", "2020-1", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", ""), total = c(242,
274, 26, 61, 272, 262, 139, 24, 54, 130, 275, 330, 58, 83, 93,
390, 15, 68, 147, 193, 25, 129, 46, 258, 194, 52, 90, 63, 57,
21, 109, 249, 205, 38, 139, 68, 91, 168, 144, 118, 34, 432, 75,
308, 93, 567, 250, 326, 156, 501, 153, 47, 183, 94, 49, 107,
118, 117, 128, 48, 64, 61, 133, 87, 136, 119, 52, 210, 125, 81,
72, 151, 67, 64, 50, 84, 264, 126, 32, 48, 128, 198, 173, 52,
141, 60, 277, 320, 74, 164, 464, 187, 287, 116, 365, 115, 734,
120, 617, 112), rile = c(-9.917, 13.869, 22.8, 4, -8.824, -4.962,
-28.058, -33.4, 3.9, -15.5, -13.091, -4.848, 26.7, -13.3, -1.075,
-7.179, -45.6, 4.6, -15.646, 1.036, 0, -13.3, 11.1, -15.891,
14.948, 9.6, -37.8, 9.5, 28, 23.81, -10.092, -0.803, -42.927,
78.947, 21.583, 4.412, 3.297, 4.167, 15.2, -25.7, -64.7, -36.1,
7.1, -8.8, -20.4, -31.3, -6.8, 15.2, -0.5, -32.8, -14.3, 12.6,
15.7, 33, -42.857, -43.925, -2.542, 29.06, -21.2, -23, -15.7,
16.4, -0.2, 6.8, -13.971, -13.5, 15.3, 30.1, -34.4, -19.8, 19.5,
3.9, 21, 22.1, 56, -22.619, -31.818, -5.556, -9.375, 29.167,
-1.7, -36.4, -9.4, 57.8, -18.44, -10, -16.4, 12.5, 41.9, 18.902,
-14.44, 37.433, 48.432, 25, 25.479, -32.174, -3.542, -15.1, 10.049,
-5.357), planeco = c(7.438, 1.825, 0, 0, 3.309, 2.29, 13.669,
0, 0, 0, 6.909, 3.636, 2, 5.7, 12.903, 4.103, 12.9, 0, 11.565,
4.663, 0, 7.1, 0, 8.915, 4.639, 1.9, 3.3, 3.2, 1.8, 0, 3.67,
3.614, 5.854, 0, 5.755, 5.882, 3.297, 4.167, 0, 7, 0, 7, 2.8,
2.7, 7.9, 13.9, 8.8, 4.8, 5.1, 14.4, 4.6, 2.1, 2.1, 0, 12.245,
11.215, 2.542, 0.855, 2.4, 12.5, 6.3, 9.8, 1.6, 4.6, 7.353, 5.9,
0, 8.5, 3.2, 4.9, 4.2, 3.4, 1.5, 1.6, 0, 3.571, 1.894, 0, 12.5,
0, 4, 11.1, 2.9, 3.8, 6.383, 1.667, 6.2, 0.9, 0, 3.049, 7.543,
2.139, 0, 1.724, 2.466, 15.652, 6.676, 10.1, 6.159, 9.821), markeco = c(10.331,
9.124, 0, 0, 2.574, 12.595, 2.158, 0, 0, 0, 9.818, 9.091, 0,
0, 9.677, 7.949, 0, 0, 11.565, 11.917, 0, 2, 0, 2.713, 15.464,
1.9, 2.2, 6.4, 22.8, 19.048, 5.505, 6.024, 0, 0, 8.633, 1.471,
9.89, 7.738, 0, 2, 0, 0.7, 9.9, 4, 8, 1.7, 10, 13.4, 0, 0.6,
1.9, 4.2, 11.4, 1.1, 0, 0, 6.78, 5.128, 0, 0, 1.6, 9.8, 2.3,
3.4, 3.676, 1.6, 15.3, 10, 2.4, 2.5, 12.5, 4.7, 7.5, 1.6, 0,
0, 0.758, 0, 15.625, 0, 0.8, 1, 2.9, 3.9, 0.709, 8.333, 1.1,
17.2, 28.4, 0, 1.94, 8.556, 0, 1.724, 9.863, 3.478, 7.357, 0.8,
0.324, 7.143), welfare = c(2.066, 5.839, 4, 0, 8.088, 1.908,
9.353, 33.3, 18.7, 31.7, 6.909, 2.121, 4.9, 19, 3.226, 5.897,
26.7, 15.3, 10.884, 6.218, 4, 16.2, 2, 10.078, 3.093, 0, 33.4,
14.3, 10.6, 0, 6.422, 4.418, 0.976, 0, 15.827, 4.412, 5.495,
14.881, 7.1, 27.7, 58.8, 24.8, 9.9, 18.5, 22.8, 14.3, 9.2, 5.5,
0.6, 14.8, 13.1, 19.2, 9.9, 9.6, 8.163, 16.822, 28.814, 8.547,
15.6, 10.4, 12.5, 1.6, 0.8, 3.4, 12.5, 5.9, 5.8, 10, 14.4, 9.9,
15.3, 15.2, 12, 9.6, 10, 8.333, 7.955, 1.587, 3.125, 8.333, 10.2,
14.2, 2.3, 0, 12.057, 6.667, 14.2, 2.8, 0, 10.366, 9.698, 3.209,
1.394, 0.862, 4.11, 16.522, 9.537, 7.5, 6.969, 15.179), intpeace = c(0.826,
3.65, 7.9, 2, 3.676, 5.725, 4.317, 0, 3.9, 1, 3.636, 3.03, 2,
0, 4.301, 4.103, 0, 1.2, 2.721, 1.554, 0, 4, 2, 1.55, 4.639,
1.9, 5.6, 1.6, 0, 4.762, 8.257, 2.811, 13.659, 0, 0, 5.882, 0,
0.595, 0, 1, 0, 0, 0, 0.3, 5.7, 5.5, 4, 0.9, 3.2, 0.4, 0, 0,
0.5, 0, 16.327, 3.738, 2.542, 6.838, 1.6, 4.2, 4.7, 1.6, 2.3,
3.5, 8.088, 1.7, 3.8, 0, 4, 0, 0, 0.7, 0, 0, 2, 3.571, 3.03,
0, 6.25, 0, 3.9, 2, 3.5, 0, 1.418, 1.667, 3.7, 0.3, 0, 3.659,
3.448, 2.674, 0, 1.724, 0.548, 0, 0.136, 0, 0.486, 0), datasetversion = c("2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a"), id_perm = c("R2XX9S", "2D829W", "TSAEYI", "T4LHGH",
"1YK8PA", "HO9FS1", "WFHDPR", "E7SUA1", "JOLKMR", "VZQDDH", "E5Z7JT",
"Y6BLD9", "K4RKCF", "PZB7CH", "FQQ88F", "1N56G3", "QMGCFN", "CR6LFI",
"XGWAUX", "SNXNNE", "YORPKV", "O6IQEJ", "Q9TAO9", "1PBVG5", "PF3F5D",
"JN1LZH", "CMR7F6", "Z6OL6C", "YMKVN2", "U4SCRD", "1BDCF8", "AGY4FF",
"Z8T6BR", "C1HESX", "PP1FLT", "VRQUU4", "BYBDN4", "TSWP7S", "87A16O",
"M5KFE1", "RW9KFZ", "2V3SRK", "LHZGG9", "22FV9F", "RA5LSZ", "4GDSR7",
"GSWOU2", "PM7TNC", "DYTZIJ", "VRYPF4", "45R9EV", "X3JG4S", "H9QEF1",
"FJ5DYQ", "9V96FY", "JDKL6J", "9DP64U", "8NB5C3", "VONCDV", "HJW2XP",
"ZCMMSZ", "K2B256", "G7GB47", "4A9XMW", "1QS6AT", "AOG6ZL", "AEY94Q",
"F5PMRC", "DCQMXK", "H2U7BY", "AOP8QA", "VSFCG5", "PJNKSC", "WPEI98",
"5IZK7H", "G9N2I8", "UBTI1E", "NUPH65", "PUN61D", "EYTEP7", "9STC2J",
"GNUP8H", "B5JVLZ", "NDPIGY", "HBDI2K", "OQ4HYY", "GZWEAU", "8I1FL1",
"3POAKF", "KTJQKG", "WSKKA7", "AXULXT", "VNYVIG", "DQFNM3", "CSKYBU",
"OHUKIF", "HK132K", "XXUREL", "4ILMPJ", "TNUUY7"), RRP = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), pre.post = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), treat = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), first.treat = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-100L), groups = structure(list(party = c(11220, 11320, 11420,
11620, 11810, 12220, 12320, 12420, 12520, 12620, 12810, 13220,
13320, 13410, 13420, 13620, 13952, 13953, 14221, 14320, 14420,
14620, 14810, 14901, 15220, 15320, 15620, 15810, 21320, 21420,
21520, 22210, 22320, 22420, 22522, 22523, 22525, 22952, 23220,
23320, 23420, 23520, 31220, 31320, 31421, 31521, 31621, 31622,
32220, 32320, 32410, 32420, 32520, 43320, 43321, 51320, 51420,
51620, 52320, 52620, 52710, 61320, 61620, 61621, 62320, 62420,
62620, 62951, 63320, 63620, 63810, 64320, 64620, 73330, 171301,
171601), .rows = structure(list(26L, 27L, 28L, 29L, 30L, 49L,
50L, 51L, 52L, 53L, 54L, 59L, 60L, 61L, 62L, 63L, 64L, 65L,
33L, 34L, 35L, 36L, 37L, 38L, 81L, 82L, 83L, 84L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 55L, 56L, 57L, 58L,
90L, 91L, 92L, 93L, 94L, 95L, 76L, 77L, 78L, 79L, 80L, 99L,
100L, 46L, 47L, 48L, c(8L, 17L, 21L, 39L), c(3L, 9L, 13L,
18L, 22L, 40L), c(4L, 10L, 14L, 23L, 41L), c(1L, 5L, 11L,
15L, 19L, 24L, 31L), c(2L, 6L, 12L, 16L, 20L, 25L, 32L),
7L, 42L, 43L, 44L, 45L, 87L, 88L, 89L, 96L, 97L, 98L, 85L,
86L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, 76L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
But I am not even able to get the first steps well.
Thanks!
We can write a small function which returns the year where treat values is 1 for the first time.
library(dplyr)
get_first_treat <- function(x, y) {
inds = match(1, x)
if(is.na(inds)) 0 else y[inds]
}
and apply this function for each party :
df %>%
group_by(party) %>%
mutate(first.treat1 = get_first_treat(treat, edate))
# party edate RRP absseats country treat first.treat
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 A 1991 1 0 11 0 1998
# 2 B 1991 0 20 11 0 1998
# 3 C 1991 0 30 11 0 1998
# 4 A 1995 1 5 11 0 1998
# 5 B 1995 0 25 11 0 1998
# 6 C 1995 0 20 11 0 1998
# 7 A 1998 1 0 11 1 1998
# 8 B 1998 0 10 11 1 1998
# 9 C 1998 0 28 11 1 1998
#10 D 1998 0 12 11 1 1998
# … with 24 more rows

mapping over lists and selecting columns (to create new lists)

I am trying to map over some data and select some columns. I try to run the folling:
map(dat$splits, ~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
map(., ~select(X1, X2, X3, X4, X5, X6, X7, X8, X9)))
But get given an error, however I can see the tibbles by using:
map(dat$splits, ~ analysis(.x))
Which gives:
[[1]]
# A tibble: 20 x 17
date ID var1 var2 Y Y_plus_1 X1 X2 X3 X4 X5 X6 X7 X8
<date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2016-10-21 CAT1 799. 3.01e-3 1 0 4.14 18 0 1 0.0770 4.29e-4 0.652 -0.976
2 2016-10-24 CAT1 813. 1.72e-2 1 1 4.14 17 0 1 0.0793 4.24e-4 0.634 -0.861
3 2016-10-25 CAT1 808. -6.69e-3 0 1 4.16 16 0 1 0.0804 4.04e-4 0.780 -0.478
4 2016-10-26 CAT1 799. -1.06e-2 0 0 4.16 15 0 1 0.0626 4.26e-4 0.378 -0.332
5 2016-10-27 CAT1 795. -4.66e-3 0 0 4.16 14 0 1 0.0587 4.42e-4 -0.253 -0.647
6 2016-10-28 CAT1 795. 2.52e-5 0 0 4.11 13 0 1 0.0610 4.40e-4 -0.523 -0.753
7 2016-10-31 CAT1 785. -1.36e-2 0 0 4.14 12 0 1 0.0704 4.25e-4 -0.125 -0.620
8 2016-11-01 CAT1 784. -1.19e-3 1 0 3.98 11 0 1 0.0867 4.17e-4 -0.933 -1.30
9 2016-11-02 CAT1 769. -1.90e-2 0 1 6.00 10 0 1 0.0673 4.53e-4 -0.958 -0.793
10 2016-11-03 CAT1 762. -8.55e-3 0 0 8.42 10 0 1 0.0877 4.29e-4 -1.51 -1.17
11 2016-11-04 CAT1 762. -1.44e-4 1 0 7.95 10 0 1 0.0924 4.26e-4 -1.67 -1.21
12 2016-11-07 CAT1 783. 2.69e-2 1 1 7.89 10 0 1 0.0821 4.24e-4 -1.64 -1.06
13 2016-11-08 CAT1 791. 1.02e-2 1 1 7.64 10 0 1 0.0361 4.51e-4 -0.963 0.0116
14 2016-11-09 CAT1 785. -6.58e-3 1 1 4.51 10 0 1 0.0634 4.87e-4 -0.762 1.15
15 2016-11-10 CAT1 763. -2.90e-2 0 1 4.53 10 0 1 0.0290 5.26e-4 -1.32 0.560
16 2016-11-11 CAT1 754. -1.12e-2 0 0 4.40 10 0 1 0.0404 4.94e-4 -1.74 0.142
17 2016-11-14 CAT1 736. -2.38e-2 0 0 4.19 10 0 1 0.0587 4.93e-4 -2.32 -0.504
18 2016-11-15 CAT1 758. 3.04e-2 1 0 4.27 10 0 1 0.125 4.17e-4 -2.74 -1.19
19 2016-11-16 CAT1 764. 7.90e-3 1 1 2.31 29 0 1 0.0637 3.30e-4 -2.16 0.593
20 2016-11-17 CAT1 771. 8.83e-3 1 1 2.31 28
That is I want to map over and select the X1... X8 columns.
Data:
dat <- structure(list(splits = list(structure(list(data = structure(list(
date = structure(c(17095, 17098, 17099, 17100, 17101, 17102,
17105, 17106, 17107, 17108, 17109, 17112, 17113, 17114, 17115,
17116, 17119, 17120, 17121, 17122, 17123, 17126, 17127, 17128,
17130, 17133, 17134, 17135, 17136, 17137, 17140, 17141, 17142,
17143, 17144, 17147, 17148, 17149, 17150, 17151, 17154, 17155,
17156, 17157, 17158, 17162, 17163, 17164, 17165), class = "Date"),
ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1"), var1 = c(799.369995, 813.109985, 807.669983,
799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999,
764.47998, 771.22998, 760.539978, 769.200012, 768.27002,
760.98999, 761.679993, 768.23999, 770.840027, 758.039978,
747.919983, 750.5, 762.52002, 759.109985, 771.190002, 776.419983,
789.289978, 789.27002, 796.099976, 797.070007, 797.849976,
790.799988, 794.200012, 796.419983, 794.559998, 791.26001,
789.909973, 791.549988, 785.049988, 782.789978, 771.820007
), var2 = c(0.00301143592272179, 0.0171885235697395, -0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978, -0.0012090379426567, -0.00947587412040363,
0.00090671757719174, 0.00861253683999563, 0.00338440726054889,
-0.016605324777718, -0.0133502127773003, 0.00344958960669994,
0.0160160159893405, -0.00447205963195563, 0.0159133949476373,
0.00678170228664343, 0.0165760738798502, -0.0000252860172512692,
0.00865350998635406, 0.00121847887105075, 0.000978545163097477,
-0.00883623264030775, 0.00429947401567232, 0.00279522911918573,
-0.00233543235943645, -0.00415322695366804, -0.00170618631415476,
0.00207620495506755, -0.00821173659091756, -0.00287881031086645,
-0.0140139389980795), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0), Y_plus_1 = c(0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251, 2.3083029424251,
2.30720560360488, 2.30720560360486, 2.33467572807035, 2.33467572807036,
2.33467572807036, 2.30720560360486, 2.33467572807035, 2.31545097851707,
2.3399413414153, 2.40685890963718, 2.40309440701756, 2.33633188340289,
2.3363318834029, 2.23996107961566, 2.23996107961567, 2.23996107961566,
3.14644299189703, 3.1910343925295, 3.4393907031427, 3.30413087760388,
3.33080017630688, 2.63827508869038, 2.99443088216722, 2.99443088216723,
2.99443088216722, 2.99443088216722, 3.14542139469794, 3.14542139469794
), X2 = c(18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10,
87, 86, 85, 84, 83, 82, 81), X3 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), X4 = 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, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0769944316144198,
0.0793311568823971, 0.0803743155230278, 0.0625789438826206,
0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155,
0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993,
0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347,
0.0437146614571738, 0.0399432627968126, 0.0535895503558405,
0.0538598239712004, 0.0600971764378981, 0.0543532803438423,
0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317,
0.0391189676384125, 0.051151724195942, 0.0442197922283997,
0.0458769828703159, 0.0392536462039503, 0.0397989336000519,
0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456,
0.00900392496961011, 0.0148108452415051, 0.0159871581537364
), X6 = c(0.000428660536007568, 0.000424348382531349, 0.000403672086504106,
0.000425772306880377, 0.000441567036819891, 0.000440420473928468,
0.000424616565866307, 0.000417156794102717, 0.000453102696396517,
0.000429420158272163, 0.000426339236438714, 0.000424204011080916,
0.000450812884669126, 0.00048728803860348, 0.000526461561504051,
0.000494106517096305, 0.000493488610269819, 0.00041740609044358,
0.000329604373072286, 0.000321981688032803, 0.000313820182149535,
0.000324018084037671, 0.000321928021838835, 0.000325886279909115,
0.000324905583026473, 0.000323263064904554, 0.000315582726878559,
0.00033115144688, 0.000326176783596685, 0.000320421043513733,
0.000317459171547033, 0.000306378296724892, 0.000304230982248009,
0.00031578316067723, 0.000315783239223671, 0.000308443856342272,
0.000302387474982801, 0.000302702247056619, 0.000300580888361005,
0.000298662388842681, 0.000295031270763261, 0.000300955138678924,
0.000308009865186193, 0.00031004060452567, 0.000308580481883199,
0.000309081734643359, 0.000309347430761987, 0.00031610525741575,
0.000316030523318374), X7 = c(0.652246323794644, 0.633658121909502,
0.779640005424855, 0.37849713571782, -0.25338461752528, -0.522937755983531,
-0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041, -2.05934220840334,
-1.83997395786783, -1.66507504806585, -1.90235222119862,
-1.84142082212644, -1.77786289146221, -1.23082082088862,
-1.53821065097434, -1.56812844617651, -1.59984630224514,
-1.15906488048868, -1.18713323505712, -0.57100200805735,
-0.228511848060405, 0.253315947225092, 0.0532312338057499,
0.223428854601438, -0.00144862031236322, -0.0398092612664749,
-0.0588761129681466, -0.0771471433941018, 0.117493259867833,
0.459197464971977, 0.292335416896516, 0.114242276938583,
0.202233666501737, 0.331679162319782, 0.175470525372577),
X8 = c(-0.975973892452776, -0.861258722930479, -0.477565916680931,
-0.33185021650311, -0.647129621511124, -0.752564004048953,
-0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353,
0.142148842103466, -0.504045772338972, -1.18889934824633,
0.593184249578906, 0.999978721521271, 1.3874173248314, 0.798117587221041,
0.799993914873595, 0.748516271275043, 0.617354487136954,
0.66965220108721, 0.886228797393692, 1.10466660545966, 0.37553937583687,
0.113225726134138, 0.35930466268218, 0.588184742745078, 0.536929080120224,
0.875533406569582, 1.29869394121533, 1.3954069386108, 1.2896602501424,
1.28952719595234, 1.04620293691641, 1.04835604946992, 0.944659231862545,
0.658278997146449, 0.530493113505759, 0.386873773618166,
0.412971784704427, 0.116661980563476, 0.274658721324867,
-0.231454627137936, -0.0490920485204462), X9 = c(-0.0145637619761308,
-0.0147930666936059, -0.0147662947883565, -0.0325177535962596,
-0.0128572105783758, -0.0126204141222343, -0.0193506283734377,
-0.0204879399440294, -0.015817519858536, -0.0171950326442131,
-0.0146419066547099, -0.0104033279055883, -0.00702994914474458,
0.012163515055523, 0.0311052843018782, 0.0466972362285693,
0.0638538376999092, 0.0587189119891982, -0.0382761719744182,
0.017369814913167, 0.00894072522957917, 0.0159165549773805,
-0.00278974160229076, 0.00087423648615031, -0.000799924570855513,
-0.00720654828839435, 0.00144205696290743, -0.00744120420924264,
-0.00623238593684327, 0.0140296542021987, 0.0078581351311665,
0.0194264610155223, 0.0214019946797077, 0.0135050903487779,
0.0136275994696147, 0.0191532733906993, 0.0145192162284441,
0.0180836191687308, 0.0211148970828572, 0.0228082176297255,
0.0282007760760499, 0.0246107996585935, 0.0169675399817873,
0.0207974225382235, 0.0246684486060467, 0.0216975706632474,
0.0291883466992148, 0.0253376536386199, 0.0262304363854249
), X10 = c(0.225525775667678, 0.218320938251066, 0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383, 0.218320516349615,
0.216853385184294, 0.180488106442423, 0.177799948562715,
0.186118707334448, 0.177473777344988, 0.161807804965392,
0.167723342514708, 0.158568160654393, 0.16773089626074, 0.16307716529608,
0.162099891399657, 0.15812964617053, 0.149690169923917, 0.164750142363451,
0.163460075128328, 0.155183021202297, 0.14616757689419, 0.154376710865108,
0.147389439599357, 0.183009449087266, 0.175519602867033,
0.177732643891337, 0.17814584046646, 0.175149718955584, 0.176134687816332,
0.175399285149764), X11 = c(0.510204081632653, 0.520408163265306,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.520408163265306, 0.530612244897959,
0.530612244897959, 0.540816326530612, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.530612244897959, 0.536082474226804, 0.536082474226804,
0.536082474226804, 0.530612244897959, 0.525773195876289,
0.536082474226804, 0.510204081632653, 0.520408163265306,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.510204081632653, 0.510204081632653,
0.510204081632653, 0.510204081632653, 0.510204081632653,
0.5, 0.5, 0.489795918367347, 0.489795918367347, 0.489795918367347,
0.5, 0.5, 0.510204081632653, 0.5, 0.5, 0.510204081632653,
0.510204081632653, 0.520408163265306, 0.520408163265306)), row.names = c(NA,
-49L), class = c("tbl_df", "tbl", "data.frame")), in_id = 1:20,
out_id = 21L, id = structure(list(id = "Slice01"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))), class = c("rsplit",
"rof_split")), structure(list(data = structure(list(date = structure(c(17095,
17098, 17099, 17100, 17101, 17102, 17105, 17106, 17107, 17108,
17109, 17112, 17113, 17114, 17115, 17116, 17119, 17120, 17121,
17122, 17123, 17126, 17127, 17128, 17130, 17133, 17134, 17135,
17136, 17137, 17140, 17141, 17142, 17143, 17144, 17147, 17148,
17149, 17150, 17151, 17154, 17155, 17156, 17157, 17158, 17162,
17163, 17164, 17165), class = "Date"), ID = c("CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"), var1 = c(799.369995,
813.109985, 807.669983, 799.070007, 795.349976, 795.369995, 784.539978,
783.609985, 768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999, 764.47998,
771.22998, 760.539978, 769.200012, 768.27002, 760.98999, 761.679993,
768.23999, 770.840027, 758.039978, 747.919983, 750.5, 762.52002,
759.109985, 771.190002, 776.419983, 789.289978, 789.27002, 796.099976,
797.070007, 797.849976, 790.799988, 794.200012, 796.419983, 794.559998,
791.26001, 789.909973, 791.549988, 785.049988, 782.789978, 771.820007
), var2 = c(0.00301143592272179, 0.0171885235697395, -0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978, -0.0012090379426567, -0.00947587412040363,
0.00090671757719174, 0.00861253683999563, 0.00338440726054889,
-0.016605324777718, -0.0133502127773003, 0.00344958960669994,
0.0160160159893405, -0.00447205963195563, 0.0159133949476373,
0.00678170228664343, 0.0165760738798502, -0.0000252860172512692,
0.00865350998635406, 0.00121847887105075, 0.000978545163097477,
-0.00883623264030775, 0.00429947401567232, 0.00279522911918573,
-0.00233543235943645, -0.00415322695366804, -0.00170618631415476,
0.00207620495506755, -0.00821173659091756, -0.00287881031086645,
-0.0140139389980795), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), Y_plus_1 = c(0,
1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0,
1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 0, 1, 0, 0), X1 = c(4.13858526513854, 4.13858526513855,
4.16341131085939, 4.16341131085937, 4.16341131085937, 4.11423119297315,
4.13858526513857, 3.97599968560627, 5.99758130881283, 8.41953801047614,
7.95231443679086, 7.88558780320248, 7.6408950559188, 4.51370117323327,
4.52868963859669, 4.39998987943623, 4.18852747359839, 4.27042958796773,
2.30720560360487, 2.3083029424251, 2.3083029424251, 2.30720560360488,
2.30720560360486, 2.33467572807035, 2.33467572807036, 2.33467572807036,
2.30720560360486, 2.33467572807035, 2.31545097851707, 2.3399413414153,
2.40685890963718, 2.40309440701756, 2.33633188340289, 2.3363318834029,
2.23996107961566, 2.23996107961567, 2.23996107961566, 3.14644299189703,
3.1910343925295, 3.4393907031427, 3.30413087760388, 3.33080017630688,
2.63827508869038, 2.99443088216722, 2.99443088216723, 2.99443088216722,
2.99443088216722, 3.14542139469794, 3.14542139469794), X2 = c(18,
17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
14, 13, 12, 11, 10, 10, 10, 10, 10, 87, 86, 85, 84, 83, 82, 81
), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
), X5 = c(0.0769944316144198, 0.0793311568823971, 0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347, 0.0437146614571738,
0.0399432627968126, 0.0535895503558405, 0.0538598239712004, 0.0600971764378981,
0.0543532803438423, 0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317, 0.0391189676384125,
0.051151724195942, 0.0442197922283997, 0.0458769828703159, 0.0392536462039503,
0.0397989336000519, 0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456, 0.00900392496961011,
0.0148108452415051, 0.0159871581537364), X6 = c(0.000428660536007568,
0.000424348382531349, 0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835, 0.000325886279909115, 0.000324905583026473,
0.000323263064904554, 0.000315582726878559, 0.00033115144688,
0.000326176783596685, 0.000320421043513733, 0.000317459171547033,
0.000306378296724892, 0.000304230982248009, 0.00031578316067723,
0.000315783239223671, 0.000308443856342272, 0.000302387474982801,
0.000302702247056619, 0.000300580888361005, 0.000298662388842681,
0.000295031270763261, 0.000300955138678924, 0.000308009865186193,
0.00031004060452567, 0.000308580481883199, 0.000309081734643359,
0.000309347430761987, 0.00031610525741575, 0.000316030523318374
), X7 = c(0.652246323794644, 0.633658121909502, 0.779640005424855,
0.37849713571782, -0.25338461752528, -0.522937755983531, -0.124971717359712,
-0.93339126000489, -0.957987757878853, -1.50800507959919, -1.67334654587184,
-1.6438607078889, -0.962881285608565, -0.761952591493898, -1.31627449065341,
-1.73806924167703, -2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041, -2.05934220840334, -1.83997395786783,
-1.66507504806585, -1.90235222119862, -1.84142082212644, -1.77786289146221,
-1.23082082088862, -1.53821065097434, -1.56812844617651, -1.59984630224514,
-1.15906488048868, -1.18713323505712, -0.57100200805735, -0.228511848060405,
0.253315947225092, 0.0532312338057499, 0.223428854601438, -0.00144862031236322,
-0.0398092612664749, -0.0588761129681466, -0.0771471433941018,
0.117493259867833, 0.459197464971977, 0.292335416896516, 0.114242276938583,
0.202233666501737, 0.331679162319782, 0.175470525372577), X8 = c(-0.975973892452776,
-0.861258722930479, -0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641, 0.0115994967476536,
1.15288649261686, 0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271, 1.3874173248314,
0.798117587221041, 0.799993914873595, 0.748516271275043, 0.617354487136954,
0.66965220108721, 0.886228797393692, 1.10466660545966, 0.37553937583687,
0.113225726134138, 0.35930466268218, 0.588184742745078, 0.536929080120224,
0.875533406569582, 1.29869394121533, 1.3954069386108, 1.2896602501424,
1.28952719595234, 1.04620293691641, 1.04835604946992, 0.944659231862545,
0.658278997146449, 0.530493113505759, 0.386873773618166, 0.412971784704427,
0.116661980563476, 0.274658721324867, -0.231454627137936, -0.0490920485204462
), X9 = c(-0.0145637619761308, -0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982, -0.0382761719744182,
0.017369814913167, 0.00894072522957917, 0.0159165549773805, -0.00278974160229076,
0.00087423648615031, -0.000799924570855513, -0.00720654828839435,
0.00144205696290743, -0.00744120420924264, -0.00623238593684327,
0.0140296542021987, 0.0078581351311665, 0.0194264610155223, 0.0214019946797077,
0.0135050903487779, 0.0136275994696147, 0.0191532733906993, 0.0145192162284441,
0.0180836191687308, 0.0211148970828572, 0.0228082176297255, 0.0282007760760499,
0.0246107996585935, 0.0169675399817873, 0.0207974225382235, 0.0246684486060467,
0.0216975706632474, 0.0291883466992148, 0.0253376536386199, 0.0262304363854249
), X10 = c(0.225525775667678, 0.218320938251066, 0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601, 0.162160340961121,
0.160663336867733, 0.116728767616044, 0.0744707092081847, 0.0816373143043771,
0.120472162777503, 0.120923040165681, 0.10545848677116, 0.172548378854213,
0.184998553367414, 0.181410472295269, 0.248454355918383, 0.218320516349615,
0.216853385184294, 0.180488106442423, 0.177799948562715, 0.186118707334448,
0.177473777344988, 0.161807804965392, 0.167723342514708, 0.158568160654393,
0.16773089626074, 0.16307716529608, 0.162099891399657, 0.15812964617053,
0.149690169923917, 0.164750142363451, 0.163460075128328, 0.155183021202297,
0.14616757689419, 0.154376710865108, 0.147389439599357, 0.183009449087266,
0.175519602867033, 0.177732643891337, 0.17814584046646, 0.175149718955584,
0.176134687816332, 0.175399285149764), X11 = c(0.510204081632653,
0.520408163265306, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959, 0.540816326530612,
0.530612244897959, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.530612244897959, 0.536082474226804, 0.536082474226804, 0.536082474226804,
0.530612244897959, 0.525773195876289, 0.536082474226804, 0.510204081632653,
0.520408163265306, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.520408163265306, 0.510204081632653, 0.510204081632653, 0.510204081632653,
0.510204081632653, 0.510204081632653, 0.5, 0.5, 0.489795918367347,
0.489795918367347, 0.489795918367347, 0.5, 0.5, 0.510204081632653,
0.5, 0.5, 0.510204081632653, 0.510204081632653, 0.520408163265306,
0.520408163265306)), row.names = c(NA, -49L), class = c("tbl_df",
"tbl", "data.frame")), in_id = 2:21, out_id = 22L, id = structure(list(
id = "Slice02"), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame"))), class = c("rsplit", "rof_split"))), id = c("Slice01",
"Slice02")), row.names = 1:2, class = c("rset", "tbl_df", "tbl",
"data.frame"), initial = 20, assess = 1, cumulative = FALSE, skip = 0)
We don't need the . and map. After the select, it can be converted to list with as.list (if it is required), otherwise, just keep it as the tibble
library(purrr)
library(dplyr)
map(dat$splits, ~ analysis(.x) %>%
as_tibble(., .name_repair = "universal") %>%
select(X1:X9) %>%
as.list)

map a custom function to certain columns in a list of tibbles

I am trying to apply map to scale a number of columns, the data looks like:
# A tibble: 20 x 18
date ID var1 var2 Y Y_plus_1 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
<date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2016-10-28 CAT1 795. 2.52e-5 0 0 4.11 13 0 1 0.0610 4.40e-4 -0.523 -0.753 -1.26e-2 0.170
2 2016-10-31 CAT1 785. -1.36e-2 0 0 4.14 12 0 1 0.0704 4.25e-4 -0.125 -0.620 -1.94e-2 0.169
3 2016-11-01 CAT1 784. -1.19e-3 1 0 3.98 11 0 1 0.0867 4.17e-4 -0.933 -1.30 -2.05e-2 0.162
4 2016-11-02 CAT1 769. -1.90e-2 0 1 6.00 10 0 1 0.0673 4.53e-4 -0.958 -0.793 -1.58e-2 0.159
5 2016-11-03 CAT1 762. -8.55e-3 0 0 8.42 10 0 1 0.0877 4.29e-4 -1.51 -1.17 -1.72e-2 0.164
6 2016-11-04 CAT1 762. -1.44e-4 1 0 7.95 10 0 1 0.0924 4.26e-4 -1.67 -1.21 -1.46e-2 0.162
7 2016-11-07 CAT1 783. 2.69e-2 1 1 7.89 10 0 1 0.0821 4.24e-4 -1.64 -1.06 -1.04e-2 0.161
8 2016-11-08 CAT1 791. 1.02e-2 1 1 7.64 10 0 1 0.0361 4.51e-4 -0.963 0.0116 -7.03e-3 0.117
9 2016-11-09 CAT1 785. -6.58e-3 1 1 4.51 10 0 1 0.0634 4.87e-4 -0.762 1.15 1.22e-2 0.0745
10 2016-11-10 CAT1 763. -2.90e-2 0 1 4.53 10 0 1 0.0290 5.26e-4 -1.32 0.560 3.11e-2 0.0816
11 2016-11-11 CAT1 754. -1.12e-2 0 0 4.40 10 0 1 0.0404 4.94e-4 -1.74 0.142 4.67e-2 0.120
12 2016-11-14 CAT1 736. -2.38e-2 0 0 4.19 10 0 1 0.0587 4.93e-4 -2.32 -0.504 6.39e-2 0.121
13 2016-11-15 CAT1 758. 3.04e-2 1 0 4.27 10 0 1 0.125 4.17e-4 -2.74 -1.19 5.87e-2 0.105
14 2016-11-16 CAT1 764. 7.90e-3 1 1 2.31 29 0 1 0.0637 3.30e-4 -2.16 0.593 -3.83e-2 0.173
15 2016-11-17 CAT1 771. 8.83e-3 1 1 2.31 28 0 1 0.0749 3.22e-4 -2.19 1.000 1.74e-2 0.185
16 2016-11-18 CAT1 761. -1.39e-2 0 1 2.31 27 0 1 0.0828 3.14e-4 -1.70 1.39 8.94e-3 0.181
17 2016-11-21 CAT1 769. 1.14e-2 1 0 2.31 26 0 1 0.0589 3.24e-4 -2.06 0.798 1.59e-2 0.248
18 2016-11-22 CAT1 768. -1.21e-3 0 1 2.31 25 0 1 0.0437 3.22e-4 -1.84 0.800 -2.79e-3 0.218
19 2016-11-23 CAT1 761. -9.48e-3 0 0 2.33 24 0 1 0.0399 3.26e-4 -1.67 0.749 8.74e-4 0.217
20 2016-11-25 CAT1 762. 9.07e-4 0 0 2.33 23 0 1 0.0536 3.25e-4 -1.90 0.617 -8.00e-4 0.180
I have a scale function I would like to apply to all the X variables in each list. I am just wondering how I can apply the scale function to all of these columns in each list.
Function:
Scale_Me <- function(x){
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
Code for one variable:
map(mylist, ~mutate(.x,
scala = Scale_Me(.x[["var1"]])))
Data:
mylist <- list(structure(list(date = structure(c(17095, 17098, 17099, 17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122), class = "Date"),
ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1"), var1 = c(799.369995,
813.109985, 807.669983, 799.070007, 795.349976, 795.369995,
784.539978, 783.609985, 768.700012, 762.130005, 762.02002,
782.52002, 790.51001, 785.309998, 762.559998, 754.02002,
736.080017, 758.48999, 764.47998, 771.22998), var2 = c(0.00301143592272179,
0.0171885235697395, -0.00669036428079295, -0.0106478836418512,
-0.00465545067066953, 0.0000251700516804565, -0.0136163258207899,
-0.00118539912060411, -0.0190272881732103, -0.00854690633203736,
-0.000144312649125955, 0.0269021803390415, 0.0102105886057713,
-0.00657804700031572, -0.0289694516279417, -0.0111990899370517,
-0.0237924756958046, 0.0304450229355975, 0.00789725649510542,
0.0088295314155904), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1), Y_plus_1 = c(0, 1, 1, 0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251), X2 = c(18,
17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 29, 28), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0769944316144198,
0.0793311568823971, 0.0803743155230278, 0.0625789438826206,
0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155,
0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993,
0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617), X6 = c(0.000428660536007568, 0.000424348382531349,
0.000403672086504106, 0.000425772306880377, 0.000441567036819891,
0.000440420473928468, 0.000424616565866307, 0.000417156794102717,
0.000453102696396517, 0.000429420158272163, 0.000426339236438714,
0.000424204011080916, 0.000450812884669126, 0.00048728803860348,
0.000526461561504051, 0.000494106517096305, 0.000493488610269819,
0.00041740609044358, 0.000329604373072286, 0.000321981688032803
), X7 = c(0.652246323794644, 0.633658121909502, 0.779640005424855,
0.37849713571782, -0.25338461752528, -0.522937755983531,
-0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069), X8 = c(-0.975973892452776, -0.861258722930479,
-0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818,
-1.06429334017641, 0.0115994967476536, 1.15288649261686,
0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271),
X9 = c(-0.0145637619761308, -0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982,
-0.0382761719744182, 0.017369814913167), X10 = c(0.225525775667678,
0.218320938251066, 0.205736199627003, 0.18430441198689, 0.171653942908123,
0.169720213220954, 0.169160112065287, 0.162051896119269,
0.158642308835919, 0.163696353319601, 0.162160340961121,
0.160663336867733, 0.116728767616044, 0.0744707092081847,
0.0816373143043771, 0.120472162777503, 0.120923040165681,
0.10545848677116, 0.172548378854213, 0.184998553367414),
X11 = c(0.510204081632653, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
date = structure(c(17098, 17099, 17100, 17101, 17102, 17105,
17106, 17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116,
17119, 17120, 17121, 17122, 17123), class = "Date"), ID = c("CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), var1 = c(813.109985, 807.669983,
799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001,
785.309998, 762.559998, 754.02002, 736.080017, 758.48999,
764.47998, 771.22998, 760.539978), var2 = c(0.0171885235697395,
-0.00669036428079295, -0.0106478836418512, -0.00465545067066953,
0.0000251700516804565, -0.0136163258207899, -0.00118539912060411,
-0.0190272881732103, -0.00854690633203736, -0.000144312649125955,
0.0269021803390415, 0.0102105886057713, -0.00657804700031572,
-0.0289694516279417, -0.0111990899370517, -0.0237924756958046,
0.0304450229355975, 0.00789725649510542, 0.0088295314155904,
-0.0138609782778413), Y = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
1, 1, 1, 0, 0, 0, 1, 1, 1, 0), Y_plus_1 = c(1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1), X1 = c(4.13858526513855,
4.16341131085939, 4.16341131085937, 4.16341131085937, 4.11423119297315,
4.13858526513857, 3.97599968560627, 5.99758130881283, 8.41953801047614,
7.95231443679086, 7.88558780320248, 7.6408950559188, 4.51370117323327,
4.52868963859669, 4.39998987943623, 4.18852747359839, 4.27042958796773,
2.30720560360487, 2.3083029424251, 2.3083029424251), X2 = c(17,
16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 29, 28, 27), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0793311568823971,
0.0803743155230278, 0.0625789438826206, 0.0586506192715035,
0.0610303101083243, 0.0703939970975855, 0.0867098273608016,
0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928,
0.0289807505667197, 0.0403890038946993, 0.0587200889534704,
0.124855015077667, 0.0636602113103218, 0.0748772236055617,
0.0828248414842617), X6 = c(0.000424348382531349, 0.000403672086504106,
0.000425772306880377, 0.000441567036819891, 0.000440420473928468,
0.000424616565866307, 0.000417156794102717, 0.000453102696396517,
0.000429420158272163, 0.000426339236438714, 0.000424204011080916,
0.000450812884669126, 0.00048728803860348, 0.000526461561504051,
0.000494106517096305, 0.000493488610269819, 0.00041740609044358,
0.000329604373072286, 0.000321981688032803, 0.000313820182149535
), X7 = c(0.633658121909502, 0.779640005424855, 0.37849713571782,
-0.25338461752528, -0.522937755983531, -0.124971717359712,
-0.93339126000489, -0.957987757878853, -1.50800507959919,
-1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703,
-2.31894300820554, -2.73548173635386, -2.16326733735933,
-2.18976199245069, -1.70174528306041), X8 = c(-0.861258722930479,
-0.477565916680931, -0.33185021650311, -0.647129621511124,
-0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818,
-1.06429334017641, 0.0115994967476536, 1.15288649261686,
0.559547871095353, 0.142148842103466, -0.504045772338972,
-1.18889934824633, 0.593184249578906, 0.999978721521271,
1.3874173248314), X9 = c(-0.0147930666936059, -0.0147662947883565,
-0.0325177535962596, -0.0128572105783758, -0.0126204141222343,
-0.0193506283734377, -0.0204879399440294, -0.015817519858536,
-0.0171950326442131, -0.0146419066547099, -0.0104033279055883,
-0.00702994914474458, 0.012163515055523, 0.0311052843018782,
0.0466972362285693, 0.0638538376999092, 0.0587189119891982,
-0.0382761719744182, 0.017369814913167, 0.00894072522957917
), X10 = c(0.218320938251066, 0.205736199627003, 0.18430441198689,
0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269), X11 = c(0.520408163265306, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804, 0.536082474226804)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
date = structure(c(17099, 17100, 17101, 17102, 17105, 17106,
17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119,
17120, 17121, 17122, 17123, 17126), class = "Date"), ID = c("CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1"), var1 = c(807.669983, 799.070007,
795.349976, 795.369995, 784.539978, 783.609985, 768.700012,
762.130005, 762.02002, 782.52002, 790.51001, 785.309998,
762.559998, 754.02002, 736.080017, 758.48999, 764.47998,
771.22998, 760.539978, 769.200012), var2 = c(-0.00669036428079295,
-0.0106478836418512, -0.00465545067066953, 0.0000251700516804565,
-0.0136163258207899, -0.00118539912060411, -0.0190272881732103,
-0.00854690633203736, -0.000144312649125955, 0.0269021803390415,
0.0102105886057713, -0.00657804700031572, -0.0289694516279417,
-0.0111990899370517, -0.0237924756958046, 0.0304450229355975,
0.00789725649510542, 0.0088295314155904, -0.0138609782778413,
0.0113866913646978), Y = c(0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1, 0, 1), Y_plus_1 = c(1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0), X1 = c(4.16341131085939,
4.16341131085937, 4.16341131085937, 4.11423119297315, 4.13858526513857,
3.97599968560627, 5.99758130881283, 8.41953801047614, 7.95231443679086,
7.88558780320248, 7.6408950559188, 4.51370117323327, 4.52868963859669,
4.39998987943623, 4.18852747359839, 4.27042958796773, 2.30720560360487,
2.3083029424251, 2.3083029424251, 2.30720560360488), X2 = c(16,
15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
29, 28, 27, 26), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243,
0.0703939970975855, 0.0867098273608016, 0.0673120522212106,
0.0877296725069155, 0.0923742675361241, 0.0821050363880187,
0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667,
0.0636602113103218, 0.0748772236055617, 0.0828248414842617,
0.0588561607897347), X6 = c(0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671
), X7 = c(0.779640005424855, 0.37849713571782, -0.25338461752528,
-0.522937755983531, -0.124971717359712, -0.93339126000489,
-0.957987757878853, -1.50800507959919, -1.67334654587184,
-1.6438607078889, -0.962881285608565, -0.761952591493898,
-1.31627449065341, -1.73806924167703, -2.31894300820554,
-2.73548173635386, -2.16326733735933, -2.18976199245069,
-1.70174528306041, -2.05934220840334), X8 = c(-0.477565916680931,
-0.33185021650311, -0.647129621511124, -0.752564004048953,
-0.620377457915967, -1.30234931058582, -0.793283548875438,
-1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353,
0.142148842103466, -0.504045772338972, -1.18889934824633,
0.593184249578906, 0.999978721521271, 1.3874173248314, 0.798117587221041
), X9 = c(-0.0147662947883565, -0.0325177535962596, -0.0128572105783758,
-0.0126204141222343, -0.0193506283734377, -0.0204879399440294,
-0.015817519858536, -0.0171950326442131, -0.0146419066547099,
-0.0104033279055883, -0.00702994914474458, 0.012163515055523,
0.0311052843018782, 0.0466972362285693, 0.0638538376999092,
0.0587189119891982, -0.0382761719744182, 0.017369814913167,
0.00894072522957917, 0.0159165549773805), X10 = c(0.205736199627003,
0.18430441198689, 0.171653942908123, 0.169720213220954, 0.169160112065287,
0.162051896119269, 0.158642308835919, 0.163696353319601,
0.162160340961121, 0.160663336867733, 0.116728767616044,
0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383), X11 = c(0.520408163265306,
0.530612244897959, 0.530612244897959, 0.520408163265306,
0.520408163265306, 0.520408163265306, 0.530612244897959,
0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306,
0.530612244897959, 0.530612244897959, 0.530612244897959,
0.536082474226804, 0.536082474226804, 0.536082474226804,
0.530612244897959)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(date = structure(c(17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122, 17123, 17126,
17127), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), var1 = c(799.070007, 795.349976, 795.369995, 784.539978, 783.609985,
768.700012, 762.130005, 762.02002, 782.52002, 790.51001, 785.309998,
762.559998, 754.02002, 736.080017, 758.48999, 764.47998, 771.22998,
760.539978, 769.200012, 768.27002), var2 = c(-0.0106478836418512,
-0.00465545067066953, 0.0000251700516804565, -0.0136163258207899,
-0.00118539912060411, -0.0190272881732103, -0.00854690633203736,
-0.000144312649125955, 0.0269021803390415, 0.0102105886057713,
-0.00657804700031572, -0.0289694516279417, -0.0111990899370517,
-0.0237924756958046, 0.0304450229355975, 0.00789725649510542,
0.0088295314155904, -0.0138609782778413, 0.0113866913646978,
-0.0012090379426567), Y = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 1, 0), Y_plus_1 = c(0, 0, 0, 0, 0, 1, 0,
0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1), X1 = c(4.16341131085937,
4.16341131085937, 4.11423119297315, 4.13858526513857, 3.97599968560627,
5.99758130881283, 8.41953801047614, 7.95231443679086, 7.88558780320248,
7.6408950559188, 4.51370117323327, 4.52868963859669, 4.39998987943623,
4.18852747359839, 4.27042958796773, 2.30720560360487, 2.3083029424251,
2.3083029424251, 2.30720560360488, 2.30720560360486), X2 = c(15,
14, 13, 12, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 29, 28,
27, 26, 25), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), X5 = c(0.0625789438826206, 0.0586506192715035,
0.0610303101083243, 0.0703939970975855, 0.0867098273608016, 0.0673120522212106,
0.0877296725069155, 0.0923742675361241, 0.0821050363880187, 0.0360531976099817,
0.063410467337928, 0.0289807505667197, 0.0403890038946993, 0.0587200889534704,
0.124855015077667, 0.0636602113103218, 0.0748772236055617, 0.0828248414842617,
0.0588561607897347, 0.0437146614571738), X6 = c(0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835), X7 = c(0.37849713571782, -0.25338461752528,
-0.522937755983531, -0.124971717359712, -0.93339126000489, -0.957987757878853,
-1.50800507959919, -1.67334654587184, -1.6438607078889, -0.962881285608565,
-0.761952591493898, -1.31627449065341, -1.73806924167703, -2.31894300820554,
-2.73548173635386, -2.16326733735933, -2.18976199245069, -1.70174528306041,
-2.05934220840334, -1.83997395786783), X8 = c(-0.33185021650311,
-0.647129621511124, -0.752564004048953, -0.620377457915967, -1.30234931058582,
-0.793283548875438, -1.16602371553076, -1.20919980603818, -1.06429334017641,
0.0115994967476536, 1.15288649261686, 0.559547871095353, 0.142148842103466,
-0.504045772338972, -1.18889934824633, 0.593184249578906, 0.999978721521271,
1.3874173248314, 0.798117587221041, 0.799993914873595), X9 = c(-0.0325177535962596,
-0.0128572105783758, -0.0126204141222343, -0.0193506283734377,
-0.0204879399440294, -0.015817519858536, -0.0171950326442131,
-0.0146419066547099, -0.0104033279055883, -0.00702994914474458,
0.012163515055523, 0.0311052843018782, 0.0466972362285693, 0.0638538376999092,
0.0587189119891982, -0.0382761719744182, 0.017369814913167, 0.00894072522957917,
0.0159165549773805, -0.00278974160229076), X10 = c(0.18430441198689,
0.171653942908123, 0.169720213220954, 0.169160112065287, 0.162051896119269,
0.158642308835919, 0.163696353319601, 0.162160340961121, 0.160663336867733,
0.116728767616044, 0.0744707092081847, 0.0816373143043771, 0.120472162777503,
0.120923040165681, 0.10545848677116, 0.172548378854213, 0.184998553367414,
0.181410472295269, 0.248454355918383, 0.218320516349615), X11 = c(0.530612244897959,
0.530612244897959, 0.520408163265306, 0.520408163265306, 0.520408163265306,
0.530612244897959, 0.520408163265306, 0.530612244897959, 0.530612244897959,
0.540816326530612, 0.530612244897959, 0.520408163265306, 0.530612244897959,
0.530612244897959, 0.530612244897959, 0.536082474226804, 0.536082474226804,
0.536082474226804, 0.530612244897959, 0.525773195876289)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame")))
The following should work:
mylist %>%
map(~ mutate_at(.x, vars(starts_with("X")), Scale_Me))
However, you have some columns with sd = 0 where your function does not work.

Resources