Invalid Length Argument
12,300
Try using as.numeric
instead of numeric
:
dat <- as.data.frame(lapply(dat, as.numeric))
Author by
Adhiraj Chattopadhyay
Updated on June 09, 2022Comments
-
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 almost 6 yearsIt 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 almost 6 years@AdhirajChattopadhyay There is no practical difference between
as.data.frame
anddata.frame
here. The latter constructs a newdata.frame
, in this case by using a data structure that is passed as an argument, while the former coerces a given data structure into adata.frame
. Semantically I therefore preferas.data.structure
in this case. -
bhumika53 about 4 yearswhat is the difference between as.numeric and numeric ?
-
RHertel about 4 years
numeric
is used to declare a type of variable. Specifically, it creates a new vector of specified length of the typedouble
. In contrast to this,as.numeric
coerces existing data into numeric form, i.e., converts it intodouble
. If it helps, in other programming languages, a coercion/conversion like the one done byas.numeric
is called "type casting".