Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

scale a df but it's not numeric?

I have a df (called env) of environmental measurements I’m looking to scale by using the following code:

scale.env <- scale(env, center=TRUE, scale=TRUE)

but I’m getting the error

scale.env <- scale(env, center=TRUE,scale=TRUE)
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

I tried to convert env to numeric but received the same error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

env <-lapply(env, as.numeric)
> str(env)
List of 5
 $ X00940_Cl_mg_per_L        : num [1:282] 0.3 0.31 0.31 1.83 2 ...
 $ X00945_SO4_mg_per_L_as_SO4: num [1:282] 9.8 8.03 8.62 6.53 6.66 ...
 $ X71851_NO3_mg_per_L_as_NO3: num [1:282] 0.19 0.23 0.23 NA NA NA NA 0.96 0.38 6.43 ...
 $ X00950_F_mg_per_L         : num [1:282] 0.07 0.07 0.06 0.07 0.08 NA 0.13 0.16 0.16 NA ...
 $ X00602_TN_mg_per_L_as_N   : num [1:282] 1.19 0.58 0.65 0.8 0.74 NA 0.7 1.44 1.12 1.55 ...

Any insight is appreciated!!
Please note the dput below is before I convert to numeric.

dput(env)
structure(list(X00940_Cl_mg_per_L = c("0.3", "0.31", "0.31", 
"1.83", "2"), X00945_SO4_mg_per_L_as_SO4 = c(9.8, 8.03, 8.62, 
6.53, 6.66), X71851_NO3_mg_per_L_as_NO3 = c("0.19", "0.23", "0.23", 
NA, NA), X00950_F_mg_per_L = c("0.07", "0.07", "0.06", "0.07", 
"0.08"), X00602_TN_mg_per_L_as_N = c("1.19", "0.58", "0.65", 
"0.8", "0.74")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

>Solution :

If you want to write the data that lapply produces back into a data frame, use the [] notation. This will preserve your data frame’s attributes

env[] <- lapply(env, as.numeric)

Then you can do

scale(env, center = TRUE, scale = TRUE)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading