Invalid Length Argument

12,300

Try using as.numeric instead of numeric:

dat <- as.data.frame(lapply(dat, as.numeric))
Share:
12,300
Adhiraj Chattopadhyay
Author by

Adhiraj Chattopadhyay

Updated on June 09, 2022

Comments

  • Adhiraj Chattopadhyay
    Adhiraj Chattopadhyay almost 2 years

    I want to convert all the coloumns of my dataframe to numeric format. So I use lapply

     data.frame(lapply(dat, numeric))
    

    But this is showng me an invalid length argument error. However, it is working when I tried with individual coloumns.

     lapply(dat$x.Type, numeric)
    

    But then again I am left to wonder how to update the orginal dataframe with this.

    I am guessing the solution to my problem is to run a loop applying lapply through all the coloumns . The problem is I am having trouble figuring out how to do that.

    Could somebody help me?

  • Adhiraj Chattopadhyay
    Adhiraj Chattopadhyay almost 6 years
    It worked thankyou. Could you explain this in more detail though? I mean what is basically the difference between [as.data.frame] & [data.frame] over here?
  • RHertel
    RHertel almost 6 years
    @AdhirajChattopadhyay There is no practical difference between as.data.frame and data.frame here. The latter constructs a new data.frame, in this case by using a data structure that is passed as an argument, while the former coerces a given data structure into a data.frame. Semantically I therefore prefer as.data.structure in this case.
  • bhumika53
    bhumika53 about 4 years
    what is the difference between as.numeric and numeric ?
  • RHertel
    RHertel about 4 years
    numeric is used to declare a type of variable. Specifically, it creates a new vector of specified length of the type double. In contrast to this, as.numeric coerces existing data into numeric form, i.e., converts it into double. If it helps, in other programming languages, a coercion/conversion like the one done by as.numeric is called "type casting".