r - Numeric Column in data.frame returning "num" with str() but not is.numeric() -
i have data.frame, d1, has 7 columns, 5th through 7th column supposed 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 coerced type 'double'   how can be? if str identifies numeric, how can not numeric? i'm importing csv.
> is.numeric_data.frame=function(x)all(sapply(x,is.numeric))  > is.numeric_data.frame(d1[[5]]) [1] true    why
d1 list, hence d1[5] list of length 1, , in case contains data.frame.  data frame, use d1[[5]].
even if data frame contains numeric data, isn't numeric itself:
> x = data.frame(1:5,6:10) > is.numeric(x) [1] false   individual columns in data frame either numeric or not numeric. instance:
> z <- data.frame(1:5,letters[1:5])  > is.numeric(z[[1]]) [1] true > is.numeric(z[[2]]) [1] false   if want know if columns in data frame numeric, can use all , sapply:
> sapply(z,is.numeric)     x1.5 letters.1.5.      true        false   > all(sapply(z,is.numeric)) [1] false  > all(sapply(x,is.numeric)) [1] true   you can wrap in convenient function:
> is.numeric_data.frame=function(x)all(sapply(x,is.numeric))  > is.numeric_data.frame(d1[[5]]) [1] true       
Comments
Post a Comment