When I import .csv files into R, all the variables are in character
format. Is there a way to automatically assign numeric
, factor
, and character
types without having to manually do this for each column?
Asked
Active
Viewed 404 times
0
-
2Please show what code you've used and a snapshot(copy-paste format or `dput`) of your data. – NelsonGon Mar 02 '19 at 17:45
-
`df %>% convert(num(var1), dte(var2))` – Ben Mar 02 '19 at 17:51
-
Add the package `rio`(I assume) to your question and also what the data looks like and what you need to convert it to. It's currently ambiguous. Also why not use such functions as `as.numeric`? – NelsonGon Mar 02 '19 at 17:52
-
You can do something like this in `dplyr`: `iris %>% mutate_at(vars(c("Sepal.Length","Petal.Length")),list(as.double))`. The syntax is for the latest `dplyr` version. You can do some form of `regex` match for the column names to convert. – NelsonGon Mar 02 '19 at 17:55
-
I know how to change each column individually, but I was wondering if there was a way to automatically assign data type or to assign multiple columns at once. – Ben Mar 02 '19 at 17:56
-
1`readr::read_csv` handles most of it automatically, unless you have all columns in quotes – Sonny Mar 02 '19 at 17:58
-
Take a look at [this](https://stackoverflow.com/questions/7680959/convert-type-of-multiple-columns-of-a-dataframe-at-once?rq=1) – NelsonGon Mar 02 '19 at 17:58
-
Here is other code I used. `cols_num = c("col_name") df[,cols_num] %<>% lapply(function(x) as.numeric(as.character(x)))` I don't want to have to write in each column name – Ben Mar 02 '19 at 17:59
-
This works as well `df$var <- as.numeric(df$var)` but still doesn't solve the problem of automatically changing column data types – Ben Mar 02 '19 at 18:03
-
Try something like: `to_change<-grep("^S",names(iris)) as.data.frame(apply(iris[,names(iris)[to_change]],2,as.factor)))`. Although `read.csv` should automatically do these for you. – NelsonGon Mar 02 '19 at 18:05
-
I get this error `Error: unexpected symbol in "to_change<-grep("^S",names(iris)) as.data.frame"` – Ben Mar 02 '19 at 18:12
-
2`read.table` and `read.csv` have the colClass argument where you can provide a character vector to define the types of each column. See `?read.table` for a description of this argument. If the automatic reader is interpreting these are characters, there may be a reason, such as one or more elements that can't be represented as numeric. – lmo Mar 02 '19 at 18:13
-
@bwn There's an extra bracket in the last call. My bad! – NelsonGon Mar 02 '19 at 18:19