Search code examples
rdataframedata-sciencesapply

Invalid length argument


I want to convert all the columns of my dataframe to numeric format.

I use lapply - like this:

 data.frame(lapply(dat, numeric))

But I get an error from this code:

Invalid length argument

However, it is working when I tried with individual columns:

 lapply(dat$x.Type, numeric)

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

I am guessing the solution to my problem is to run a loop applying lapply through all the columns.

The problem is I am having trouble figuring out how to do that.

Could somebody help me?


Solution

  • Try using as.numeric instead of numeric:

    dat <- as.data.frame(lapply(dat, as.numeric))