I have a data.frame, d1, that has 7 columns, the 5th through 7th column are supposed to be numeric:
str(d1[5])
'data.frame': 871 obs. of 1 variable:
$ Latest.Assets..Mns.: num 14008 1483 11524 1081 2742 ...
is.numeric(d1[5])
[1] FALSE
as.numeric(d1[5])
Error: (list) object cannot be coerced to type 'double'
How can this be? If str identifies it as numeric, how can it not be numeric? I’m importing from CSV.
Why
d1is a list, henced1[5]is a list of length 1, and in this case contains adata.frame. to get the data frame, used1[[5]].Even if a data frame contains numeric data, it isn’t numeric itself:
Individual columns in a data frame are either numeric or not numeric. For instance:
If you want to know if ALL columns in a data frame are numeric, you can use
allandsapply:You can wrap this all up in a convenient function: