I have imported an excel spreadsheet into R and the data frame has numerous columns that should be numeric. I can format a named column as numeric as follows:
df$quantity <- as.numeric(df$quantity)
How would I do this for certain named columns? Here's an example data frame, though it doesn't have anywhere near as many columns as the real thing. Ideally the answer would use dplyr.
cols.to.format <- c("quantity", "li_hep", "edta")
df <- structure(list(source = c("Biobank", "Biobank", "Biobank", "Biobank",
"Biobank"), sample_type = c("EDTA Plasma Large Aliquot", "EDTA Plasma Large Aliquot",
"EDTA Plasma Large Aliquot", "EDTA Plasma Large Aliquot", "EDTA Plasma Large Aliquot"
), quantity = c("10", "3", "8", "0", "7"), li_hep = c("0", "0",
"0", "0", "0"), edta = c("2", "2", "0", "0", "0")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))