Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:47:00+00:00 2026-06-13T16:47:00+00:00

I’ve a data frame that summarises the number of missing and non-missing observations in

  • 0

I’ve a data frame that summarises the number of missing and non-missing observations in a data frame that is passed to it[1]. I then have been asked to test for differences between two treatment arms in the data I have (personally I disagree with the need or utility of doing so, but its what I’ve been asked to do). So I’ve written a small function to do this…

quick.test <- function(x, y){
  chisq   <- chisq.test(x = x,  y = y)
  fisher  <- fisher.test(x = x, y = y)
  results <- cbind(chisq  = chisq$statistic,
                   df     = chisq$parameter,
                   p      = chisq$p.value,
                   fisher = fisher$p.value)
  results
}

I then use apply() to pass the relevant columns to this function as follows…

apply(miss.t1, 1, function(x) quick.test(x[2:3], x[4:5]))

This is fine for the above specified miss.t1 data frame, but I’m working with time-series data and have three time-points I wish to summarise so have miss.t2 and miss.t3 (each of which is summarising the number of present/missing data for each time point, and have been created in the same manner using the function described in [1]).

miss.t2 fails with the following error…

apply(miss.t2, 1, function(x) quick.test(x[2:3], x[4:5]))
Error in chisq.test(x = x, y = y) : 
  'x' and 'y' must have at least 2 levels

My initial thought was that one of the columns had a missing value for some reason, but this doesn’t appear to be the case…

> describe(miss.t2)
miss.t2 

 5  Variables      171  Observations
--------------------------------------------------------------------------------
variable 
      n missing  unique 
    171       0     171 

lowest : Abtotal   Abyn      agg_ment  agg_phys  All.score
highest: z_pf      z_re      z_rp      z_sf      z_vt      
--------------------------------------------------------------------------------
nmiss.1 
      n missing  unique    Mean 
    171       0       4   8.649 

0 (6, 4%), 8 (9, 5%), 9 (153, 89%), 10 (3, 2%) 
--------------------------------------------------------------------------------
npresent.1 
      n missing  unique    Mean 
    171       0       4   9.351 

8 (3, 2%), 9 (153, 89%), 10 (9, 5%), 18 (6, 4%) 
--------------------------------------------------------------------------------
nmiss.2 
      n missing  unique    Mean 
    171       0       4   10.65 

0 (6, 4%), 11 (160, 94%), 12 (4, 2%), 13 (1, 1%) 
--------------------------------------------------------------------------------
npresent.2 
      n missing  unique    Mean 
    171       0       4   14.35 

12 (1, 1%), 13 (4, 2%), 14 (160, 94%), 25 (6, 4%) 
--------------------------------------------------------------------------------

Next thing I tried was trying subsets of miss.t2 by taking head(miss.t2, n=XX) and it works fine upto row 54…

> apply(head(miss.t2, n=53), 1, function(x) quick.test(x[2:3], x[4:5]))
     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
[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
[2,] 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  1  1  1  1  1  1  1  1  1
[4,] 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
     29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
[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
[2,]  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  1  1  1  1  1  1
[4,]  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
There were 50 or more warnings (use warnings() to see the first 50)
> apply(head(miss.t2, n=54), 1, function(x) quick.test(x[2:3], x[4:5]))
Error in chisq.test(x = x, y = y) : 
  'x' and 'y' must have at least 2 levels
> miss.t2[54,]
   variable nmiss.1 npresent.1 nmiss.2 npresent.2
54      psq      10          8      11         14
> traceback()
5: stop("'x' and 'y' must have at least 2 levels") at #2
4: chisq.test(x = x, y = y) at #2
3: quick.test(x[2:3], x[4:5])
2: FUN(newX[, i], ...)
1: apply(head(miss.t2, n = 54), 1, function(x) quick.test(x[2:3], 
       x[4:5]))

Similarly with the ‘bottom’ of the data frame the last 26 rows are parsed fine, but not the 27th from last…

> apply(tail(miss.t2, n=26), 1, function(x) quick.test(x[2:3], x[4:5]))
     146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
[1,]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
[2,]   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
[4,]   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
     164 165 166 167 168 169 170 171
[1,]   0   0   0   0   0   0   0   0
[2,]   1   1   1   1   1   1   1   1
[3,]   1   1   1   1   1   1   1   1
[4,]   1   1   1   1   1   1   1   1
There were 26 warnings (use warnings() to see them)
> apply(tail(miss.t2, n=27), 1, function(x) quick.test(x[2:3], x[4:5]))
Error in chisq.test(x = x, y = y) : 
  'x' and 'y' must have at least 2 levels
In addition: Warning message:
In chisq.test(x = x, y = y) : Chi-squared approximation may be incorrect

> miss.t2[118,]
    variable nmiss.1 npresent.1 nmiss.2 npresent.2
118     sf16       9          9      11         14

I can’t see anything wrong with these two lines that means they should fail and the traceback() shown above doesn’t reveal anything useful (to my mind).

Can anyone offer any suggestions as to why or where things are going wrong?

Many thanks in advance,

Neil

EDIT : Formatted reply to Vincent Zoonekynd …

I opted for the chisq.test(x = x, y = y) version described in ?chisq.test(), using cbind() as you suggest to produce a matrix results in
Error in sum(x) : invalid ‘type’ (character) of argument.

Putting print statements and showing the length of x and y results in the same error, but shows the values and lenghts as being…

> miss.t2.res <- data.frame(t(apply(miss.t2, 1, function(x) quick.test(x[2:3], x[4:5])))) 
[1] "Your x is : 9" "Your x is : 9" 
[1] 2    ### < Length of x
[1] "Your y is : 11" "Your y is : 14"
[1] 2    ### < Length of y
Error in chisq.test(x = x, y = y) : 'x' and 'y' must have at least 2 levels

EDIT 2 : Thanks to Vincent Zoonekynd pointers the problem was that because the counts were identical for two cells the call to chisq.test() treats these as factors and collapses them. The solution was to modify the quick.test() function and coerce the arguments that are being passed into a matrix, so the function that worked is now….

quick.test <- function(x, y){
  chisq   <- chisq.test(rbind(as.numeric(x), as.numeric(y)))
  fisher  <- fisher.test(rbind(as.numeric(x), as.numeric(y)))
  results <- cbind(chisq  = chisq$statistic,
                   df     = chisq$parameter,
                   p      = chisq$p.value,
                   fisher = fisher$p.value)
  results
}

Many thanks for the help & pointers Vincent, very much appreciated.

[1] http://gettinggeneticsdone.blogspot.co.uk/2011/02/summarize-missing-data-for-all.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:47:01+00:00Added an answer on June 13, 2026 at 4:47 pm

    The solution suggested by Vincent Zoonkeynd in the comments above, was to modify the quick.test() function and coerce the arguments that are being passed into a matrix, so the function that worked is now….

    quick.test <- function(x, y){
      chisq   <- chisq.test(rbind(as.numeric(x), as.numeric(y)))
      fisher  <- fisher.test(rbind(as.numeric(x), as.numeric(y)))
      results <- cbind(chisq  = chisq$statistic,
                       df     = chisq$parameter,
                       p      = chisq$p.value,
                       fisher = fisher$p.value)
      results
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.