Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is expand.grid in R returning <0 rows>?

I’m trying to use expand.grid to eventually plot estimates from a statistical model. But the output is:

[1] ID     Age    Sex    Tcoded
<0 rows> (or 0-length row.names)

code:

new_df <- with(test,
               expand.grid(ID = factor(levels(ID)[1], levels = levels(ID)),
                           Age = gratia::seq_min_max(Age, n = 100),
                           Sex = factor(levels(Sex), levels = levels(Sex)),
                           Tcoded = median(Tcoded)))

If I remove the first line for ID, the function works.
Why is this happening?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sample data:

structure(list(ID = c(33714L, 35377L, 40556L, 40798L, 40800L, 
40815L, 50848L, 52183L, 52461L, 53320L, 53873L, 54206L, 54581L, 
55122L, 55267L, 55462L, 55612L, 55920L, 56022L, 56307L, 56420L, 
56679L, 57405L, 57480L, 57725L, 57809L, 58004L, 58215L, 58229L, 
59326L, 59327L, 59865L, 60099L, 60100L, 60280L, 60384L, 60429L, 
60493L, 60503L, 60603L, 60664L, 60846L, 61415L, 61749L, 61883L, 
62081L, 62983L, 63327L, 63329L, 64418L, 64507L, 64596L, 65178L, 
65250L, 65802L, 65975L, 65978L, 66396L, 66572L, 66589L, 74034L, 
74427L, 74607L, 74952L, 75732L, 76574L, 76595L, 76755L, 76759L, 
77203L, 77453L, 77668L, 81064L, 81065L, 33714L, 35377L, 40556L, 
40798L, 40800L, 40815L, 50848L, 52183L, 52461L, 53320L, 53873L, 
54206L, 54581L, 55122L, 55267L, 55462L, 55612L, 55920L, 56022L, 
56307L, 56420L, 56679L, 57405L, 57480L, 57725L, 57809L, 58004L, 
58215L, 58229L, 59326L, 59327L, 59865L, 60099L, 60100L, 60280L, 
60384L, 60429L, 60493L, 60503L, 60603L, 60664L, 60846L, 61415L, 
61749L, 61883L, 62081L, 62983L, 63327L, 63329L, 64418L, 64507L, 
64596L, 65178L, 65250L, 65802L, 65975L, 65978L, 66396L, 66572L, 
66589L, 74034L, 74427L, 74607L, 74952L, 75732L, 76574L, 76595L, 
76755L, 76759L, 77203L, 77453L, 77668L, 81064L, 81065L, 33714L, 
35377L, 40556L, 40798L, 40800L, 40815L, 50848L, 52183L, 52461L, 
53320L, 53873L, 54206L, 54581L, 55122L, 55267L, 55462L, 55612L, 
55920L, 56022L, 56307L, 56420L, 56679L, 57405L, 57480L, 57725L, 
57809L, 58004L, 58215L, 58229L, 59326L, 59327L, 59865L, 60099L, 
60100L, 60280L, 60384L, 60429L, 60493L, 60503L, 60603L, 60664L, 
60846L, 61415L, 61749L, 61883L, 62081L, 62983L, 63327L, 63329L, 
64418L, 64507L, 64596L, 65178L, 65250L, 65802L, 65975L, 65978L, 
66396L, 66572L, 66589L, 74034L, 74427L, 74607L, 74952L, 75732L, 
76574L, 76595L, 76755L, 76759L, 77203L, 77453L, 77668L, 81064L, 
81065L), Sex = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 
2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 
2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 
2L, 1L, 2L, 1L, 1L), .Label = c("Male", "Female"), class = "factor"), 
    Age = c(15L, 15L, 9L, 11L, 16L, 9L, 16L, 16L, 14L, 8L, 6L, 
    14L, 10L, 15L, 13L, 15L, 8L, 9L, 9L, 8L, 9L, 9L, 13L, 10L, 
    7L, 8L, 8L, 6L, 15L, 8L, 11L, 14L, 12L, 10L, 16L, 12L, 10L, 
    6L, 13L, 11L, 12L, 13L, 10L, 13L, 14L, 12L, 17L, 9L, 12L, 
    11L, 10L, 12L, 10L, 10L, 14L, 16L, 15L, 14L, 14L, 13L, 10L, 
    12L, 9L, 9L, 16L, 10L, 14L, 15L, 13L, 15L, 13L, 13L, 8L, 
    11L, 16L, 16L, 11L, 13L, 18L, 10L, 18L, 18L, 15L, 10L, 8L, 
    15L, 12L, 16L, 14L, 16L, 9L, 11L, 11L, 10L, 10L, 11L, 14L, 
    12L, 8L, 9L, 9L, 8L, 16L, 9L, 13L, 16L, 13L, 12L, 18L, 13L, 
    11L, 8L, 14L, 12L, 13L, 14L, 11L, 14L, 15L, 14L, 18L, 11L, 
    14L, 12L, 12L, 13L, 11L, 11L, 15L, 17L, 16L, 15L, 15L, 14L, 
    11L, 13L, 10L, 11L, 18L, 11L, 15L, 16L, 14L, 17L, 14L, 14L, 
    9L, 12L, 18L, 18L, 12L, 14L, 19L, 11L, 19L, 19L, 16L, 11L, 
    9L, 17L, 13L, 18L, 16L, 18L, 11L, 12L, 12L, 11L, 11L, 12L, 
    16L, 13L, 9L, 11L, 10L, 9L, 17L, 11L, 14L, 17L, 14L, 13L, 
    19L, 15L, 12L, 9L, 15L, 14L, 14L, 15L, 12L, 16L, 17L, 15L, 
    20L, 12L, 15L, 13L, 13L, 14L, 12L, 12L, 16L, 19L, 18L, 16L, 
    16L, 15L, 12L, 14L, 11L, 11L, 19L, 12L, 16L, 17L, 15L, 18L, 
    15L, 15L, 10L, 13L), Mean_DTI = c(-3.91040964443843, -4.1357309415226, 
    -3.15711439697149, -5.3057953682613, -5.64125379300701, -5.14501797062188, 
    0.244858660867345, 0.0760011434854038, -1.38050716781916, 
    -0.765398953185992, -1.47720169386702, -1.64309892713909, 
    -0.396819316883117, -1.36975793945299, -1.80785572309015, 
    -0.401914887891913, -0.811404680008261, -2.4492910546021, 
    -0.620903118150862, -1.10304119407832, -0.683772758549858, 
    -1.47159656575735, -0.823876696667884, 0.229547683217108, 
    -1.91347963071532, -1.05594355918274, -2.37962731952471, 
    -1.34893889218848, -0.385827442278431, -0.980092345023242, 
    -0.181058281596406, -0.832975930612162, -1.9959308225624, 
    -0.12787022611412, -0.205371434695515, -0.609353157197594, 
    -0.477183751078979, -1.53245224237668, -0.137187841673061, 
    -1.11978378453579, -1.38506891710323, -0.665622819909006, 
    -0.858065551674517, -0.935493966384356, -0.444790478237349, 
    -0.0888284463372063, 0.793433276899957, -1.37118955226023, 
    -1.27752810419379, 0.571630350274245, 0.38391922015976, -0.585743678190174, 
    -0.639562613892594, 0.637702921021628, 1.02283103079116, 
    0.509051885361466, 0.256796855802238, -0.827467860997893, 
    1.25436407158129, 0.797121499725372, -1.46470541258355, 0.72639012119852, 
    -1.77912640845008, 1.31912635264069, 0.135328148802092, -0.522413009938002, 
    0.680529982119362, 0.068624697834574, -0.598312753345204, 
    0.141370040141094, -0.392378890718311, 1.88269650621348, 
    -3.0968410713246, -0.0622101539722123, -4.1452184094485, 
    -0.954638754603117, -0.106347504757922, -0.716821176235476, 
    0.33910245990621, -1.65370256776216, 0.666505029533247, 1.11178514183042, 
    0.0370564222038934, 0.0466166840013479, -0.557572449898689, 
    -0.961796818639283, 0.936133529770083, -0.283139554139275, 
    -0.111006312537392, 0.655416096433155, 0.253351279215336, 
    -1.27117077274472, 0.821677299062997, 1.30061244464207, -0.571112110007776, 
    -0.978927643078376, -0.0939968112175565, 1.01351341523222, 
    -0.482667556069396, 0.416191169882131, -1.13218300732386, 
    0.0109719515636322, 0.570902411558704, 0.251628490921888, 
    0.757376045856767, -0.286245425992256, -0.850640576775985, 
    0.323694423760568, 0.33667599752107, 0.489882832518856, 0.0192219236731108, 
    -0.267852841112888, 1.81167395220041, -0.0196014744891418, 
    -0.0975879755475657, 1.11193072957353, 1.43135023795345, 
    -0.0901872652728838, 0.562191411596049, 0.0591857591563773, 
    1.10166679368438, -0.140293713526042, 0.0486791770287176, 
    0.34512008662136, 0.818328780971503, -0.238419852381131, 
    -0.368114266866903, 1.03486628422146, 0.539091489689507, 
    -0.233227222876931, -0.351614322647946, -0.108579850152251, 
    1.42892377556831, 0.96726504217144, -0.436055213650844, 0.565831105173759, 
    -1.54960733143962, 1.100259445501, -0.614861226811863, -0.361659876922429, 
    1.40706134947819, -0.552573937385301, 0.105482661464863, 
    -0.374326010572864, -0.0292830594058542, 1.98545718822419, 
    -1.19221368673224, 0.760991474810628, -0.539568099000947, 
    -0.0739542319162944, 0.34373700306183, 0.053071073945822, 
    0.0606901658351647, -0.211219209043705, 0.216590374080456, 
    1.00007081361854, -0.495649129829898, 0.0867746364754286, 
    -1.67723925289802, -0.826836980777755, 1.18863120556783, 
    -0.795681203752549, -0.650869928607352, 0.333545861044237, 
    0.63103014946249, -1.75748236397463, -0.118601139802882, 
    1.51474775013074, 0.132537717059181, -0.677973513449372, 
    0.467947612557183, 1.56786301174147, -0.118528345931329, 
    0.554596584330558, -0.59224659738235, -0.543498968064875, 
    -0.380440695783417, 0.0643541240367275, -0.210830975062082, 
    0.503471021875644, -0.660672836643319, 1.50676468888363, 
    0.780839937121078, -0.116635705270918, -0.240385286913095, 
    0.00044110481212036, 1.4571920623552, -0.350401091455376, 
    0.0850033189342734, 0.197882349091022, 0.726511444317778, 
    -0.331741595713643, 0.837497833814114, 0.609264781867777, 
    0.532151807268005, 0.65446977610295, -0.581909867621652, 
    0.322942220421174, 1.52993740466172, -0.264455793773691, 
    -0.0568476721010507, 1.05299195823846, 0.915435805624833, 
    0.297609953120304, -0.114257772133482, 0.248013061968027, 
    1.64822744593733, 1.26800079018578, -0.0459528559917708, 
    0.720784993088846, -1.05679282101754, 1.38345187047077, -0.64414862780051, 
    -1.25411274217718, 0.177548594303543, -0.257734492966852, 
    -0.818028922319694, 0.409736779937656, -0.216411838547905, 
    1.32536236097051, -0.500938817829506, 0.803770006660658), 
    Tcoded = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 3, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 2, 2, 2, 2, 3, 
    3, 3, 2, 3, 2, 3, 2, 2, 2, 3, 2, 2, 3, 3, 2, 3, 3, 2, 2, 
    3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 2, 3, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 4, 
    4, 4, 4, 4, 3, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
    3, 4, 4, 4, 3, 4, 3, 4, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 3, 
    4, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 3, 3, 4, 4, 3, 
    3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, 3, 3, 3, 3)), row.names = c(NA, 
-222L), class = c("tbl_df", "tbl", "data.frame"))

>Solution :

Try running each individual part of your problem to see what is happening

with(test,
  factor(levels(ID)[1], levels = levels(ID)))

returns

factor(0)
Levels: 

so has zero length and ruins everything else (because if any element of expland.grid has length zero the output data.frame has zero rows). You are trying to get the first level of ID before converting it to a factor. Don’t know if you just wanted the first ID? or the ID just as levels instead of as numeric?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading