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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: invalid type (list) for variable – quick tips
Next
Answered
Louis da Silva
  • 20
Louis da Silva
Asked: May 18, 20222022-05-18T11:02:30+00:00 2022-05-18T11:02:30+00:00In: r

Error: invalid type (list) for variable – quick tips

  • 20

. Advertisement .

..3..

. Advertisement .

..4..

As advised, I used some code samples in another forum, but it could not improve the problem. My question is the “invalid type (list) for variable” in r – How to solve it? The command line is:

data <- read.table("usedcar.dat", row.names = 1)
 young <- data[1,]
 med <- data[2,]
 old <- data[3,]
 Price <- c(young, med, old)
 Age <- as.factor(c(rep(1,12), rep(2,12), rep(3,12)))
 data <- cbind(Age, Price)
 data <- as.data.frame(data)
 replicate(12, rnorm(3))
 str(data)
 'data.frame': 36 obs. of 2 variables:
  $ Age :List of 36
  ..$ 1 : int 1
  ..$ 2 : int 1
  ..$ 3 : int 1
  ...
  ..$ 36: int 3
  $ Price:List of 36
  ..$ 1 : int 2300
  ...
  ..$ 36: int 2075

and the result:

m1 <- aov(Price ~ Age, data = data)
 Error in model.frame.default(formula = Price ~ Age, data = data, drop.unused.levels = TRUE) : invalid type (list) for variable 'Price'

What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.

invalid type for variable
  • 2 2 Answers
  • 250 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-12T08:48:58+00:00Added an answer on June 12, 2022 at 8:48 am

    The cause: Because data frames’ default rows are lists, but not numeric vectors. When you read.table(), you obtain a data frame (Therefore, creating a matrix won’t change anything).

    data <- as.data.frame(matrix(rnorm(36),nrow=3))
    young <- data[1,]; med <- data[2,]; old <- data[3,]
    Price <- c(young, med, old)
    str(Price)
    ## ## List of 36
    ## ## $ V1 : num 0.648
    ## ## $ V2 : num 0.157
    ## ## ...

    Solution: The issue is that this is a list rather than a numeric vector. So, Unlist() is a simple technique to deal with this:

    dd <- data.frame(Age,Price=unlist(Price))
    aov(Price~Age,dd)
    • 23
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Rose Lepage
    2022-05-25T20:20:23+00:00Added an answer on May 25, 2022 at 8:20 pm

    invalid type (list) for variable x is an error message that lm and other formula-based functions give. It indicates that x was not expecting a vector but instead a list. The str(data_frame_name$x) result is a standard way to debug the error. data_frame_name is the data frame containing x. You will usually find x not the data type you expected.

    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.