sem.mi or runMI - r

I am running a path analysis in lavaan (with ordinal) and would like to use imputed data.
But whether I impute data separately and use runMI or let the original data be imputed as a part of sem.mi command, I get same error:
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
If I run:
options(expressions = 100000)
the error message changes to: Error: protect(): protection stack overflow
I tried to change
--max-ppsize=500000
but in the command line I can't access rstudio.exe (says: the system cannot find the path specified, - even though I double-checked the path:
C:\Program Files\RStudio\bin\rstudio.exe --max-ppsize=500000)
What can I do to run my analysis with imputed data or to impute it as a part of the path analyses estimation?
Here is my code:
imp <- mice(dat2,m=5,print=F)
imputedData <- NULL
for(i in 1:5) {
imputedData[[i]] <- complete(x=imp, action=i, include=FALSE)
}
model5 <- 'ceadiff ~ mompa + cdpea + momabhx
mompa ~ b1*peadiff + c*momabhx + cdpea + b2*mommhpsi
peadiff ~ a1*momabhx + mommhpsi
cdpea ~ momabhx + mommhpsi
mommhpsi ~ a2*momabhx
peadiff ~~ cdpea
direct := c
indirect1 := a1 * b1
indirect1 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)'
fit5 <- runMI(model5, data = imputedData, fun="sem", ordered = "mompa")
summary(fit5, standardized = TRUE, fit = TRUE, ci = T)
# or:
fit5 <- sem.mi(model5, data = dat2, m=5, ordered = "mompa")
summary(fit5, standardized = TRUE, fit = TRUE, ci = T)
P.S. It prints summary with a warning in this scenario but doesn't print p-values or CIs, so I cannot determine what coefficients are sig.:
fit5 <- sem.mi(model5, data = dat2, m=5, ordered = "mompa")
summary(fit5)
** WARNING ** lavaan (0.5-23.1097) model has NOT been fitted
** WARNING ** Estimates below are simply the starting values
Thank you!
P.S. I don't know how to supply my data sample.
Here is the unimputed data output:
> dput(dat2)
structure(list(id = structure(c(145, 253, 189, 305, 149, 567,
151, 853, 272, 67, 111, 695, 1695, 1301, 2322, 1335, 1490, 580,
209, 1109, 1317, 812, 1459, 2150, 685, 1583, 839, 2156, 1627,
1103, 649, 2294, 1712, 1711, 793, 1425, 1114, 146, 1529, 985,
1889, 1974, 444, 1664, 1569, 859, 1947, 1219, 1427, 1533, 2143,
769, 256, 147, 1393, 1847, 1967, 1651, 1084, 1343, 996, 1765,
1596, 2157, 978, 1448, 915, 1411, 1412, 675, 1876, 53, 400, 2103,
1028, 663, 1090, 360, 2134, 1937, 1061, 1823, 935, 891, 1968,
34, 487, 207, 295, 1118, 1164, 1053, 1511, 777, 1760, 38, 480,
459, 307, 1962, 199, 499, 1375, 782, 1855, 1624, 109, 1481, 483,
536, 972, 1151, 19, 403, 543, 502, 2251, 254, 429, 2118, 1272,
1995, 982, 1748, 1641, 1994, 1718, 510, 494, 273, 602, 549, 293,
1796, 1497, 1197, 1874, 1179, 159, 205, 242, 299, 100, 1200,
579, 870, 1482, 2131, 33, 1319, 148, 1297, 626, 1051, 1948, 1057,
1581, 1349, 1284, 1178, 1178, 1044, 1001, 547, 276, 507, 871,
698, 1006, 1946, 2101, 68, 265, 1186, 1895, 1864, 1884, 1553,
1761, 2171, 168, 30, 1132, 1983, 1897, 1383, 1353, 1697, 1752,
505, 1605, 1144, 1358, 1052, 1645, 1346, 14, 439, 2154, 932,
971, 2104, 1345, 1821, 52, 1642, 1661, 1835, 1232, 2132, 809,
606, 54, 528, 59, 1848, 232, 1750, 2340, 882, 716, 2105, 711,
2109, 2353, 41, 2144, 552, 304, 2404, 1527, 1980, 927, 1586,
1805, 1982, 1181, 2163, 861, 198, 1404, 986, 1404, 238, 2115,
1125), format.spss = "F4.0", display_width = 11L), peadiff = structure(c(4,
7, 2, 2, 3, 4, 5, 5, 2, 6, 2, 6, 4, 3, 4, 5, 2, 3, 2, 1, 1, 3,
3, 3, 3, 5, 6, 3, 2, 2, 2, 4, 2, 2, 3, 5, 2, 4, 6, 2, 2, 3, 2,
1, 7, 7, 2, 5, 6, 4, 4, 4, 2, 9, 3, 4, 6, 7, 3, 3, 4, 3, 7, 5,
7, 4, 1, 1, 6, 14, 6, 2, 4, 3, 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, 3, 4, 6, 4,
2, 6, 2, 3, 4, 2, 7, 4, 7, 4, 3, 0, 3, 0, 2, 2, 1, 3, 5,
1, 4, 2, 1, 2, 7, 4, 4, 4, 8, 6, 2, 6, 1, 1, 5, 3, 0, 5,
8, 4, 8, 3, 0, 3, 4, 5, 5, 2, 6, 0, 6, NA, 4, 4, 1, 3, 12,
2, 0, 4, 0, 5, 4, 3, 2, 1, 1, 5, 5, 6, 3, 1, 2, 1, 4, 2,
8, 6, 3, 0, 1, 3), format.spss = "F8.2", display_width = 10L),
cdpea = structure(c(22, 18, 17, 13, 19, 20, 19, 20, 17, 17,
17, 14, 17, 15, 21, 12, 16, 15, 14, 17, 19, 18, 17, 18, 19,
16, 18, 15, 16, 18, 17, 19, 18, 15, 16, 18, 18, 17, 22, 18,
18, 12, 19, 16, 15, 17, 14, 17, 15, 19, 17, 18, 14, 17, 19,
20, 16, 6, 12, 17, 17, 16, 13, 20, 18, 16, 16, 18, 21, 17,
21, 13, 17, 14, 18, 15, 18, 17, 23, 19, 17, 18, 15, 17, 19,
15, 21, 17, 20, 16, 15, 18, 15, 18, 17, 18, 16, 18, 21, 16,
19, 21, 18, 16, 19, 18, 18, 18, 18, 18, 19, 20, 20, 22, 14,
19, 18, 16, 22, 14, 16, 17, 18, 15, 16, 19, 16, 19, 18, 18,
15, 18, 19, 16, 16, 18, 15, 13, 12, 20, 19, 18, 19, 13, 19,
19, 16, 20, 18, 18, 18, 18, 18, 18, 19, 15, 14, 18, 16, 15,
15, 18, 18, 18, 18, 20, 17, 16, 19, 18, 19, 17, 18, 18, 16,
16, 18, 15, 19, 19, 17, 17, 16, 15, 15, 15, 17, 12, 17, 17,
19, 14, 21, 19, 19, 18, 23, 18, 21, 18, 16, 17, 18, 13, 14,
17, 18, 16, 18, 16, 18, 18, 17, 17, 6, 22, 17, 18, 20, 18,
10, 18, 15, 10, 16, 16, 18, 18, 17, 21, 18, 18, 15, 13, 15,
17, 12, 16, 16, 16, 15, 20, 17, 14, 17, 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, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1,
1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 0, 1), format.spss = "F8.2", display_width = 10L),
capiabr1 = structure(c(36, 43, NA, NA, 90, 95, 128, 137,
136, 245, 322, 154, 87, 111, 181, 278, 173, 137, 69, 24,
27, 70, 34, 27, 11, 53, 31, 49, 14, 54, 131, 35, 43, 43,
60, 58, 55, 60, 18, 38, 76, 98, 41, 20, 117, 58, 98, 10,
16, 101, 120, 165, 44, 96, 23, 19, 53, 57, 77, 41, 53, 100,
90, 96, 91, 29, 54, 134, 134, 105, 106, NA, 125, 61, 72,
34, 215, 42, NA, 106, 47, 45, 107, 208, 191, NA, 50, 56,
222, 47, 89, 134, 204, 211, 228, NA, 24, 34, 34, 135, 174,
112, 239, 104, 102, 129, 71, 100, 159, 280, 97, 105, NA,
56, 76, 120, 176, 89, 154, 46, 59, 214, 53, 245, 197, 60,
425, 25, 62, 137, 199, 171, 191, 46, 49, 117, 183, 79, 47,
76, NA, 158, 151, 47, 70, 118, 198, 94, 43, 296, 108, 56,
277, 214, 331, NA, 293, 277, 41, 134, 134, 283, 87, 96, 126,
305, 152, 82, 308, 168, 274, NA, 48, 171, 98, 90, 84, 257,
144, 255, NA, 106, 67, 184, 173, 156, 243, 357, 116, 132,
226, 260, 308, 358, 225, 312, 102, 244, 87, 176, 270, 224,
136, 243, NA, 117, 234, 280, 133, 143, 234, 273, NA, 169,
145, 310, 255, 280, 58, 152, 239, 254, 322, 342, 288, NA,
155, 179, 206, 270, 173, 319, 194, 206, 319, 111, 408, 310,
324, 296, 288, 391, 409, 379, 311, 338), format.spss = "F3.0", display_width = 11L),
cbclint = structure(c(51, 55, NA, NA, 65, 57, 46, 58, 53,
56, 75, 65, 33, NA, 65, NA, 51, 65, 34, 60, 45, 29, 43, 37,
65, 49, 56, 64, 53, 51, 39, 43, 64, 61, 74, 29, 60, 53, 45,
43, 45, 49, 47, 47, 66, 57, 73, 41, 56, 37, 65, 45, 53, 60,
53, 33, 43, 51, 53, 45, 47, 59, NA, 47, 79, 68, 56, 66, 70,
47, 63, 61, 61, 56, 33, 53, 56, 43, 51, 55, 51, 73, 56, 88,
56, 59, 30, 54, 82, 50, 63, 51, 58, 37, 67, 58, 51, 52, 40,
72, 63, NA, 43, 56, 60, 48, 66, NA, 55, 47, 61, 56, 55, 51,
55, 40, 64, 40, 66, 76, 45, 63, 53, 47, 51, 70, 80, 40, 53,
51, 43, 54, 64, 53, 64, 58, 56, 60, 55, 40, 40, 49, 48, 41,
47, 56, 60, 53, 55, 49, 55, 33, 67, 58, 41, 46, 67, 63, 64,
73, 73, 60, 49, 40, 51, 45, 53, 49, 65, 54, 58, 51, 68, 45,
41, 53, 60, 55, 61, 66, 69, 66, 67, 70, 66, NA, 56, 58, 61,
67, 73, 47, 74, 65, 62, 72, 59, 60, 73, 64, 48, 56, 53, 81,
65, 65, 65, 65, 59, 56, 70, 68, 63, 64, 74, 60, 75, 58, 63,
43, 72, 69, 59, 71, 71, 64, 66, 63, 46, 66, 66, 66, 53, NA,
73, 68, 65, 68, 62, 57, 68, 69, 74, 65, 78, 47), format.spss = "F8.0", display_width = 10L),
bpsidrr1 = structure(c(NA, 21, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 18, NA, NA, NA, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 8, 9, 10, 10, 10, 11,
11, 11, 9, 11, 8, 11, 9, 10, 12, 11, 13, 10, 8, 11, 10, 13,
12, 14, 9, 10, 13, 11, 11, 10, 13, 13, 13, 12, 10, 11, 13,
10, 13, 16, 12, 15, 10, 12, 13, 13, 11, 14, 15, 13, 13, 14,
13, 14, 13, 18, 13, 14, 14, 14, 15, 16, 17, 16, 14, 15, 14,
14, 15, 14, 20, 16, 16, 13, 17, 16, 15, 14, 16, 18, 17, 17,
19, 14, 17, 16, 16, 17, 16, 14, 14, 15, 17, 18, 17, 14, 14,
18, 17, 19, 16, 16, 17, 18, 15, 19, 16, 21, 18, 17, 19, 15,
20, 18, 19, 16, 18, 23, 15, 18, 20, 19, 12, 12, 21, 16, 17,
17, 20, 20, 19, 19, 22, 20, 19, 22, 14, 19, 19, 23, 19, 20,
19, 19, 20, 20, 23, 18, 19, 25, 20, 23, 20, 21, 22, 21, 21,
24, 22, 24, 22, 22, 18, 23, 24, 22, 22, 24, 21, 23, 21, 20,
21, 23, 23, 25, 24, 22, 23, 26, 23, 26, 26, 23, 26, 26, 23,
25, 24, 22, 27, 25, 24, 27, 23, 25, 25, 26, 23, 27, 30, 28,
29, 27, 31, 34, 32, 31, 34), format.spss = "F2.0", display_width = 11L),
ecbiir1 = structure(c(177, 197, 148, 133, 172, 133, 129,
NA, 159, 67, 141, 167, 111, 190, 174, NA, 137, 93, 99, 136,
54, 36, 36, 75, 126, 97, 68, 205, 110, NA, 109, 47, 93, 200,
183, 42, 73, 132, 82, 91, 154, 157, 82, 124, 207, 84, 188,
76, 104, 73, 185, 108, 140, 183, 52, 48, 100, 110, 109, 56,
88, 69, 189, 82, 210, 159, 68, 144, 119, 81, 190, 180, 199,
206, 72, 153, 151, NA, 115, 111, NA, 161, 118, 159, 127,
124, 136, 174, 232, 48, 161, 54, 74, 53, NA, 112, 148, 135,
137, 159, 75, 74, 36, 101, 142, 83, 132, 99, 141, 117, 117,
134, 105, 134, 147, 54, 206, 170, 69, 134, 64, 55, 129, 79,
110, 173, 159, 113, 163, 139, 111, 103, 93, 86, 179, 144,
167, 118, 124, 118, 91, 166, 66, 127, 54, 177, 108, 125,
115, 142, 130, 156, 152, 51, 132, 76, 155, 185, 148, 132,
146, 147, 134, 50, 158, 143, 142, 98, 111, 150, 138, NA,
221, 150, 167, 145, 146, 63, 201, 195, 192, 183, 168, 162,
170, NA, 87, 119, 171, 136, 66, 183, 162, NA, 168, 153, 151,
109, 147, 214, 156, 147, 148, 117, NA, 140, 124, 165, 175,
106, 198, 141, 183, 208, 201, 139, 171, 170, 165, 116, 226,
102, 157, 182, 161, 169, 208, 144, 140, 139, 128, 174, 158,
231, 168, 181, 211, 176, 159, 180, 110, 188, 151, 206, 205,
67), format.spss = "F3.0", display_width = 11L), mommhpsi = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 35.75, 32.75, 32.75, 32.75, 32.75, 38.5, 38.5,
32.75, 32.75, 32.75, 32.75, 34.25, 36.5, 43, 43, 49, 33,
38, NA, 33.5, 36.5, 36.75, 43.75, NA, 33.75, 50, 35.75, 49.25,
34, 39, 45.25, 50.75, 50, NA, NA, 34.25, 34.25, 34.25, 38.25,
42.75, NA, 34.5, 42.75, 36.25, 43, NA, 34.75, 34.75, 39.5,
39.5, 39, 48, NA, NA, 35, 35, 38.5, 50.5, NA, 41.5, 38.25,
43.5, 44.5, 43, 51.75, 44.5, NA, NA, NA, NA, 35.5, 38.5,
35.5, 38.5, 42.75, 50.25, NA, NA, NA, NA, NA, NA, 35.75,
35.75, 45, 40.5, 46, NA, NA, NA, NA, 47, 45.75, NA, NA, NA,
NA, NA, NA, NA, 47, 39.25, 50.75, 42.25, 42.25, 44.75, 44,
43.75, NA, NA, NA, NA, NA, NA, 45.75, 40.5, 38.25, 42.25,
51.75, NA, NA, NA, NA, NA, 39.75, 43.25, 50.5, 53.5, 54,
NA, 52.75, NA, 37.25, 41.5, 46.5, NA, 55.25, NA, 59.75, 42.25,
44.25, 44.25, 48.25, 47, NA, NA, NA, 46.5, 49.75, 50, 49.25,
56.25, NA, NA, NA, 39.75, 47, 44, 41, 54.75, 55.25, NA, NA,
38.25, 51, 48.75, NA, 43.75, 50.25, NA, NA, 46.25, 57, 59.75,
58.5, 62.5, 62.25, NA, NA, 46.75, 46, 56.25, 55, 55.75, 58.25,
NA, 44.75, 49.5, 46.5, 57.25, 53, 60.5, 63, NA, NA, NA, 56.75,
NA, 60.5, 43.75, 39.75, 59.25, 58.75, 57.5, 56.5, 63, NA,
NA, NA, NA, 55.5, 50, NA, 61.25, 61.5, 61, 62.75, 66.5, 57,
64.75, NA, 59.25, 68.25, 65.25, NA, 68.75, 50)), .Names = c("id",
"peadiff", "ceadiff", "cdpea", "mompa", "momabhx", "capiabr1",
"cbclint", "bpsidrr1", "ecbiir1", "mommhpsi"), row.names = c(NA,
-246L), class = "data.frame")

Your code works correctly. The problem in given by the version of lavaan and semTools that you are using.
Following the suggestions given here by Terrence D. Jorgensen (one of the authors of semTools), start a new session of R and reinstall the two packages as follows:
install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
# if necessary: install.packages("devtools")
devtools::install_github("simsem/semTools/semTools")
Now the commands:
fit5 <- runMI(model5, data = imputedData, fun="sem", ordered = "mompa")
summary(fit5, standardized = TRUE, ci = T)
give the following output:
Rubin's (1987) rules were used to pool point and SE estimates across 5 imputed data sets, and to calculate degrees of freedom for each parameter's t test and CI.
lavaan.mi object based on 5 imputed data sets.
See class?lavaan.mi help page for available methods.
Convergence information:
The model converged on 5 imputed data sets
Parameter Estimates:
Information Expected
Information saturated (h1) model
Standard Errors Robust.sem
Regressions:
Estimate Std.Err t df P(>|z|) ci.lower ci.upper Std.lv Std.all
ceadiff ~
mompa 0.473 0.165 2.863 2016.256 0.004 0.149 0.797 0.473 0.223
cdpea 0.137 0.038 3.589 2507.509 0.000 0.062 0.212 0.137 0.157
momabhx -0.251 0.302 -0.831 Inf 0.406 -0.843 0.341 -0.251 -0.059
mompa ~
peadiff (b1) 0.108 0.035 3.091 Inf 0.002 0.039 0.176 0.108 0.245
momabhx (c) 0.548 0.165 3.324 Inf 0.001 0.225 0.871 0.548 0.273
cdpea -0.048 0.031 -1.525 Inf 0.127 -0.109 0.014 -0.048 -0.116
mommhpsi (b2) -0.022 0.009 -2.365 61.332 0.021 -0.040 -0.003 -0.022 -0.192
...

Related

Fill new column with different numbers depending on how often values appear in other column, R

I have a dataframe with different company IDs appearing from once to over 30 times in different rows. I want to add a new column "di_Flex" and fill it with specific values depending on how often the same company ID appears in a column:
If it appears twice in the column, add the value 6 to the new column "di_Flex",
if it appears 3x, add "8",
if it appears 4x add "10",
if it appears 5x add "12.8",
if it appears 6x add "14.67",
if it appears 7 or more times add "16".
Here is the dataframe:
c(0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 7, 7, 8, 9, 9, 9, 10, 10, 11, 11, 12, 12, 13, 14,
15, 16, 17, 17, 18, 18, 19, 20, 21, 22, 23, 23, 23, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 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, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27,
28, 29, 30, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35, 36,
36, 37, 38, 38, 38, 38, 38, 38, 39, 40, 41, 41, 41, 42, 42, 42,
43, 43, 43, 44, 45, 45, 46, 46, 46, 47, 48, 49, 50, 50, 51, 53,
54, 54, 54, 54, 55, 57, 57, 57, 59, 59, 59, 59, 60, 60, 60, 60,
61, 61, 62, 62, 62, 63, 63, 64, 64, 64, 64, 65, 65, 66, 66, 66,
66, 66, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA)
Thank you for your help!
Assuming your data is called df with a column value:
library(tidyverse)
left_join(df, df %>%
group_by(value) %>%
tally()) %>%
mutate(di_Flex = case_when(n == 2 ~ 6,
n == 3 ~ 8,
n == 4 ~ 10,
n == 5 ~ 12.8,
n == 6 ~ 14.67,
n >= 7 ~ 16)) %>%
select(-n)
This gives us:
1 0 12.8
2 0 12.8
3 0 12.8
4 0 12.8
5 0 12.8
6 1 NA
7 2 NA
8 3 NA
9 4 NA
10 5 8.0
11 5 8.0
12 5 8.0
13 6 16.0
14 6 16.0
15 6 16.0
16 6 16.0
17 6 16.0
18 6 16.0
19 6 16.0
20 6 16.0
Data:
df <- data.frame(value = c(0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 7, 7, 8, 9, 9, 9, 10, 10, 11, 11, 12, 12, 13, 14,
15, 16, 17, 17, 18, 18, 19, 20, 21, 22, 23, 23, 23, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 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, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27,
28, 29, 30, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35, 36,
36, 37, 38, 38, 38, 38, 38, 38, 39, 40, 41, 41, 41, 42, 42, 42,
43, 43, 43, 44, 45, 45, 46, 46, 46, 47, 48, 49, 50, 50, 51, 53,
54, 54, 54, 54, 55, 57, 57, 57, 59, 59, 59, 59, 60, 60, 60, 60,
61, 61, 62, 62, 62, 63, 63, 64, 64, 64, 64, 65, 65, 66, 66, 66,
66, 66, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA))

Iterate over columns with NAs to create percentile variables with dplyr and data.table

I need quite a simple thing. To iterate over columns of a dataset to create percentil versions of said columns. I tried with dplyr and data.table but none seem to do what I need. Particulary, I need to exclude de NA values when creating the percentile versions of the columns.
Reproducible example below:
values<-c(19,
6,
27,
63,
50,
59,
97,
89,
NA,
9,
31,
58,
83,
2,
1,
31,
3,
1,
27,
40,
32,
42,
99,
NA,
12,
16,
23,
98,
44,
25,
13,
70,
64,
NA,
37,
75,
73,
59,
21,
3,
76,
43,
6,
96,
55,
48,
70,
90,
18,
58,
22,
19,
26,
49,
59,
94,
31,
45,
20,
8,
26,
56,
7,
11,
98,
50,
41,
38,
86,
0,
37,
NA,
40,
7,
88,
38,
41,
41,
19,
34,
21,
64,
87,
22,
54,
39,
75,
72,
91,
78)
values2<- c(98,
60,
9,
98,
NA,
88,
NA,
54,
92,
90,
NA,
83,
92,
65,
44,
NA,
98,
40,
26,
40,
54,
56,
15,
90,
15,
63,
57,
NA,
85,
69,
73,
43,
24,
27,
82,
75,
29,
98,
29,
5,
91,
88,
28,
12,
53,
NA,
2,
42,
86,
2,
78,
20,
50,
73,
77,
NA,
4,
39,
90,
NA,
29,
14,
98,
88,
77,
79,
30,
9,
74,
93,
NA,
16,
27,
16,
18,
40,
NA,
2,
66,
71,
82,
10,
62,
84,
25,
NA,
15,
12,
85,
50)
groups<-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,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2)
df<-as.data.frame(cbind(groups,values,values2))
library(dplyr)
for (i in c("values","values2")) {
df<-df %>%
group_by(groups) %>%
mutate(!!sym(paste( i,"_percentile", sep="")) := percent_rank(na.omit(i)))
}
for (i in c("values","values2")) {
df<-df %>%
group_by(groups) %>%
mutate(!!sym(paste( i,"_percentile", sep="")) := rank(i)/length(i) )
}
library(data.table)
df<- as.data.table(df)
for (i in c("values","values2")) {
df[, paste(i,"_percentile",sep="") := rank(get(i))/length( get(i)), by = groups ]
}
for (i in c("values","values2")) {
df[!is.na(i), paste(i,"_percentile",sep="") := rank(get(i))/length( get(i)), by = groups ]
}
An option is mutate_at. After grouping by 'groups', use mutate_at to loop over the columns that starts_with ('values') as column name, replace, the values where the values are not NA with the percent_rank of the non-NA elements
library(dplyr)
df %>%
group_by(groups) %>%
mutate_at(vars(starts_with('values')),
list(percentile = ~ replace(., !is.na(.), percent_rank(.[!is.na(.)]))))
Or with data.table
library(data.table)
nm1 <- paste(names(df1)[2:3], "_percentile")
setDT(df)[, (nm1) := lapply(.SD, function(x) replace(x, !is.na(x),
frank(x[!is.na(x)])/sum(!is.na(x)))), .SDcols = 2:3, by = groups]
My tidyverse answer has the same structure as #akrun's -- using mutate_at to add multiple columns, starts_with to select the columns. A few things worth pointing out with the more minimal example:
The percent_rank function already removes NA's when it calculates, so you don't have to do the additional work to filter them out of the calc.
There is one degenerate case where there's only one actual measure. (In my case, it's group "b"). percent_rank can return a NaN value there because it's scaling the min_rank. Inside the direct mutate_at, that issue seems to be avoided. (It's unclear what value that should be assigned to in your case).
There's another sort-of degenerate case when there's a tie. In group "a", I have a tie for first place, and the percent_rank's are accordingly not 1.0.
library(tidyverse)
df <- tribble(
~groups, ~values1, ~values2,
"a", 1, 10,
"a", 2, 10,
"a", NA, 8,
"a", 3, 9,
"a", 4, 7,
"b", NA, 10,
"b", 2, NA,
"b", NA, 8
)
df %>%
group_by(groups) %>%
mutate_at(
vars(starts_with("values")),
list(percentile = ~ percent_rank(.)))
#> # A tibble: 8 x 5
#> # Groups: groups [2]
#> groups values1 values2 values1_percentile values2_percentile
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 a 1 10 0 0.75
#> 2 a 2 10 0.333 0.75
#> 3 a NA 8 NA 0.25
#> 4 a 3 9 0.667 0.5
#> 5 a 4 7 1 0
#> 6 b NA 10 NA 1
#> 7 b 2 NA 0 NA
#> 8 b NA 8 NA 0

Calculate row similarity percentage pair wise and add it as a new column

I have a date frame like this sample, I would like to find similar rows (not duplicate) and calculate similarity per wise. I find this solution but i would like to keep all my columns and add similarity percentage as a new variable. My aim is to find records with highest similarity percentage. How could I do it ?
sample data set
df <- tibble::tribble(
~date, ~user_id, ~Station_id, ~location_id, ~ind_id, ~start_hour, ~start_minute, ~start_second, ~end_hour, ~end_minute, ~end_second, ~duration_min,
20191015, 19900234, 242, 2, "ac", 7, 25, 0, 7, 30, 59, 6,
20191015, 19900234, 242, 2, "ac", 7, 31, 0, 7, 32, 59, 2,
20191015, 19900234, 242, 2, "ac", 7, 33, 0, 7, 38, 59, 6,
20191015, 19900234, 242, 2, "ac", 7, 39, 0, 7, 40, 59, 2,
20191015, 19900234, 242, 2, "ac", 7, 41, 0, 7, 43, 59, 3,
20191015, 19900234, 242, 2, "ac", 7, 44, 0, 7, 45, 59, 2,
20191015, 19900234, 242, 2, "ac", 7, 47, 0, 7, 59, 59, 13,
20191015, 19900234, 242, 2, "ad", 7, 47, 0, 7, 59, 59, 13,
20191015, 19900234, 242, 2, "ac", 8, 5, 0, 8, 6, 59, 2,
20191015, 19900234, 242, 2, "ad", 8, 5, 0, 8, 6, 59, 2,
20191015, 19900234, 242, 2, "ac", 8, 7, 0, 8, 8, 59, 2,
20191015, 19900234, 242, 2, "ad", 8, 7, 0, 8, 8, 59, 2,
20191015, 19900234, 242, 2, "ac", 16, 26, 0, 16, 55, 59, 30,
20191015, 19900234, 242, 2, "ad", 16, 26, 0, 16, 55, 59, 30,
20191015, 19900234, 242, 2, "ad", 17, 5, 0, 17, 6, 59, 2,
20191015, 19900234, 242, 2, "ac", 17, 5, 0, 17, 23, 59, 19,
20191015, 19900234, 242, 2, "ad", 17, 7, 0, 17, 15, 59, 9,
20191015, 19900234, 242, 2, "ad", 17, 16, 0, 17, 22, 59, 7,
20191015, 19900234, 264, 2, "ac", 17, 24, 0, 17, 35, 59, 12,
20191015, 19900234, 264, 2, "ad", 17, 25, 0, 17, 35, 59, 11,
20191016, 19900234, 242, 1, "ac", 7, 12, 0, 7, 14, 59, 3,
20191016, 19900234, 242, 1, "ad", 7, 13, 0, 7, 13, 59, 1,
20191016, 19900234, 242, 1, "ac", 17, 45, 0, 17, 49, 59, 5,
20191016, 19900234, 242, 1, "ad", 17, 46, 0, 17, 48, 59, 3,
20191016, 19900234, 242, 2, "ad", 7, 14, 0, 8, 0, 59, 47,
20191016, 19900234, 242, 2, "ac", 7, 15, 0, 8, 0, 59, 47
)
Function for comparing rows
row_cf <- function(x, y, df){
sum(df[x,] == df[y,])/ncol(df)
}
Function output
# 1) Create all possible row combinations
# 2) Rename
# 3) Run through each row
# 4) Calculate similarity
expand.grid(1:nrow(df), 1:nrow(df)) %>%
rename(row_1 = Var1, row_2 = Var2) %>%
rowwise() %>%
mutate(similarity = row_cf(row_1, row_2, df))
# A tibble: 676 x 3
row_1 row_2 similarity
<int> <int> <dbl>
1 1 1 1
2 2 1 0.75
3 3 1 0.833
4 4 1 0.75
5 5 1 0.75
6 6 1 0.75
7 7 1 0.75
8 8 1 0.667
9 9 1 0.583
10 10 1 0.5
Edit:
I would like to find similar rows in the data like here
Using your "function output", call it sim. Eliminate the self-comparisons and then keep the max similarity row grouped by row_1:
sim = sim %>%
filter(row_1 != row_2) %>%
group_by(row_1) %>%
slice(which.max(similarity))
Then you can add these to your original data:
df %>% mutate(row_1 = 1:n()) %>%
left_join(sim)
The row_2 column gives the row number of the most similar row, and similarity gives its similarity score. (You may want to improve these column names.)

FMI function for MI datasets is acting up?

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,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 0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), format.spss = "F8.2", display_width = 10L),
mommh = c(63, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.75, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 43.25, NA, NA, 63, 41.5,
34.25, 38.5, 39, 38.5, NA, 49.75, 57.5, 59.25, 50, 42.75,
45, 49, 32.75, NA, 35.75, 64.75, 50.5, 46.5, 39.75, 51.75,
34.75, 61.25, 46, 43, 56.25, 47, 42.25, 36.5, 34.5, 47, 50,
35, 48.25, 46.5, 58.5, 35.5, 55.25, 43.5, 42.75, 35.75, 38,
35.5, 50, 38.25, 57, 45.75, 38.5, 44.25, 51.75, NA, 38.25,
39.75, 34, 57.25, 39.25, 42.25, 37.25, NA, 32.75, 52.75,
NA, NA, 55.75, 62.25, 59.75, 43.75, 59.75, 35.75, NA, 34.25,
59.25, 39, 34.75, 32.75, NA, 53.5, NA, 40.5, 50, 33.5, 45.25,
41, 50, NA, 38.5, 61.5, 36.25, 46.25, 46, 44.75, 44.75, 62.5,
38.25, 49.5, 33.75, NA, 50.25, 43, 43.75, 42.25, 60.5, NA,
50.25, 54.75, 42.75, 45.75, 61, 58.25, 44.5, 46.5, 34.25,
56.75, 40.5, 47, 42.25, 48, 44, 36.75, 39.75, 48.75, 38.25,
49.25, 49.25, NA, NA, 34.25, 44.5, NA, 51, 44, 50.75, 56.25,
35, 55, 58.75, 56.5, 68.75, 54, 53, 41.5, 50.75, NA, 32.75,
46.75, 32.75, 43, 57, 55.25, NA, NA, 43.75, 55.5, NA, NA,
32.75, NA, NA, NA, NA, 60.5, 32.75, NA, 68.25, 50.5, 32.75,
66.5, 33, 38.5, 43, 43.75, 62.75, 47, 36.5, 39.5, 39.5),
risk6 = structure(c(0, 0, 0, 0, 3, 1, 1, 1, 1, 0, 1, 1, 0,
0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 0, 0, 2, 1, 1,
2, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 2, 1, 3, 2, 2, 0, 0, 0,
2, 0, 2, 2, 1, 2, 2, 1, 3, 2, 3, 1, 1, 0, 1, 3, 1, 2, 2,
0, 1, 0, 0, 1, 3, 1, 0, 1, 0, 0, 1, 3, 0, 1, 1, 0, 0, 2,
3, 3, 1, 2, 3, 2, 0, 0, 4, 1, 2, 1, 3, 2, 1, 2, 0, 1, 1,
2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1,
NA, 1, 1, 1, 2, 0, NA, 3, 0, 2, 2, 3, 4, 4, 0, 1, 0, 2, 3,
2, 2, 2, 2, 1, 3, 2, 2, 3, 1, 1, 1, 0, 0, 1, 1, 0, 2, 0,
1, 2, 3, 1, 1, 1, 2, 1, 2, 0, 0, 2, 2, 0, 1, 2, 0, 0, 2,
1, 1, 1, 1, 1, 3, 1, 0, 3, 0, 1, 0, 1, 1, 1, 2, 2, 0, 2,
3, 3, 0, 0, 0, 1, 2, 1, 1, 1, 0, 1, 1, 3, 1, 1, 0, 0, 3,
2, 0, 0, 3, 2, 1, 3, 1, 0, 3, 0, 1, 1, 2, 3, 3, 1, 4, 2,
3, 2, 2), format.spss = "F8.2", display_width = 10L), eadiff = structure(c(-1.26734803867686,
-0.355541076313792, 0.518653050779668, 1.50568568368194,
0.0940935989894723, 2.07356799670629, 1.01843817310907, -1.26734803867686,
-0.317928241044189, 0.531190662536203, 0.0940935989894723,
-1.47335895869369, -0.586627219843691, -1.26734803867686,
0.325179742519372, 0.556265886049271, 1.4179224013862, 1.2244490931259,
-0.586627219843691, 0.081555987232938, -0.149530156296961,
-0.380616299826861, -0.805175751617057, -0.368078688070326,
0.0940935989894723, -0.124454932783893, 0.955750114326398,
-0.805175751617057, 0.531190662536203, -0.830250975130125,
0.968287726082933, 0.749739194309568, -0.368078688070326,
-1.03626189514696, 3.19138587908619, -0.574089608087157,
1.67408376842917, -0.586627219843691, -0.343003464557258,
-0.162067768053496, 0.325179742519372, -1.24227281516379,
-1.03626189514696, 0.749739194309568, 0.325179742519372,
0.556265886049271, 0.762276806066102, -0.817713363373591,
-0.805175751617057, 0.119168822502541, -0.805175751617057,
-0.149530156296961, 0.0940935989894723, -1.48589657045022,
1.01843817310907, 0.312642130762837, 1.21191148136937, -0.355541076313792,
-1.04879950690349, -0.368078688070326, -0.124454932783893,
0.312642130762837, -1.25481042692032, -0.136992544540427,
1.01843817310907, -0.124454932783893, -0.368078688070326,
-0.805175751617057, 0.081555987232938, -0.805175751617057,
0.325179742519372, 2.97283734731282, 0.337717354275906, 0.0690183754764037,
-0.136992544540427, -0.830250975130125, 3.03552540609549,
0.0940935989894723, 0.0690183754764037, -0.124454932783893,
-0.817713363373591, -0.355541076313792, 0.312642130762837,
0.980825337839467, -0.343003464557258, 0.993362949596001,
-0.586627219843691, -0.574089608087157, -1.02372428339042,
-0.561551996330623, -0.111917321027358, -0.136992544540427,
-0.149530156296961, -0.830250975130125, 0.568803497805805,
0.0690183754764037, -0.805175751617057, -0.830250975130125,
0.556265886049271, 0.968287726082933, 0.531190662536203,
0.312642130762837, 0.337717354275906, 0.774814417822636,
0.337717354275906, 0.337717354275906, -0.586627219843691,
0.106631210746007, -1.02372428339042, -0.574089608087157,
-0.355541076313792, 0.737201582553033, 0.325179742519372,
0.312642130762837, 0.556265886049271, 0.0940935989894723,
0.300104519006303, -0.330465852800723, 0.0940935989894723,
-0.355541076313792, -0.599164831600226, 0.312642130762837,
0.531190662536203, -1.25481042692032, 0.531190662536203,
1.89263230020253, -0.817713363373591, -1.02372428339042,
0.980825337839467, -0.149530156296961, -0.586627219843691,
1.23698670488244, 0.556265886049271, 0.325179742519372, -0.817713363373591,
1.01843817310907, -1.02372428339042, -0.805175751617057,
-0.355541076313792, 1.67408376842917, 0.0690183754764037,
-0.368078688070326, -0.124454932783893, 0.980825337839467,
-1.03626189514696, 0.119168822502541, -1.03626189514696,
-1.03626189514696, 1.4555352366558, -0.136992544540427, -1.04879950690349,
0.749739194309568, -0.792638139860522, 0.312642130762837,
-0.0993797092708241, -0.17460537981003, -0.343003464557258,
-0.586627219843691, 0.300104519006303, -0.355541076313792,
-0.805175751617057, 0.518653050779668, -1.26734803867686,
-1.25481042692032, -0.368078688070326, -0.805175751617057,
-0.343003464557258, -0.343003464557258, -0.599164831600226,
-0.124454932783893, 1.66154615667263, -0.586627219843691,
-0.586627219843691, -0.124454932783893, 0.955750114326398,
-0.355541076313792, -0.343003464557258, 0.0940935989894723,
-0.792638139860522, -0.599164831600226, NA, -0.586627219843691,
-1.26734803867686, 0.762276806066102, 1.2244490931259, 0.081555987232938,
-0.574089608087157, -1.01118667163389, 0.312642130762837,
0.081555987232938, -0.368078688070326, -1.26734803867686,
1.63647093315956, -0.368078688070326, 0.531190662536203,
0.081555987232938, 0.543728274292737, 0.0564807637198694,
0.955750114326398, -1.25481042692032, 1.44299762489927, -1.04879950690349,
0.106631210746007, -0.586627219843691, 0.0940935989894723,
-0.162067768053496, 0.0940935989894723, -0.111917321027358,
0.968287726082933, 0.0940935989894723, 0.312642130762837,
-0.586627219843691, 0.543728274292737, -0.124454932783893,
0.543728274292737, -0.817713363373591, -0.586627219843691,
-0.368078688070326, 0.0940935989894723, -0.599164831600226,
-1.03626189514696, 0.774814417822636, 0.106631210746007,
-0.111917321027358, -0.817713363373591, -0.330465852800723,
0.993362949596001, -0.368078688070326, 1.19937386961283,
0.531190662536203, 0.749739194309568, 1.6490085449161, 0.0690183754764037,
-0.574089608087157, -0.368078688070326, 1.00590056135254,
1.4555352366558, -0.574089608087157, -0.586627219843691,
-0.817713363373591, -0.817713363373591, 0.0940935989894723,
-0.792638139860522, 0.0690183754764037), format.spss = "F8.2", display_width = 10L)), .Names = c("mompa",
"momabhx", "mommh", "risk6", "eadiff"), row.names = c(NA, -244L
), class = "data.frame")
And here is the same dataset with 4 source variables (depr, anxt, host, bpsipdr1)
> dput(dat3)
structure(list(mompa = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0), format.sp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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")

Format html-output from R table like excel pivot w/ option "Do not repeat item labels"

I have casted (reshape2) a dataframe table with the following structure:
Region | school
|Region | school | Year1 | Year2 |
-----------------------------------
|R1 | S1 | Value1| Value2 |
|R1 | S2 | Value3| Value4 |
|R1 | S3 | Value5| Value6 |
|R2 | S4 | Value7| Value8 |
|R2 | S5 | Value9| Value10|
So for each region, there are severeal schools. I would would like to only show the region once, like this:
|Region | school | Year1 | Year2 |
-----------------------------------
|R1 | S1 | Value1| Value2 |
| | S2 | Value3| Value4 |
| | S3 | Value5| Value6 |
|R2 | S4 | Value7| Value8 |
| | S5 | Value9| Value10|
I am pretty sure I have seen this in many xtables - but I can't find and example now. Any ideas? If your are in doubt as to what I am trying to do; In Excel this is option can be found (it is the default i think) under pivottable tools -> design -> Report layout.
Below my casted datafram (dput):
structure(list(region = c("Hovedstaden", "Hovedstaden", "Hovedstaden",
"Hovedstaden", "Hovedstaden", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Nordjylland", "Nordjylland", "Nordjylland",
"Nordjylland", "Nordjylland", "Sjælland", "Sjælland", "Sjælland",
"Sjælland", "Sjælland", "Sjælland", "Sjælland", "Syddanmark",
"Syddanmark", "Syddanmark", "Syddanmark", "Syddanmark", "Syddanmark",
"Syddanmark"), school = c("Campus Bornholm", "CPH - Uddannelsescenter København Vest",
"Erhvervsskolen Nordsjælland", "Københavns Tekniske Skole",
"TEC Teknisk Erhvervsskole Center", "Den jydske Haandværkerskole",
"Herningsholm Erhvervsskole", "Holstebro Tekniske Skole", "Learnmark Horsens",
"Professionshøjskolen VIA University College", "Skive Tekniske Skole",
"Teknisk Skole Silkeborg", "Tradium", "Tradium, Tekniske erhvervsudd. og Teknisk Gymnasium, HTX",
"Uddannelsescenter Holstebro", "UddannelsesCenter Ringkøbing-Skjern",
"UddannelsesCenter Ringkøbing-Skjern, teknisk skole", "Viden Djurs",
"Vitus Bering Danmark", "AARHUS TECH", "Erhvervsskolerne Aars",
"EUC Nord", "EUC Nordvest", "Nordvestjysk Uddannelsescenter",
"Tech College Aalborg", "CELF - Center for erhv.rettede udd. Lolland-Falster",
"CEUS", "EUC Nordvestsjælland", "EUC Sjælland", "Roskilde Tekniske Skole",
"Roskilde Tekniske Skole - Pulsen", "Selandia - CEU", "EUC Syd",
"EUC Vest", "HANSENBERG", "Odense Tekniske Skole", "Svendborg Erhvervsskole",
"Syddansk Erhvervsskole Odense-Vejle", "Vejle Tekniske Skole"
), `2003` = c(0, 0, 83, 0, 294, 120, 73, 73, 0, 0, 28, 28, 0,
29, 0, 0, 29, 0, 29, 47, 0, 25, 0, 25, 78, 0, 36, 25, 52, 0,
55, 18, 66, 45, 45, 64, 15, 0, 72), `2004` = c(1243, 0, 71, 0,
296, 122, 79, 79, 0, 0, 39, 39, 0, 30, 0, 0, 30, 0, 5, 53, 0,
36, 0, 36, 82, 0, 53, 36, 58, 0, 76, 31, 56, 54, 54, 92, 30,
0, 82), `2005` = c(1397, 0, 80, 0, 348, 132, 123, 123, 0, 0,
44, 0, 0, 37, 0, 0, 37, 0, 37, 61, 0, 65, 65, 0, 143, 0, 56,
40, 61, 0, 57, 26, 76, 53, 53, 119, 34, 0, 102), `2006` = c(1676,
7, 98, 0, 423, 176, 132, 7, 0, 0, 48, 48, 0, 42, 0, 0, 42, 0,
5, 69, 0, 87, 87, 0, 154, 0, 69, 47, 70, 0, 89, 42, 74, 81, 81,
122, 38, 0, 119), `2007` = c(6, 6, 94, 94, 416, 190, 118, 13,
0, 13, 57, 0, 0, 62, 0, 62, 0, 62, 0, 71, 7, 72, 72, 0, 193,
0, 69, 53, 71, 78, 0, 42, 82, 68, 68, 108, 52, 0, 142), `2008` = c(2090,
2090, 79, 79, 345, 158, 113, 9, 0, 6, 42, 0, 0, 31, 0, 0, 0,
31, 0, 62, 423, 76, 76, 0, 141, 53, 0, 31, 43, 90, 0, 26, 84,
62, 62, 0, 28, 193, 0), `2009` = c(1687, 1687, 58, 0, 237, 90,
55, 0, 0, 55, 23, 23, 28, 0, 28, 0, 0, 28, 0, 42, 244, 37, 37,
0, 68, 38, 0, 44, 34, 59, 0, 21, 41, 39, 39, 0, 15, 104, 0),
`2010` = c(1043, 1043, 70, 70, 285, 113, 56, 0, 56, 0, 32,
0, 29, 0, 29, 0, 0, 29, 0, 52, 287, 52, 52, 0, 75, 41, 0,
23, 34, 56, 0, 27, 52, 35, 0, 0, 35, 145, 0), `2011` = c(1223,
0, 66, 0, 279, 130, 73, 0, 73, 0, 22, 0, 23, 0, 23, 0, 0,
0, 0, 53, 0, 44, 44, 0, 90, 51, 0, 35, 32, 65, 0, 28, 35,
45, 0, 0, 31, 141, 0), `2012` = c(0, 0, 72, 0, 349, 130,
93, 0, 93, 0, 46, 0, 26, 0, 9, 0, 0, 0, 0, 56, 0, 56, 0,
0, 101, 41, 0, 38, 28, 76, 0, 34, 44, 47, 0, 0, 32, 186,
0), `2013` = c(0, 0, 56, 0, 286, 142, 79, 0, 79, 0, 25, 0,
27, 0, 27, 0, 0, 0, 0, 65, 0, 56, 0, 0, 112, 41, 0, 19, 39,
72, 0, 36, 44, 49, 0, 0, 19, 143, 0), `2014` = c(0, 0, 18,
0, 49, 38, 26, 0, 26, 0, 7, 0, 7, 0, 7, 0, 0, 0, 0, 7, 0,
8, 0, 0, 20, 9, 0, 18, 10, 9, 0, 10, 11, 15, 0, 0, 7, 36,
0)), .Names = c("region", "school", "2003", "2004", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014"), row.names = c(NA, -39L), class = "data.frame")
the function htmlTable ind Gmisc package can provide two-level coloumnlabels and rowlabels (using cgroup/rgroup and n.croup/n.rgroup. See e.g. http://timelyportfolio.blogspot.dk/2013/04/tables-are-like-cockroaches.html and http://gforge.se/2013/02/tables-from-r-into-word/

Resources