Excuse my lack of programming expertise, but this issue keeps appearing very often in my codes in R.
I upload a dataset with read.csv
as:
db <- read.csv("data.csv", sep = ",")
typeof(db)
returns the database imported as a list.
Importing as:
db <- as.matrix(read.csv("data.csv", sep = ","))
typeof(db)
returns the dataset imported as double (the database contains homogeneous numeric data types).
Finally, the following:
db <- as.matrix(read.csv("data.csv", sep = ",")) %>% as_tibble()
typeof(db)
returns the dataset imported as list again. Hence, the conversion to a tibble converts the data type from double to list.
Since I oftern use tibbles and generally double data type is much more amenable to various applications, is there a way to import/convert the database as a matrix and at the same time as tibble class?
You cannot have an object of class matrix and tibble at the same time. To check the class of an object use class(db)
instead of typeof(db)
.
To import the data as tibble use readr::read_csv
-
db <- readr::read_csv("data.csv")
class(db)