daDoctoR/R/col_num.R

26 lines
543 B
R
Raw Normal View History

2018-10-02 21:07:43 +02:00
#' Formatting multiple columns as numeric
#'
#' Depending on dply's contains()-function.
#' @param string Columnnames containg strings.
#' @param data Dataframe
#' @keywords numeric
#' @export
col_num<-function(string,data){
## Defining factors for columns containing string (can be vector of multiple strings), based on dplyr
require(dplyr)
d<-data
s<-string
n<-c()
2018-10-09 14:27:07 +02:00
2018-10-02 21:07:43 +02:00
for(i in 1:length(s)){
2018-10-09 14:27:07 +02:00
n<-c(n,names(dplyr::select(d,dplyr::contains(s[i]))))
2018-10-02 21:07:43 +02:00
}
2018-10-09 14:27:07 +02:00
2018-10-02 21:07:43 +02:00
for(i in 1:length(n)) {
d[,n[i]]<-as.numeric(d[,n[i]])
}
return(d)
}