Changing class on columns i a dataframe

Given a dataframe with some columns that has a “wrong” class, eg character, that should be numeric. How to make that change.

i <- as.character(1:10)
c <- rep("a",10)
c
##  [1] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
df <- data.frame(a=i,b=i, c=c, d=i, stringsAsFactors = FALSE)
sapply(df,class)
##           a           b           c           d 
## "character" "character" "character" "character"

The dataframe has four columns. The columns a,b and d are characters, but should be numeric.

df[,c("a","b","d")] <- sapply(df[,c("a","b","d")], as.numeric)
sapply(df,class)
##           a           b           c           d 
##   "numeric"   "numeric" "character"   "numeric"

An important thing to note here is that apply functions a bit different from the other members of the apply-family:

apply(df,2,class)
##           a           b           c           d 
## "character" "character" "character" "character"
sapply(df,class)
##           a           b           c           d 
##   "numeric"   "numeric" "character"   "numeric"

What happens is that apply coerces df to an array. And all content in an array have to be of the same class. The lowest common denominator for that is character.

if (!require('knitr')) {
  install.packages("knitr")
}
if (!require('devtools')) {
  install.packages("devtools")
}
if (!require('RWordPress')) {
  devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
library(RWordPress)
library(knitr)

options(WordPressLogin=c(ChristianKnudsen="2ZKwcFS6vD*fqWMQ"),
        WordPressURL="https://christianknudsen.info/xmlrpc.php")
knit2wp('classchange.Rmd', title = 'Changing class on columns i a dataframe',publish = FALSE)
## 
## 
## processing file: classchange.Rmd
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |.......                                                          |  11%
##   ordinary text without R code
## 
## 
  |                                                                       
  |..............                                                   |  22%
## label: unnamed-chunk-55
## 
  |                                                                       
  |......................                                           |  33%
##   ordinary text without R code
## 
## 
  |                                                                       
  |.............................                                    |  44%
## label: unnamed-chunk-56
## 
  |                                                                       
  |....................................                             |  56%
##   ordinary text without R code
## 
## 
  |                                                                       
  |...........................................                      |  67%
## label: unnamed-chunk-57
## 
  |                                                                       
  |...................................................              |  78%
##   ordinary text without R code
## 
## 
  |                                                                       
  |..........................................................       |  89%
## label: unnamed-chunk-58
## 
  |                                                                       
  |.................................................................| 100%
##   ordinary text without R code
## output file: classchange.md
## [1] "152"
## attr(,"class")
## [1] "WordpressPostId"
options(WordPressLogin=c(admin="7g0G9!27"),
        WordPressURL="https://nusse.dk/xmlrpc.php")
knit2wp('classchange.Rmd', title = 'Changing class on columns i a dataframe',publish = FALSE)
## 
## 
## processing file: classchange.Rmd
## Error in parse_block(g[-1], g[1], params.src): duplicate label 'unnamed-chunk-3'