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"))
Related
Looking at similar questions, I could not find one that matched my need.
If one does contain a solution, please share its link.
I have this dput-produced data:
structure(list(Player = c("Seth Lugo", "Jacob deGrom", "Rick Porcello",
"David Peterson", "Michael Wacha", "Seth Lugo", "Jacob deGrom",
"Rick Porcello", "David Peterson", "Steven Matz", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "David Peterson", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "Michael Wacha", "David Peterson",
"Jacob deGrom", "Seth Lugo", "Rick Porcello", "Robert Gsellman",
"Michael Wacha", "Ariel Jurado", "Jacob deGrom", "Rick Porcello",
"Seth Lugo", "Robert Gsellman", "David Peterson"), Date = structure(c(1601164800,
1601078400, 1601078400, 1600905600, 1600819200, 1600732800, 1600646400,
1600560000, 1600473600, 1600387200, 1600300800, 1600214400, 1600128000,
1599955200, 1599868800, 1599782400, 1599609600, 1599523200, 1599436800,
1599350400, 1599264000, 1599177600, 1599091200, 1599004800, 1598918400,
1598832000, 1598745600, 1598745600, 1598659200, 1598572800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), DblHdr = c(0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2), DateStr = c("09/27/2020",
"09/26/2020", "09/26/2020", "09/24/2020", "09/23/2020", "09/22/2020",
"09/21/2020", "09/20/2020", "09/19/2020", "09/18/2020", "09/17/2020",
"09/16/2020", "09/15/2020", "09/13/2020", "09/12/2020", "09/11/2020",
"09/09/2020", "09/08/2020", "09/07/2020", "09/06/2020", "09/05/2020",
"09/04/2020", "09/03/2020", "09/02/2020", "09/01/2020", "08/31/2020",
"08/30/2020", "08/30/2020", "08/29/2020", "08/28/2020"), Month = c("09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "08", "08", "08", "08", "08"), Tm = c("NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM"), Opp = c("WSN", "WSN", "WSN", "WSN", "TBR", "TBR", "TBR",
"ATL", "ATL", "ATL", "PHI", "PHI", "PHI", "TOR", "TOR", "TOR",
"BAL", "BAL", "PHI", "PHI", "PHI", "PHI", "NYY", "BAL", "BAL",
"MIA", "NYY", "NYY", "NYY", "NYY"), Rslt = c("L 5-15", "L 3-4",
"L 3-5", "W 3-2", "L 5-8", "W 5-2", "L 1-2", "L 0-7", "W 7-2",
"L 2-15", "W 10-6", "W 5-4", "L 1-4", "L 3-7", "L 2-3", "W 18-1",
"W 7-6", "L 2-11", "L 8-9", "W 14-1", "W 5-1", "L 3-5", "W 9-7",
"W 9-4", "L 5-9", "L 3-5", "L 7-8", "L 2-5", "L 1-2", "W 4-3"
), W_L = c("L", "L", "L", "W", "L", "W", "L", "L", "W", "L",
"W", "W", "L", "L", "L", "W", "W", "L", "L", "W", "W", "L", "W",
"W", "L", "L", "L", "L", "L", "W"), temp = c("L 5", "L 3", "L 3",
"W 3", "L 5", "W 5", "L 1", "L 0", "W 7", "L 2", "W 10", "W 5",
"L 1", "L 3", "L 2", "W 18", "W 7", "L 2", "L 8", "W 14", "W 5",
"L 3", "W 9", "W 9", "L 5", "L 3", "L 7", "L 2", "L 1", "W 4"
), RS = c(5, 3, 3, 3, 5, 5, 1, 0, 7, 2, 10, 5, 1, 3, 2, 18, 7,
2, 8, 14, 5, 3, 9, 9, 5, 3, 7, 2, 1, 4), RA = c(15, 4, 5, 2,
8, 2, 2, 7, 2, 15, 6, 4, 4, 7, 3, 1, 6, 11, 9, 1, 1, 5, 7, 4,
9, 5, 8, 5, 2, 3), Rdiff = c(-10, -1, -2, 1, -3, 3, -1, -7, 5,
-13, 4, 1, -3, -4, -1, 17, 1, -9, -1, 13, 4, -2, 2, 5, -4, -2,
-1, -3, -1, 1), absV = c(10, 1, 2, 1, 3, 3, 1, 7, 5, 13, 4, 1,
3, 4, 1, 17, 1, 9, 1, 13, 4, 2, 2, 5, 4, 2, 1, 3, 1, 1), App_Dec = c("GS-2, L",
"GS-5", "GS-3, L", "GS-7, W", "GS-6, L", "GS-7, W", "GS-7, L",
"GS-7, L", "GS-6, W", "GS-3, L", "GS-2", "GS-2", "GS-6, L", "GS-5, L",
"GS-6, L", "GS-6, W", "GS-4", "GS-4, L", "GS-2", "GS-7, W", "GS-5, W",
"GS-6", "GS-2", "GS-3", "GS-4", "GS-6, L", "GS-5", "GS-4", "GS-4",
"GS-4"), IP = c(1.1, 5, 3, 7, 6, 6.1, 7, 7, 6, 2.2, 1.2, 2, 6,
5, 5.1, 6, 4, 4, 2, 7, 5, 6, 1.2, 3, 4, 6, 5, 3.2, 4, 4), H = c(5,
5, 8, 4, 6, 4, 4, 3, 3, 8, 8, 4, 6, 3, 7, 3, 10, 7, 3, 3, 4,
3, 4, 4, 9, 6, 4, 4, 4, 4), R = c(6, 3, 5, 1, 4, 2, 2, 1, 1,
6, 6, 3, 4, 2, 3, 1, 5, 5, 5, 1, 1, 2, 4, 2, 5, 4, 2, 1, 1, 3
), ER = c(6, 3, 3, 1, 4, 1, 2, 1, 1, 6, 6, 3, 4, 2, 3, 1, 5,
4, 5, 1, 1, 2, 4, 2, 5, 1, 2, 1, 1, 3), BB = c(2, 2, 1, 1, 0,
1, 2, 2, 4, 3, 0, 1, 2, 2, 1, 2, 0, 0, 4, 2, 2, 2, 4, 1, 0, 2,
2, 2, 0, 3), SO = c(1, 10, 3, 4, 4, 7, 14, 10, 10, 5, 3, 1, 5,
2, 5, 9, 3, 3, 3, 12, 8, 6, 0, 2, 2, 9, 2, 7, 4, 3), HR = c(0,
2, 1, 0, 2, 1, 1, 1, 1, 2, 4, 0, 1, 1, 0, 0, 0, 2, 1, 1, 1, 0,
0, 0, 1, 1, 0, 1, 1, 0), UER = c(0, 0, 2, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0),
Pit = c(38, 113, 67, 107, 66, 95, 112, 100, 102, 76, 52,
40, 94, 81, 91, 102, 66, 71, 70, 108, 81, 100, 52, 69, 84,
103, 86, 60, 57, 70), Str = c(24, 78, 42, 68, 45, 66, 70,
70, 62, 45, 30, 25, 66, 52, 60, 68, 45, 49, 37, 74, 50, 65,
22, 41, 53, 72, 55, 39, 33, 37), GSc = c(19, 53, 29, 68,
48, 65, 73, 75, 68, 20, 18, 36, 47, 53, 46, 69, 25, 33, 29,
77, 61, 62, 27, 44, 26, 57, 51, 54, 54, 42), BF = c(12, 22,
19, 26, 23, 24, 26, 26, 24, 18, 14, 11, 26, 20, 24, 23, 21,
20, 14, 26, 21, 23, 13, 15, 21, 27, 20, 16, 15, 18), AB = c(8,
20, 18, 24, 23, 23, 23, 23, 20, 15, 13, 9, 24, 18, 22, 21,
21, 20, 9, 24, 19, 21, 8, 13, 20, 25, 18, 14, 15, 15), H2B = c(2,
0, 1, 1, 1, 0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 1, 1, 1, 1, 1,
0, 0, 1, 0, 2, 2, 2, 0, 1, 0), H3B = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 1, 0), IBB = 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, 1, 0, 0),
HBP = c(1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), SH = 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, 1, 0, 0, 0, 0, 0), SF = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0), GDP = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1), SB = c(0, 1,
1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0,
1, 0, 0, 0, 3, 0, 0, 0, 0), CS = c(0, 0, 0, 0, 1, 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), PO = c(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), BK = 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), WP = c(0, 1, 1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0), ERA = c("40.5", "5.4", "9", "1.29", "6", "1.42",
"2.57", "1.29", "1.5", "20.25", "32.4", "13.5", "6", "3.6",
"5.0599999999999996", "1.5", "11.25", "9", "22.5", "1.29",
"1.8", "3", "21.6", "6", "11.25", "1.5", "3.6", "2.4500000000000002",
"2.25", "6.75"), WPA = c(-0.471, -0.087, -0.256, 0.34, -0.22,
0.18, 0.107, 0.219, 0.229, -0.358, -0.487, -0.186, -0.156,
0.036, -0.047, 0.049, -0.329, -0.321, -0.34, 0.193, 0.156,
0.07, -0.312, -0.042, -0.278, -0.271, 0.029, 0.02, 0.092,
-0.174), RE24 = c(-5.122, -0.193, -3.316, 2.931, -1.08, 1.509,
1.406, 2.406, 1.92, -4.641, -5.444, -1.919, -0.758, 0.679,
0.245, 2.215, -3.054, -3.054, -4.027, 2.406, 1.433, 0.92,
-3.788, -0.359, -2.812, -1.08, 0.707, 0.364, 1.166, -0.834
), aLI = c(1.45, 1.244, 0.974, 1.271, 0.965, 0.921, 0.955,
0.888, 1.066, 0.962, 0.767, 1.073, 0.941, 0.852, 1.353, 0.392,
0.857, 0.805, 0.904, 0.75, 1.037, 0.861, 1.232, 1.355, 0.914,
1.239, 1.213, 1.28, 0.748, 1.407)), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"))
Desired output:
The numbers starting in the second column are the total absV values for each player for each column. The last column contains the sum of all the absV values for each player where absV > 5. Only a sample of the first 3 rows are shown, and the absV values are just filler numbers.
| Player | 1 | 2 | 3 | 4 | 5 | >5 |
| deGrom | 2 | 3 | 5 | 0 | 1 | 3 |
| Matz | 2 | 3 | 5 | 0 | 1 | 3 |
Code tried (I need help getting beyond the point shown). I would prefer if the code uses dplyr:
starter %>%
select(Player, absV) %>%
group_by(Player, absV) %>%
summarize(numG= n()) %>%
arrange(Player,absV)
To do this you to bifurcate your data with rows per player >5 and <=5, then rbind them together and thereafter pivot_wider. Follow this code
library(dplyr)
library(tidyr)
df <- starter %>% group_by(Player) %>%
mutate(row = row_number()) %>%
select(Player, absV, row) %>% arrange(Player)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 5 4 1 1 NA
3 Jacob deGrom 1 1 1 17 13 2
4 Michael Wacha 3 9 5 NA NA NA
5 Rick Porcello 2 7 3 1 2 1
6 Robert Gsellman 2 1 NA NA NA NA
7 Seth Lugo 10 3 4 1 4 3
8 Steven Matz 13 NA NA NA NA NA
Note. Loading tidyverse package, at once, directly is advised.
Note-2 If you still want to sort absV before changing the data-format, add absV in arrange syntax beforehand joining them..
df <- starter %>% group_by(Player) %>%
arrange(Player, absV) %>%
mutate(row = row_number()) %>%
select(Player, absV, row)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
#this will give the following diff output
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 1 1 4 5 NA
3 Jacob deGrom 1 1 1 2 13 17
4 Michael Wacha 3 5 9 NA NA NA
5 Rick Porcello 1 1 2 2 3 7
6 Robert Gsellman 1 2 NA NA NA NA
7 Seth Lugo 1 3 3 4 4 10
8 Steven Matz 13 NA NA NA NA NA
Additional Question in comments below
Follow this code to work out frequency of each absV
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup()
#check it
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup() %>% select(Player, absV, freq)
Player absV freq
<chr> <dbl> <int>
1 Seth Lugo 10 1
2 Jacob deGrom 1 3
3 Rick Porcello 2 2
4 David Peterson 1 3
5 Michael Wacha 3 1
6 Seth Lugo 3 2
7 Jacob deGrom 1 3
8 Rick Porcello 7 1
9 David Peterson 5 1
10 Steven Matz 13 1
# ... with 20 more rows
Using data.table
library(data.table)
dcast(setDT(starter), Player ~ rowid(Player), value.var = 'absV')
In this timeseries data frame below, the day of month is a variable. I would like to reshape this dataset from wide to long but keep the right date format.
structure(list(Year = c(1994, 1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016),
Month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,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 0,
0, 0, 0, 0, 8.2, 0, 0, 0, 0, 1.4, 0, 0.6, 12.2, 0, 4.8, 0,
0, 0, 0.6, 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), day31 = c(0,
102, 0, 0, 2.4, 0, 0, 0, 2.4, 0, 47, 0, 0, 0, 25, 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, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), row.names = c(NA, 50L), class = "data.frame")
If I use melt function it will not consider the number of the days in the month putting NA values in the wrong dates such as 1994-02-30. I could remove the rows with NA values but I will need to be sure that my dataset there's no any NA value.
melt(Data,c("Year","Month")))
My desired output would be like this:
Data<-
Date Value
1994-01-01 0.1
1994-01-02 0
1994-01-03 12
You can get the data in long format, extract the data from column names, combine year, month and date values to create an actual date.
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = starts_with('day'),
values_drop_na = TRUE) %>%
mutate(name = readr::parse_number(name)) %>%
unite(Date, Year, Month, name, sep = '-') %>%
mutate(Date = as.Date(Date))
# A tibble: 1,487 x 2
# Date value
# <date> <dbl>
# 1 1994-01-01 0
# 2 1994-01-02 0
# 3 1994-01-03 0
# 4 1994-01-04 0
# 5 1994-01-05 0
# 6 1994-01-06 8.6
# 7 1994-01-07 0
# 8 1994-01-08 2
# 9 1994-01-09 0
#10 1994-01-10 0
# … with 1,477 more rows
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,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 0, 0, 0, 0, 0, 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
Example:
my_diamonds <- diamonds %>% fastDummies::dummy_cols(select_columns = "color", remove_selected_columns = T)
my_diamonds %>% glimpse
Looks like this:
Observations: 53,940
Variables: 16
$ carat <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.30, 0.23, 0.22, 0.31, 0.20, 0.32, 0.30, 0.30, 0.30, 0.30, 0.30, 0.23, 0.2…
$ cut <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Very Good, Fair, Very Good, Good, Ideal, Premium, Ideal, Premium, Premium, I…
$ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, SI1, SI2, SI2, I1, SI2, SI1, SI1, SI1, SI2, VS2, VS1, SI1, SI1, VVS2, VS1…
$ depth <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64.0, 62.8, 60.4, 62.2, 60.2, 60.9, 62.0, 63.4, 63.8, 62.7, 63.3, 63.8, 61.…
$ table <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58, 54, 54, 56, 59, 56, 55, 57, 62, 62, 58, 57, 57, 61, 57, 57, 57, 59, 58,…
$ price <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 342, 344, 345, 345, 348, 351, 351, 351, 351, 352, 353, 353, 353, 354, 355, …
$ x <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.25, 3.93, 3.88, 4.35, 3.79, 4.38, 4.31, 4.23, 4.23, 4.21, 4.26, 3.85, 3.9…
$ y <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.28, 3.90, 3.84, 4.37, 3.75, 4.42, 4.34, 4.29, 4.26, 4.27, 4.30, 3.92, 3.9…
$ z <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.73, 2.46, 2.33, 2.71, 2.27, 2.68, 2.68, 2.70, 2.71, 2.66, 2.71, 2.48, 2.4…
$ color_D <int> 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, …
$ color_E <int> 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
$ color_F <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ color_G <int> 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ color_H <int> 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 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, 0, 0, 0, 0, 1, …
$ color_I <int> 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, …
$ color_J <int> 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, …
Is there an out of the box, non custom function way to get my_diamonds back into it's original form with a single column for 'color'?
You can use pivot_longer :
library(dplyr)
tidyr::pivot_longer(my_diamonds, cols = starts_with('color'),
names_to = 'color',
names_pattern = '.*_(.*)') %>%
filter(value == 1) %>%
select(-value)
# A tibble: 53,940 x 10
# carat cut clarity depth table price x y z color
# <dbl> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>
# 1 0.23 Ideal SI2 61.5 55 326 3.95 3.98 2.43 E
# 2 0.21 Premium SI1 59.8 61 326 3.89 3.84 2.31 E
# 3 0.23 Good VS1 56.9 65 327 4.05 4.07 2.31 E
# 4 0.290 Premium VS2 62.4 58 334 4.2 4.23 2.63 I
# 5 0.31 Good SI2 63.3 58 335 4.34 4.35 2.75 J
# 6 0.24 Very Good VVS2 62.8 57 336 3.94 3.96 2.48 J
# 7 0.24 Very Good VVS1 62.3 57 336 3.95 3.98 2.47 I
# 8 0.26 Very Good SI1 61.9 55 337 4.07 4.11 2.53 H
# 9 0.22 Fair VS2 65.1 61 337 3.87 3.78 2.49 E
#10 0.23 Very Good VS1 59.4 61 338 4 4.05 2.39 H
# … with 53,930 more rows
Another option using max.col:
col <- "color"
my_diamonds$color <- my_diamonds %>%
select(starts_with(col)) %>%
{gsub(paste0(col,"_"), "", names(.))[max.col(.)]}
A base R option:
cols <- sub("color_", "", grep("^color_", names(my_diamonds), value=TRUE)); cols
[1] "D" "E" "F" "G" "H" "I" "J"
my_diamonds$color <- cols[
apply(my_diamonds[,grep("^color_", names(my_diamonds))], 1, which.max]
all(my_diamonds$color==diamonds$color)
#[1] TRUE
Or using the much quicker max.col (thanks #chinsoon12):
my_diamonds$color <- cols[max.col(my_diamonds[,grep("^color_", names(my_diamonds))])]
all(my_diamonds$color == diamonds$color)
#[1] TRUE
I used the fmi function from SemTools package just a few weeks ago, and it worked great! Here is the code that I saved and that worked fine:
dat.imp2 <- mice(data = dat1, m = 37, method = "pmm", seed = 444)
out <- fmi(dat.imp2$imputations)
out
I have used it to compare the loss of efficiency in using 4 source variables vs 1 composite, so I re-ran it twice - first with the 4 source variables and then with 1 composite, and it was much better for composite. Also, the output showed fmi for means and variances separately.
Come back to this code a few weeks later, and it doens't work! The error message reads:
Error in dim(robj) <- c(dX, dY) :
dims [product 0] do not match the length of object [1]
So, I modified the code as follows:
imp2 <- mice(dat1, m = 37, method = "pmm", seed = 444)
out <- fmi(imp2$data)
out
This works but only with the composite variable in the dataset, and only gives me fmi for means but not variances. If I substitute this composite variable with the four source variables it gives me the following error:
Warning message:
In lavaan(slotOptions = object#Options, slotParTable = object#ParTable, :
lavaan WARNING: model has NOT converged!
I don't understand how the code that worked a couple weeks ago does not work now? Did anyone come across this problem? I wasn't able to find much online.
Thank you!
Here is the dataset with one composite variable instead (mommh)
> dput(dat2)
structure(list(mompa = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,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 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), format.spss = "F8.2", display_width = 10L),
mommh = c(63, 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, 35.75, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 43.25, NA, NA, 63, 41.5,
34.25, 38.5, 39, 38.5, NA, 49.75, 57.5, 59.25, 50, 42.75,
45, 49, 32.75, NA, 35.75, 64.75, 50.5, 46.5, 39.75, 51.75,
34.75, 61.25, 46, 43, 56.25, 47, 42.25, 36.5, 34.5, 47, 50,
35, 48.25, 46.5, 58.5, 35.5, 55.25, 43.5, 42.75, 35.75, 38,
35.5, 50, 38.25, 57, 45.75, 38.5, 44.25, 51.75, NA, 38.25,
39.75, 34, 57.25, 39.25, 42.25, 37.25, NA, 32.75, 52.75,
NA, NA, 55.75, 62.25, 59.75, 43.75, 59.75, 35.75, NA, 34.25,
59.25, 39, 34.75, 32.75, NA, 53.5, NA, 40.5, 50, 33.5, 45.25,
41, 50, NA, 38.5, 61.5, 36.25, 46.25, 46, 44.75, 44.75, 62.5,
38.25, 49.5, 33.75, NA, 50.25, 43, 43.75, 42.25, 60.5, NA,
50.25, 54.75, 42.75, 45.75, 61, 58.25, 44.5, 46.5, 34.25,
56.75, 40.5, 47, 42.25, 48, 44, 36.75, 39.75, 48.75, 38.25,
49.25, 49.25, NA, NA, 34.25, 44.5, NA, 51, 44, 50.75, 56.25,
35, 55, 58.75, 56.5, 68.75, 54, 53, 41.5, 50.75, NA, 32.75,
46.75, 32.75, 43, 57, 55.25, NA, NA, 43.75, 55.5, NA, NA,
32.75, NA, NA, NA, NA, 60.5, 32.75, NA, 68.25, 50.5, 32.75,
66.5, 33, 38.5, 43, 43.75, 62.75, 47, 36.5, 39.5, 39.5),
risk6 = structure(c(0, 0, 0, 0, 3, 1, 1, 1, 1, 0, 1, 1, 0,
0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 0, 0, 2, 1, 1,
2, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 2, 1, 3, 2, 2, 0, 0, 0,
2, 0, 2, 2, 1, 2, 2, 1, 3, 2, 3, 1, 1, 0, 1, 3, 1, 2, 2,
0, 1, 0, 0, 1, 3, 1, 0, 1, 0, 0, 1, 3, 0, 1, 1, 0, 0, 2,
3, 3, 1, 2, 3, 2, 0, 0, 4, 1, 2, 1, 3, 2, 1, 2, 0, 1, 1,
2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1,
NA, 1, 1, 1, 2, 0, NA, 3, 0, 2, 2, 3, 4, 4, 0, 1, 0, 2, 3,
2, 2, 2, 2, 1, 3, 2, 2, 3, 1, 1, 1, 0, 0, 1, 1, 0, 2, 0,
1, 2, 3, 1, 1, 1, 2, 1, 2, 0, 0, 2, 2, 0, 1, 2, 0, 0, 2,
1, 1, 1, 1, 1, 3, 1, 0, 3, 0, 1, 0, 1, 1, 1, 2, 2, 0, 2,
3, 3, 0, 0, 0, 1, 2, 1, 1, 1, 0, 1, 1, 3, 1, 1, 0, 0, 3,
2, 0, 0, 3, 2, 1, 3, 1, 0, 3, 0, 1, 1, 2, 3, 3, 1, 4, 2,
3, 2, 2), format.spss = "F8.2", display_width = 10L), eadiff = structure(c(-1.26734803867686,
-0.355541076313792, 0.518653050779668, 1.50568568368194,
0.0940935989894723, 2.07356799670629, 1.01843817310907, -1.26734803867686,
-0.317928241044189, 0.531190662536203, 0.0940935989894723,
-1.47335895869369, -0.586627219843691, -1.26734803867686,
0.325179742519372, 0.556265886049271, 1.4179224013862, 1.2244490931259,
-0.586627219843691, 0.081555987232938, -0.149530156296961,
-0.380616299826861, -0.805175751617057, -0.368078688070326,
0.0940935989894723, -0.124454932783893, 0.955750114326398,
-0.805175751617057, 0.531190662536203, -0.830250975130125,
0.968287726082933, 0.749739194309568, -0.368078688070326,
-1.03626189514696, 3.19138587908619, -0.574089608087157,
1.67408376842917, -0.586627219843691, -0.343003464557258,
-0.162067768053496, 0.325179742519372, -1.24227281516379,
-1.03626189514696, 0.749739194309568, 0.325179742519372,
0.556265886049271, 0.762276806066102, -0.817713363373591,
-0.805175751617057, 0.119168822502541, -0.805175751617057,
-0.149530156296961, 0.0940935989894723, -1.48589657045022,
1.01843817310907, 0.312642130762837, 1.21191148136937, -0.355541076313792,
-1.04879950690349, -0.368078688070326, -0.124454932783893,
0.312642130762837, -1.25481042692032, -0.136992544540427,
1.01843817310907, -0.124454932783893, -0.368078688070326,
-0.805175751617057, 0.081555987232938, -0.805175751617057,
0.325179742519372, 2.97283734731282, 0.337717354275906, 0.0690183754764037,
-0.136992544540427, -0.830250975130125, 3.03552540609549,
0.0940935989894723, 0.0690183754764037, -0.124454932783893,
-0.817713363373591, -0.355541076313792, 0.312642130762837,
0.980825337839467, -0.343003464557258, 0.993362949596001,
-0.586627219843691, -0.574089608087157, -1.02372428339042,
-0.561551996330623, -0.111917321027358, -0.136992544540427,
-0.149530156296961, -0.830250975130125, 0.568803497805805,
0.0690183754764037, -0.805175751617057, -0.830250975130125,
0.556265886049271, 0.968287726082933, 0.531190662536203,
0.312642130762837, 0.337717354275906, 0.774814417822636,
0.337717354275906, 0.337717354275906, -0.586627219843691,
0.106631210746007, -1.02372428339042, -0.574089608087157,
-0.355541076313792, 0.737201582553033, 0.325179742519372,
0.312642130762837, 0.556265886049271, 0.0940935989894723,
0.300104519006303, -0.330465852800723, 0.0940935989894723,
-0.355541076313792, -0.599164831600226, 0.312642130762837,
0.531190662536203, -1.25481042692032, 0.531190662536203,
1.89263230020253, -0.817713363373591, -1.02372428339042,
0.980825337839467, -0.149530156296961, -0.586627219843691,
1.23698670488244, 0.556265886049271, 0.325179742519372, -0.817713363373591,
1.01843817310907, -1.02372428339042, -0.805175751617057,
-0.355541076313792, 1.67408376842917, 0.0690183754764037,
-0.368078688070326, -0.124454932783893, 0.980825337839467,
-1.03626189514696, 0.119168822502541, -1.03626189514696,
-1.03626189514696, 1.4555352366558, -0.136992544540427, -1.04879950690349,
0.749739194309568, -0.792638139860522, 0.312642130762837,
-0.0993797092708241, -0.17460537981003, -0.343003464557258,
-0.586627219843691, 0.300104519006303, -0.355541076313792,
-0.805175751617057, 0.518653050779668, -1.26734803867686,
-1.25481042692032, -0.368078688070326, -0.805175751617057,
-0.343003464557258, -0.343003464557258, -0.599164831600226,
-0.124454932783893, 1.66154615667263, -0.586627219843691,
-0.586627219843691, -0.124454932783893, 0.955750114326398,
-0.355541076313792, -0.343003464557258, 0.0940935989894723,
-0.792638139860522, -0.599164831600226, NA, -0.586627219843691,
-1.26734803867686, 0.762276806066102, 1.2244490931259, 0.081555987232938,
-0.574089608087157, -1.01118667163389, 0.312642130762837,
0.081555987232938, -0.368078688070326, -1.26734803867686,
1.63647093315956, -0.368078688070326, 0.531190662536203,
0.081555987232938, 0.543728274292737, 0.0564807637198694,
0.955750114326398, -1.25481042692032, 1.44299762489927, -1.04879950690349,
0.106631210746007, -0.586627219843691, 0.0940935989894723,
-0.162067768053496, 0.0940935989894723, -0.111917321027358,
0.968287726082933, 0.0940935989894723, 0.312642130762837,
-0.586627219843691, 0.543728274292737, -0.124454932783893,
0.543728274292737, -0.817713363373591, -0.586627219843691,
-0.368078688070326, 0.0940935989894723, -0.599164831600226,
-1.03626189514696, 0.774814417822636, 0.106631210746007,
-0.111917321027358, -0.817713363373591, -0.330465852800723,
0.993362949596001, -0.368078688070326, 1.19937386961283,
0.531190662536203, 0.749739194309568, 1.6490085449161, 0.0690183754764037,
-0.574089608087157, -0.368078688070326, 1.00590056135254,
1.4555352366558, -0.574089608087157, -0.586627219843691,
-0.817713363373591, -0.817713363373591, 0.0940935989894723,
-0.792638139860522, 0.0690183754764037), format.spss = "F8.2", display_width = 10L)), .Names = c("mompa",
"momabhx", "mommh", "risk6", "eadiff"), row.names = c(NA, -244L
), class = "data.frame")
And here is the same dataset with 4 source variables (depr, anxt, host, bpsipdr1)
> dput(dat3)
structure(list(mompa = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 0, 1, 0, 1,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1, 1, 0, 1, 3, 1, 2, 2,
0, 1, 0, 0, 1, 3, 1, 0, 1, 0, 0, 1, 3, 0, 1, 1, 0, 0, 2,
3, 3, 1, 2, 3, 2, 0, 0, 4, 1, 2, 1, 3, 2, 1, 2, 0, 1, 1,
2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1,
NA, 1, 1, 1, 2, 0, NA, 3, 0, 2, 2, 3, 4, 4, 0, 1, 0, 2, 3,
2, 2, 2, 2, 1, 3, 2, 2, 3, 1, 1, 1, 0, 0, 1, 1, 0, 2, 0,
1, 2, 3, 1, 1, 1, 2, 1, 2, 0, 0, 2, 2, 0, 1, 2, 0, 0, 2,
1, 1, 1, 1, 1, 3, 1, 0, 3, 0, 1, 0, 1, 1, 1, 2, 2, 0, 2,
3, 3, 0, 0, 0, 1, 2, 1, 1, 1, 0, 1, 1, 3, 1, 1, 0, 0, 3,
2, 0, 0, 3, 2, 1, 3, 1, 0, 3, 0, 1, 1, 2, 3, 3, 1, 4, 2,
3, 2, 2), format.spss = "F8.2", display_width = 10L), eadiff = structure(c(-1.26734803867686,
-0.355541076313792, 0.518653050779668, 1.50568568368194,
0.0940935989894723, 2.07356799670629, 1.01843817310907, -1.26734803867686,
-0.317928241044189, 0.531190662536203, 0.0940935989894723,
-1.47335895869369, -0.586627219843691, -1.26734803867686,
0.325179742519372, 0.556265886049271, 1.4179224013862, 1.2244490931259,
-0.586627219843691, 0.081555987232938, -0.149530156296961,
-0.380616299826861, -0.805175751617057, -0.368078688070326,
0.0940935989894723, -0.124454932783893, 0.955750114326398,
-0.805175751617057, 0.531190662536203, -0.830250975130125,
0.968287726082933, 0.749739194309568, -0.368078688070326,
-1.03626189514696, 3.19138587908619, -0.574089608087157,
1.67408376842917, -0.586627219843691, -0.343003464557258,
-0.162067768053496, 0.325179742519372, -1.24227281516379,
-1.03626189514696, 0.749739194309568, 0.325179742519372,
0.556265886049271, 0.762276806066102, -0.817713363373591,
-0.805175751617057, 0.119168822502541, -0.805175751617057,
-0.149530156296961, 0.0940935989894723, -1.48589657045022,
1.01843817310907, 0.312642130762837, 1.21191148136937, -0.355541076313792,
-1.04879950690349, -0.368078688070326, -0.124454932783893,
0.312642130762837, -1.25481042692032, -0.136992544540427,
1.01843817310907, -0.124454932783893, -0.368078688070326,
-0.805175751617057, 0.081555987232938, -0.805175751617057,
0.325179742519372, 2.97283734731282, 0.337717354275906, 0.0690183754764037,
-0.136992544540427, -0.830250975130125, 3.03552540609549,
0.0940935989894723, 0.0690183754764037, -0.124454932783893,
-0.817713363373591, -0.355541076313792, 0.312642130762837,
0.980825337839467, -0.343003464557258, 0.993362949596001,
-0.586627219843691, -0.574089608087157, -1.02372428339042,
-0.561551996330623, -0.111917321027358, -0.136992544540427,
-0.149530156296961, -0.830250975130125, 0.568803497805805,
0.0690183754764037, -0.805175751617057, -0.830250975130125,
0.556265886049271, 0.968287726082933, 0.531190662536203,
0.312642130762837, 0.337717354275906, 0.774814417822636,
0.337717354275906, 0.337717354275906, -0.586627219843691,
0.106631210746007, -1.02372428339042, -0.574089608087157,
-0.355541076313792, 0.737201582553033, 0.325179742519372,
0.312642130762837, 0.556265886049271, 0.0940935989894723,
0.300104519006303, -0.330465852800723, 0.0940935989894723,
-0.355541076313792, -0.599164831600226, 0.312642130762837,
0.531190662536203, -1.25481042692032, 0.531190662536203,
1.89263230020253, -0.817713363373591, -1.02372428339042,
0.980825337839467, -0.149530156296961, -0.586627219843691,
1.23698670488244, 0.556265886049271, 0.325179742519372, -0.817713363373591,
1.01843817310907, -1.02372428339042, -0.805175751617057,
-0.355541076313792, 1.67408376842917, 0.0690183754764037,
-0.368078688070326, -0.124454932783893, 0.980825337839467,
-1.03626189514696, 0.119168822502541, -1.03626189514696,
-1.03626189514696, 1.4555352366558, -0.136992544540427, -1.04879950690349,
0.749739194309568, -0.792638139860522, 0.312642130762837,
-0.0993797092708241, -0.17460537981003, -0.343003464557258,
-0.586627219843691, 0.300104519006303, -0.355541076313792,
-0.805175751617057, 0.518653050779668, -1.26734803867686,
-1.25481042692032, -0.368078688070326, -0.805175751617057,
-0.343003464557258, -0.343003464557258, -0.599164831600226,
-0.124454932783893, 1.66154615667263, -0.586627219843691,
-0.586627219843691, -0.124454932783893, 0.955750114326398,
-0.355541076313792, -0.343003464557258, 0.0940935989894723,
-0.792638139860522, -0.599164831600226, NA, -0.586627219843691,
-1.26734803867686, 0.762276806066102, 1.2244490931259, 0.081555987232938,
-0.574089608087157, -1.01118667163389, 0.312642130762837,
0.081555987232938, -0.368078688070326, -1.26734803867686,
1.63647093315956, -0.368078688070326, 0.531190662536203,
0.081555987232938, 0.543728274292737, 0.0564807637198694,
0.955750114326398, -1.25481042692032, 1.44299762489927, -1.04879950690349,
0.106631210746007, -0.586627219843691, 0.0940935989894723,
-0.162067768053496, 0.0940935989894723, -0.111917321027358,
0.968287726082933, 0.0940935989894723, 0.312642130762837,
-0.586627219843691, 0.543728274292737, -0.124454932783893,
0.543728274292737, -0.817713363373591, -0.586627219843691,
-0.368078688070326, 0.0940935989894723, -0.599164831600226,
-1.03626189514696, 0.774814417822636, 0.106631210746007,
-0.111917321027358, -0.817713363373591, -0.330465852800723,
0.993362949596001, -0.368078688070326, 1.19937386961283,
0.531190662536203, 0.749739194309568, 1.6490085449161, 0.0690183754764037,
-0.574089608087157, -0.368078688070326, 1.00590056135254,
1.4555352366558, -0.574089608087157, -0.586627219843691,
-0.817713363373591, -0.817713363373591, 0.0940935989894723,
-0.792638139860522, 0.0690183754764037), format.spss = "F8.2", display_width = 10L),
host = structure(c(68, 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, 38, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 41, 49, 41, 78, 41, 41,
49, 41, 45, 45, 51, 71, 73, 62, 51, 51, 65, 38, NA, 38, 70,
58, 45, 38, 64, 38, 72, 55, 45, 60, 58, 38, 38, 38, 38, 45,
38, 38, 51, 60, 38, 68, 51, 60, 38, 45, 38, 38, 38, 68, 45,
38, 51, 51, NA, 45, 38, 38, 66, 38, 45, 38, 65, 38, 51, NA,
NA, 60, 71, 70, 45, 71, 38, NA, 38, 55, 38, 38, 38, NA, 62,
58, 38, 58, 38, 51, 38, 72, 64, 45, 71, 45, 45, 51, 45, 45,
75, 38, 51, 38, 58, 55, 55, 38, 38, 70, 55, 65, 64, 55, 55,
69, 68, 55, 38, 38, 55, 45, 58, 38, 64, 38, 51, 45, 45, 38,
45, 62, 66, NA, 38, 45, 58, 58, 51, 65, 64, 38, 60, 60, 70,
75, 65, 62, 51, 62, NA, 38, 58, 38, 45, 38, 65, NA, 64, 38,
51, NA, NA, 38, NA, NA, NA, NA, 70, 38, NA, 75, 55, 38, 71,
38, 38, 55, 55, 58, 58, 45, 45, 45), format.spss = "F2.0", display_width = 11L),
anxt = structure(c(73, 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, 39, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 51, 51, 51, 66, 51, 40,
40, 51, 55, 62, 55, 55, 67, 51, 55, 55, 65, 39, NA, 39, 62,
59, 59, 39, 67, 39, 62, 51, 51, 65, 51, 51, 51, 39, 51, 59,
39, 59, 55, 71, 39, 53, 51, 51, 51, 51, 39, 55, 39, 65, 59,
51, 39, 65, NA, 39, 51, 39, 65, 51, 51, 39, 59, 39, 67, NA,
NA, 59, 70, 67, 39, 65, 39, NA, 39, 65, 51, 39, 39, NA, 62,
65, 55, 39, 39, 59, 39, 70, NA, 55, 67, 39, 51, 55, 51, 55,
70, 55, 56, 39, 70, 55, 51, 51, 51, 62, NA, 59, 62, 55, 59,
62, 59, 51, 51, 39, 65, 39, 55, 62, 51, 55, 39, 39, 62, 51,
55, 62, NA, NA, 39, 51, NA, 62, 39, 62, 59, 39, 59, 71, 51,
74, 59, 51, 51, 62, NA, 39, 51, 39, 51, 72, 62, NA, 62, 55,
62, NA, NA, 39, NA, NA, NA, NA, 70, 39, NA, 70, 65, 39, 73,
39, 51, 51, 55, 74, 62, 39, 51, 51), format.spss = "F2.0", display_width = 11L),
depr = structure(c(71, 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, 42, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 53, 68, NA, 71, 44, 44,
44, 44, 42, 42, 61, 64, 57, 70, 42, 50, 54, 42, NA, 50, 78,
57, 50, 42, 54, 42, 68, 42, 54, 64, 54, 54, 42, 42, 68, 64,
42, 65, 50, 68, 42, 70, 50, 42, 42, 42, 42, 50, 42, 60, 54,
42, 56, 64, NA, 42, 42, 42, 61, 42, 42, 42, 57, 42, 64, 57,
NA, 68, 73, 72, 57, 68, 42, 57, 42, 65, 50, 42, 42, NA, 62,
62, 42, 61, 42, 54, 54, 42, 71, 42, 64, 42, 54, 54, 57, 42,
70, 42, 54, 42, 57, 57, 54, 60, 54, 71, 60, 54, 60, 42, 42,
68, 70, 50, 60, 42, 69, 54, 42, 42, 57, 50, 42, 42, 54, 42,
65, 57, 68, NA, 42, 60, 64, 50, 60, 50, 70, 42, 65, 64, 65,
71, 64, 62, 42, 62, NA, 42, 42, 42, 57, 70, 61, NA, 57, 42,
68, NA, NA, 42, NA, NA, NA, NA, 65, 42, NA, 75, 61, 42, 75,
42, 42, 54, 50, 72, 42, 50, 42, 42), format.spss = "F2.0", display_width = 11L),
bpsipdr1 = structure(c(40, 26, 34, 29, 23, 41, 37, 21, 38,
NA, 33, 28, 25, NA, NA, 15, 18, 30, NA, NA, 28, 34, NA, 51,
24, 28, 23, 12, 39, 55, 28, NA, 26, 18, 33, NA, 27, 32, 27,
23, 28, 41, NA, 22, 21, 26, 26, 36, 16, 24, 24, 25, 23, 24,
26, 35, 32, 27, 38, 25, 26, 32, 27, 41, 28, NA, 27, 37, 30,
12, 21, 20, 12, NA, 32, 40, 40, 17, 23, 24, 12, 12, NA, 16,
49, 28, 32, 40, 22, 20, 43, 36, 22, 36, 25, 26, 15, 19, 31,
32, 21, 31, 30, 35, 23, 30, 22, 18, 12, 14, 23, 57, 34, 35,
25, 23, 31, 27, 33, 27, 28, 17, 37, 26, 31, 30, NA, 12, 29,
NA, 19, 36, 35, 30, 34, 35, 24, 34, 18, 52, 17, 20, 12, 25,
28, NA, 27, 42, 15, 17, 33, 16, 30, 12, 44, 19, 35, 24, 26,
37, 35, 18, 37, 16, NA, 34, 12, 26, 26, 39, 34, 23, 33, 19,
27, 45, 36, 22, 37, 18, 38, 24, 33, 27, 20, 33, 15, 33, 34,
22, 32, 16, 30, 24, 18, 22, 42, 34, 26, 26, 32, 21, 36, 40,
40, 55, 28, 37, 22, 17, 41, 12, 36, 12, 19, 48, 33, 26, NA,
40, 41, 24, 27, 12, 36, 24, 38, NA, 37, 12, 20, 53, 21, 12,
47, 13, 23, 12, 15, 47, 26, 12, 20, 20), format.spss = "F3.0", display_width = 11L)), .Names = c("mompa",
"momabhx", "risk6", "eadiff", "host", "anxt", "depr", "bpsipdr1"
), row.names = c(NA, -244L), class = "data.frame")