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",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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
Related
I have an unbalanced panel data set of workers between 2003-2021. It's set by PersonID using the plm package.
I would like to generate a dummy variable for whether the worker enrolled in a school after job loss.
The coding criteria for this is:
Per group (aka PersonID):
1.) If after PermSeparation==1, any value in the TimeID_PSE column is MORE than any value in the LayoffTimeID column, then Enroll_Post_Disp==1 (on the same row corresponding with TimeID_PSE). Anything not fitting this criteria gets a zero.
I've denoted in red what I want the output to look like.
So for example, TimeID_PSE==20170821 gets a 1 in the Enroll_Post_Disp column because 20170821 > 20151231. Same for TimeID_PSE==20180108.
So far I've tried the following which gets me close, but it only assigns the Enroll_Post_Disp dummy if the TimeID_PSE value is on the same row as LayoffTimeID, (and not if the value in TimeID_PSE is greater than any value in LayoffTimeID).
test <- df[1:1000, ] %>% #first 1,000 rows for faster computations
group_by(PersonID) %>%
rowwise() %>%
mutate(Enroll_Post_Disp = ifelse(TimeID_PSE > LayoffTimeID, 1, 0))
The panel data looks like this (first 50 rows):
structure(list(First.PersonID = 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),
PersonID = c(1534, 1534, 1534, 1534, 1534, 1534, 3345, 3345,
3345, 3743, 3743, 3743, 3743, 3743, 3743, 3743, 3743, 4910,
4910, 4910, 4910, 4910, 4910, 4910, 5062, 5062, 5062, 5062,
5062, 5062, 5062, 7255, 7255, 7255, 7255, 7255, 7255, 7255,
7255, 7255, 7255, 7255, 7255, 10118, 10118, 10118, 10118,
10118, 10118, 10118), CalendarYear = c(2016, 2017, 2018,
2019, 2020, 2021, 2012, 2013, 2014, 2008, 2009, 2010, 2011,
2012, 2013, 2014, 2015, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2010, 2011,
2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021,
2008, 2009, 2010, 2011, 2012, 2013, 2014), AcademicYear = c(2015,
2016, 2017, 2018, 2019, 2020, 2011, 2012, 2013, 2007, 2008,
2009, 2010, 2011, 2012, 2013, 2014, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2014, 2015, 2016, 2017, 2018, 2019, 2020,
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
2019, 2020, 2007, 2008, 2009, 2010, 2011, 2012, 2013), LayoffCalendarYear = c(2016,
NA, NA, NA, NA, NA, 2012, 2013, NA, 2008, NA, NA, NA, NA,
NA, NA, NA, 2014, NA, NA, NA, NA, NA, NA, 2015, NA, NA, NA,
NA, NA, NA, 2010, 2011, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 2008, NA, NA, NA, NA, NA, NA), LayoffCalendarQuarter = c(1,
NA, NA, NA, NA, NA, 4, 1, NA, 4, NA, NA, NA, NA, NA, NA,
NA, 1, NA, NA, NA, NA, NA, NA, 4, NA, NA, NA, NA, NA, NA,
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4, NA, NA,
NA, NA, NA, NA), LayoffTimeID = c(20160331, NA, NA, NA, NA,
NA, 20121231, 20130331, NA, 20081231, NA, NA, NA, NA, NA,
NA, NA, 20140331, NA, NA, NA, NA, NA, NA, 20151231, NA, NA,
NA, NA, NA, NA, 20100331, 20110331, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 20081231, NA, NA, NA, NA, NA, NA), Age = c(47,
48, 49, 50, 51, 52, 28, 29, 30, 43, 44, 45, 46, 47, 48, 49,
50, 35, 36, 37, 38, 39, 40, 41, 33, 34, 35, 36, 37, 38, 39,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 28, 29, 30,
31, 32, 33, 34), AGEatLayoff = c(47, 47, 47, 47, 47, 47,
28, 29, 29, 43, 43, 43, 43, 43, 43, 43, 43, 35, 35, 35, 35,
35, 35, 35, 33, 33, 33, 33, 33, 33, 33, 44, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 28, 28, 28, 28, 28, 28, 28),
Time_To_Layoff = c(0, 1, 2, 3, 4, 5, 0, 1, 2, 0, 1, 2, 3,
4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6),
PermSeparation = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), WFTFLAG = 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), Enroll_Post_Disp = c(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, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TimeID_PSE = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
20121229, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), EnrollmentStatus_PSE = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), EnrollmentStatus_NSCE = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), EnrollmentBeginTimeID_NSCE = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), EnrollmentEndTimeID_NSCE = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), DegreeLevel1_PSE = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Associate Degree",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), Major1_PSE = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 4.4e+07, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Industry_of_Disp = c("Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Professional, Scientific, and Technical Services",
"Professional, Scientific, and Technical Services", "Professional, Scientific, and Technical Services",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Retail Trade", "Retail Trade", "Retail Trade", "Retail Trade",
"Retail Trade", "Retail Trade", "Retail Trade", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Health Care and Social Assistance", "Health Care and Social Assistance",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Administrative and Support and Waste Management and Remediation Services",
"Retail Trade", "Retail Trade", "Retail Trade", "Retail Trade",
"Retail Trade", "Retail Trade", "Retail Trade"), Gender = c("Female",
"Female", "Female", "Female", "Female", "Female", "Female",
"Female", "Female", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Female", "Female", "Female", "Female",
"Female", "Female", "Female", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male", "Male"), RaceEthnicity_PSE = c("White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", "White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", "White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", NA, NA, NA,
NA, NA, NA, NA, NA, "White (Not Hispanic)", "White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", "White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), RaceEthnicity_PSC = c("White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", "White (Not Hispanic)",
"White (Not Hispanic)", "White (Not Hispanic)", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), DisablingCondition_PSE = c("Not disabled",
"Not disabled", "Not disabled", "Not disabled", "Not disabled",
"Not disabled", "Not disabled", "Not disabled", "Not disabled",
NA, NA, NA, NA, NA, NA, NA, NA, "Not disabled", "Not disabled",
"Not disabled", "Not disabled", "Not disabled", "Not disabled",
"Not disabled", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), DisablingCondition_NSCE = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
NA, NA, NA, NA, NA, NA, NA, NA, 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, NA, NA, NA), County = c(37, 147,
147, 147, 147, 147, 175, 175, 37, 109, 109, 109, 109, 109,
109, 109, 53, 59, 59, 175, 175, 175, 53, 53, 175, 175, 175,
175, 175, 175, 53, 53, 53, 53, 53, 19, 19, 19, 3, 171, 171,
171, 171, 169, 169, 169, 169, 169, 169, 109), IndustryID = c(930,
930, 930, 930, 930, 930, 818, 984, 818, 909, 909, 909, 909,
909, 909, 909, 852, 602, 602, 602, 602, 602, 936, 936, 933,
933, 933, 933, 933, 933, 893, 852, 852, 852, 852, 874, 874,
874, 874, 874, 874, 874, 874, 1323, 985, 985, 985, 985, 985,
985), EmployeeAnnualWages_adj_win = c(49365.3976993575, 62004.973813953,
31603.0562689936, 70469.9915410537, 85031.6247083522, 18709,
74075.0580319728, 10487.254105911, 37418.7884393328, 54957.3732496958,
55416.8801363691, 56982.2654087984, 55468.1410064036, 55836.8942767655,
56643.5019072261, 49126.6830876089, 1095.28671905025, 14394.4833009456,
14710.9125407302, 10947.8847682257, 15109.0902656239, 14310.1867507725,
8261.03411351222, 2771.01803120932, 44740.9761759846, 46150.0139910037,
47082.7021212652, 50664.6005541642, 53111.6446489586, 55219.3649830863,
1433, 25700.418179581, 28820.110272923, 32736.1643744376,
25837.7745624004, 15384.6406015673, 7285.14297890206, 7966.20345721364,
17352.0185761998, 30722.4957992982, 33843.3178399339, 37278.4103103,
11076, 10577.8278303448, 17358.0287469667, 30199.6066454611,
36439.8568547162, 41696.5047115589, 42388.6994374014, 7185.22240000282
), Avg_Quarterly_Wages = c(12341.3494248394, 15501.2434534883,
7900.7640672484, 17617.4978852634, 21257.9061770881, 4677.25,
18518.7645079932, 2621.81352647776, 9354.69710983321, 13739.3433124239,
13854.2200340923, 14245.5663521996, 13867.0352516009, 13959.2235691914,
14160.8754768065, 12281.6707719022, 273.821679762562, 3598.6208252364,
3677.72813518255, 2736.97119205643, 3777.27256640598, 3577.54668769313,
2065.25852837805, 692.75450780233, 11185.2440439962, 11537.5034977509,
11770.6755303163, 12666.1501385411, 13277.9111622396, 13804.8412457716,
358.25, 6425.10454489525, 7205.02756823076, 8184.0410936094,
6459.44364060011, 3846.16015039182, 1821.28574472552, 1991.55086430341,
4338.00464404994, 7680.62394982455, 8460.82945998348, 9319.602577575,
2769, 2644.4569575862, 4339.50718674168, 7549.90166136526,
9109.96421367905, 10424.1261778897, 10597.1748593503, 1796.3056000007
), Avg_Quarterly_WageRates = c(45.2785357747047, 40.2694463895401,
19.8482018563467, 41.3965259033139, 45.5764589637828, 11.8112373737374,
36.3798804185651, 34.6415856520052, 28.3483906141643, 25.2240608746217,
26.3289174680879, 27.1358694383627, 26.8492376700108, 27.285188591429,
27.5840964510407, 27.2406362366645, 4.84833010748691, 14.2956923945638,
14.5797747785242, 10.292498118163, 13.8365850400032, 13.1546591276055,
7.57076927514013, 3.9813477459904, 21.3658022913431, 22.1993252685404,
22.6259885712124, 24.0645657698337, 24.8602406618609, 25.5251249251744,
6.39732142857143, 13.2521132049609, 15.6711847053711, 13.5225496732746,
13.1810833307802, 12.1015683404843, 12.439570986205, 12.4299493894196,
12.1597887709879, 15.1391311684269, 15.6351512899473, 15.7836076162657,
4.24042879019907, 9.65574107491348, 10.4518986806169, 14.6630228730063,
16.1266695491898, 17.8190191075038, 18.1148288194023, 7.92613015332173
), Avg_Quarterly_WorkHours = c(273.25, 385.5, 199, 425.25,
465.5, 99, 509, 73, 165, 544.5, 526.25, 525, 516, 511.5,
513.25, 450.5, 30, 251.75, 252, 199.5, 273, 273, 136.5, 43.5,
522.75, 520, 520.5, 527, 534.25, 540.75, 14, 474.5, 491.75,
604.5, 488.5, 318, 146.5, 160.25, 356.75, 507.25, 541.75,
590, 163.25, 276, 311, 514.5, 562.75, 585, 585, 141.5), EmployeeQuarterlyWages_Qtr1_adj_win = c(14333.7470369592,
15932.6395960226, 0, 19299.0645546357, 22347.1559925294,
18709, 17675.1918978829, 2354.28153398003, 28800.4135071007,
12546.6154926867, 12689.2304728644, 13383.2529594018, 12286.7660692029,
12910.5852144058, 12840.372457315, 12852.585169226, 456.178915345562,
3067.77059614586, 3018.32665792552, 0, 4013.83572976883,
3612.88780948544, 0, 2771.01803120932, 10053.085720886, 10453.3840434157,
12531.2150461745, 12668.8479340977, 13322.1629166968, 13888.5894295633,
1433, 1640.1349834194, 6272.88860891654, 7499.21002398156,
6748.78530639929, 4036.17877686951, 1966.48554986057, 1615.59483379715,
3477.69958850255, 7405.98836215608, 8935.14467242853, 9233.23726303976,
11076, 3417.68446499895, 0, 6358.00811375537, 7093.29136341474,
10204.3075019428, 11288.6869008282, 0), EmployeeQuarterlyWages_Qtr2_adj_win = c(13281.5217503772,
14768.6161800362, 0, 15893.5343253933, 17149.5343359543,
0, 18555.6468361722, 3911.78300334726, 8618.37493223215,
13474.362620018, 14755.2715809065, 15312.8966179248, 15214.1944002318,
15208.5017892708, 15452.8805231842, 15000.0245865281, 639.107803704684,
3671.02250068648, 4118.18659918475, 3230.0606705057, 3408.05716190507,
3323.68412581098, 0, 0, 12074.4499372543, 12195.4265511369,
10775.7837217809, 11992.2408084862, 12011.0390780498, 12253.4061410295,
0, 5040.93002100948, 6753.56511265337, 8845.85759045354,
5724.02145687537, 3605.77514099233, 1593.76793982886, 1691.23763873385,
3647.93663129638, 7105.99349625496, 7864.62318735702, 7914.20336831979,
0, 2396.78498024236, 3149.5761145824, 7755.85042916963, 9711.11101910432,
10127.5922593305, 9825.40322012319, 7070.75334790782), EmployeeQuarterlyWages_Qtr3_adj_win = c(13931.824073415,
17861.6242692385, 14822.7679065388, 18617.5345398822, 22484.2936434884,
0, 17973.7912268202, 3197.58890163592, 0, 14230.910820189,
13310.5582388526, 13088.7741782879, 12901.1646078139, 12903.5038073954,
12854.3306482279, 15073.2847798689, 0, 3730.54640777588,
3718.02965589917, 4011.326655822, 4118.85208733646, 3739.14464153735,
3672.63063937899, 0, 10053.085720886, 12495.7397767064, 12657.2346752556,
14432.1271099338, 15478.0447985141, 14490.5295005665, 0,
9284.15802735589, 8496.77037808495, 8758.52023732565, 5707.73690081029,
4141.49030479691, 1863.58805015856, 1941.87499240469, 4985.51339610505,
8311.36855097277, 9030.53767604877, 9233.23726303976, 0,
2219.29197487777, 6588.34746983815, 7425.33837948057, 8918.41643399445,
11395.1641141867, 11427.1056273813, 114.469052094995), EmployeeQuarterlyWages_Qtr4_adj_win = c(7818.30483860607,
13442.0937686557, 16780.2883624548, 16659.8581211425, 23050.6407363801,
0, 19870.4280710975, 1023.60066694784, 0, 14705.4843168021,
14661.8198437456, 15197.3416531839, 15066.015929155, 14814.3034656935,
15495.918278499, 6200.78855198586, 0, 3925.14379633737, 3856.36962772075,
3706.49744189801, 3568.34528661355, 3634.47017393876, 4588.40347413323,
0, 12560.3547969583, 11005.4636197447, 11118.4686780542,
11571.3847016465, 12300.3978556979, 14586.839911927, 0, 9735.19514779623,
7296.88617326819, 7632.57652267686, 7657.23089831549, 3601.19637890853,
1861.30143905407, 2717.49599227795, 5240.8689602958, 7899.1453899144,
8013.01230409961, 10897.7324159007, 0, 2544.06641022574,
7620.10516254618, 8660.40972305548, 10717.0380382027, 9969.44083609888,
9847.50368906866, 0), EmployeeQuarterlyHoursWorked_Qtr1_win = c(331,
367, 0, 460, 463, 396, 491, 57, 508, 506, 489, 494, 468,
477, 468, 469, 40, 214, 211, 0, 294, 294, 0, 174, 480, 480,
560, 560, 560, 560, 56, 132, 491, 571, 487, 341, 162, 130,
286, 502, 586, 588, 653, 375, 0, 465, 471, 585, 585, 0),
EmployeeQuarterlyHoursWorked_Qtr2_win = c(289, 353, 0, 412,
404, 0, 516, 101, 152, 529, 560, 563, 561, 556, 558, 552,
80, 258, 280, 252, 252, 252, 0, 0, 571, 560, 480, 480, 487,
482, 0, 389, 290, 640, 456, 296, 128, 136, 300, 474, 497,
504, 0, 259, 227, 557, 610, 585, 585, 560), EmployeeQuarterlyHoursWorked_Qtr3_win = c(302,
450, 397, 431, 481, 0, 488, 87, 0, 568, 496, 481, 480, 472,
466, 552, 0, 261, 252, 294, 294, 294, 241, 0, 480, 560, 561,
588, 609, 560, 0, 672, 634, 634, 456, 340, 148, 156, 410,
549, 580, 588, 0, 222, 474, 478, 540, 585, 585, 6), EmployeeQuarterlyHoursWorked_Qtr4_win = c(171,
372, 399, 398, 514, 0, 541, 47, 0, 575, 560, 562, 555, 541,
561, 229, 0, 274, 265, 252, 252, 252, 305, 0, 560, 480, 481,
480, 481, 561, 0, 705, 552, 573, 555, 295, 148, 219, 431,
504, 504, 680, 0, 248, 543, 558, 630, 585, 585, 0), WageRate_Qtr1_win = c(43.3043717128677,
43.4131869101433, 0, 41.9544881622515, 48.2659956642104,
47.2449494949495, 35.9983541708409, 41.3031848066672, 56.6937273761825,
24.7956827918709, 25.9493465702748, 27.0916051809753, 26.253773652143,
27.0662163824021, 27.4366932848611, 27.4042327702047, 11.4044728836391,
14.335376617504, 14.3048656773721, 0, 13.6525024822069, 12.2887340458688,
0, 15.9253909839616, 20.9439285851792, 21.7778834237827,
22.3771697253116, 22.6229427394602, 23.7895766369586, 24.8010525527916,
25.5892857142857, 12.4252650259045, 12.7757405476915, 13.1334676426998,
13.8578753724831, 11.8363013984443, 12.1387996904973, 12.4276525676704,
12.1597887709879, 14.7529648648527, 15.2476871543149, 15.702784460952,
16.9617151607963, 9.1138252399972, 0, 13.6731357285062, 15.060066588991,
17.4432606870817, 19.2969006851764, 0), WageRate_Qtr2_win = c(45.9568226656651,
41.837439603502, 0, 38.576539624741, 42.4493424157285, 0,
35.9605558840546, 38.7305247856164, 56.6998350804747, 25.4713849149679,
26.3486992516188, 27.1987506535076, 27.1197761144952, 27.3534204843,
27.6933342709394, 27.17395758429, 7.98884754630855, 14.2287693825057,
14.7078092828027, 12.8177010734353, 13.5240363567662, 13.1892227214721,
0, 0, 21.1461470004454, 21.7775474127445, 22.4495494203769,
24.9838350176796, 24.6633245955848, 25.4220044419699, 0,
12.9586890000244, 23.2881555608737, 13.8216524850837, 12.5526786334986,
12.1816727736227, 12.451312029913, 12.435570873043, 12.1597887709879,
14.9915474604535, 15.8241915238572, 15.702784460952, 0, 9.25399606271181,
13.8747846457374, 13.9243275209509, 15.9198541296792, 17.3121235202231,
16.7955610600396, 12.6263452641211), WageRate_Qtr3_win = c(46.1318677927649,
39.6924983760856, 37.3369468678559, 43.1961358233926, 46.7448932297056,
0, 36.8315393992217, 36.7538954211025, 0, 25.0544204580792,
26.8358029009125, 27.2115887282493, 26.877426266279, 27.3379317953292,
27.5844005326779, 27.3066753258495, 0, 14.2932812558463,
14.7540859361078, 13.6439682170816, 14.0097009773349, 12.7181790528481,
15.2391312837302, 0, 20.9439285851792, 22.3138210298329,
22.5619156421668, 24.5444338604316, 25.4155087003516, 25.8759455367259,
0, 13.815711350232, 13.401846022216, 13.8147006897881, 12.5169668877419,
12.180853837638, 12.59181114972, 12.4479166179788, 12.1597887709879,
15.13910482873, 15.5698925449117, 15.702784460952, 0, 9.99681069764761,
13.8994672359455, 15.5341807102104, 16.5155859888786, 19.4789130157038,
19.5335138929595, 19.0781753491658), WageRate_Qtr4_win = c(45.7210809275209,
36.1346606684293, 42.0558605575308, 41.8589400028706, 44.8456045454866,
0, 36.7290722201433, 21.7787375946349, 0, 25.5747553335689,
26.1818211495457, 27.0415331907187, 27.1459746471261, 27.3831857036848,
27.6219577156845, 27.0776792663138, 0, 14.3253423223992,
14.5523382178142, 14.708323182135, 14.1601003437046, 14.4225006902332,
15.0439458168303, 0, 22.4292049945684, 22.9280492078015,
23.1153194969942, 24.1070514617635, 25.5725527145486, 26.0014971692103,
0, 13.8087874436826, 13.2189966907032, 13.3203778755268,
13.7968124293973, 12.2074453522323, 12.5763610746897, 12.4086574989861,
12.1597887709879, 15.6729075196714, 15.8988339367056, 16.0260770822069,
0, 10.2583322992973, 14.0333428407849, 15.5204475323575,
17.0111714892106, 17.0417792070066, 16.8333396394336, 0)), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
I would work with some auxiliary variables.
df <- df %>%
group_by(PersonID) %>%
mutate(LayoffTimeID_max = max(LayoffTimeID, na.rm = TRUE),
PermSeparation_max = max(PermSeparation, na.rm = TRUE),
Enroll_Post_Disp = case_when(PermSeparation_max == 1 & TimeID_PSE > LayoffTimeID_max ~ 1,
TRUE ~ 0)) %>%
select(-c(LayoffTimeID_max,PermSeparation_max))
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"))
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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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, 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), day1 = c(0,
0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 7.4, 0, 0, 28.2, 0,
0, 0, 0, 0, 0, 0, 0, 0,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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 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, 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, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0), format.spss = "F8.2", display_width = 10L), momabhx = structure(c(1,
0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
0, 1, 0, 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,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 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),
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),
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")