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 3789250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:07:38+00:00 2026-05-19T12:07:38+00:00

Here’s a problem I’m encountering: Example Data df <- data.frame(1,2,3,4,5,6,7,8) df <- rbind(df,df,df,df) What

  • 0

Here’s a problem I’m encountering:

Example Data

df <- data.frame(1,2,3,4,5,6,7,8)
df <- rbind(df,df,df,df)

What I would like to do is find the p.value for the chisq.test of 1,2,3 vs. 4,5,6 in the data.frame defined above in the first row.

Let’s try it flat out:

chisq.test(c(1,2,3),c(4,5,6))$p.value ## this works.

But when I try to do it by calling the columns/rows…

chisq.test(df[1,1:3],df[1,4:6])$p.value

Gives: Error in complete.cases(x, y) : not all arguments have the same length

Interesting, because that doesn’t seem to be true:

length(df[1,1:3])
length(df[1,4:6])

Any thoughts on how to change the notation to get the desired result?

  • 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-05-19T12:07:39+00:00Added an answer on May 19, 2026 at 12:07 pm

    ?chisq.test tells us:

    Arguments:
    
           x: a numeric vector or matrix. ‘x’ and ‘y’ can also both be
              factors.
    
           y: a numeric vector; ignored if ‘x’ is a matrix.  If ‘x’ is a
              factor, ‘y’ should be a factor of the same length.
    

    If we look at df as per your Q, the subsets you define are:

    > is.numeric(df[1,1:3])
    [1] FALSE
    > is.vector(df[1,1:3])
    [1] FALSE
    > is.matrix(df[1,1:3])
    [1] FALSE
    

    and the same for your other subset. What happens then is in the lap of the God’s. What happens internally is that as df[1,1:3] is a data frame, it is converted first to a one column matrix, and thence to a vector:

    Browse[2]> x ## here x is df[1,1:3]
    [1] 1 2 3
    

    whilst df[1,4:6] (y in the chisq.test function) is left untouched:

    Browse[2]> y
      X4 X5 X6
    1  4  5  6
    

    and when the code calls complete.cases(x,y), we get the error you report:

    Browse[2]> complete.cases(x, y)
    Error in complete.cases(x, y) : not all arguments have the same length
    

    complete.cases calls internal code so we can’t see what is going on, but essentially R thinks x and y are not of the same length and this is because they are of different types.

    @Prasad provides a work around, namely unlisting the 2 data frames you supply to chisq.test into vectors.

    However, the way you are using the function doesn’t make much sense, to me at least. One would normally store the data in columns, rather than rows of a data frame. It might not appear like there is a difference, but the columns of the data frame are its components, like the components of a list. Each individual component (column) is a discrete entity, a vector of data on the /n/ observations in the data frame. If we transpose your df (and cast back to a data frame) to reflect a more natural data set-up:

    > df2 <- data.frame(t(df))
    

    then we can use the approach you did, but index the separate rows of the first column of df2 (rather than the separate columns of the first row of df) in the chisq.test call:

    > chisq.test(df2[1:3,1], df2[4:6,1])
    
        Pearson's Chi-squared test
    
    data:  df2[1:3, 1] and df2[4:6, 1] 
    X-squared = 6, df = 4, p-value = 0.1991
    
    Warning message:
    In chisq.test(df2[1:3, 1], df2[4:6, 1]) :
      Chi-squared approximation may be incorrect
    

    This works, because R is able to drop the empty dimension in both subsets, so both inputs are vectors of the appropriate length:

    > df2[1:3,1] ## drops the empty dimension!
    [1] 1 2 3
    > is.vector(df2[1:3,1])
    [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is another spoj problem that asks how to find the number of distinct
Here's a coding problem for those that like this kind of thing. Let's see
Here, I have one drop down with 3 value like 0, 1, 2 and
Here is an example of a problem I am attempting to solve and implements
Here is the code in a function I'm trying to revise. This example works
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is an example: I write html code inside of textarea, then I swap
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),

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.