Data Types in R

id
carat
cut
color
clarity
depth
table
price
x
y
z
1 0.23 NA E SI2 61.5 55.0 326 3.95 3.98 2.43
2 0.21 NA E SI1 59.8 61.0 326 3.89 3.84 2.31
3 0.23 NA E VS1 56.9 65.0 327 4.05 4.07 2.31
4 0.29 NA I VS2 62.4 58.0 334 4.20 4.23 2.63
5 0.31 NA J SI2 63.3 58.0 335 4.34 4.35 2.75
6 0.24 NA J VVS2 62.8 57.0 336 3.94 3.96 2.48
7 0.24 NA I VVS1 62.3 57.0 336 3.95 3.98 2.47

Load libraries

library(tidyverse)

Load dataset

df <- diamonds

Get Data Structure

Using str() gets you that information plus additional such as the levels of your factors and the first few values of each variable.

str(diamonds$carat)

Inspect Data Class

class(diamonds$carat)

Set Data Column as Numeric

df$depth <- as.numeric(df$depth)

Set Data Column as Factor

df$cut <- as.factor(df$cut)

Set Data Column as Logical

df$cut <- as.logical(df$cut)

Set Data Column as Character

df$cut <- as.character(df$cut)

Set Data Column as Date/Time

df$date <- as.POSIXct(df$date, format="%Y-%m-%d %H:%M:%S", tz="UTC")

DescriptionCode
Year (4 Digits)%Y
Year (2 Digits)%y
Month (Full)%B
Month (Abbreviated)%b OR %h
Month (Decimal)%m
Week of Year (Start=Sunday)%U
Week of Year (Start=Monday)%W
Day of Year (Decimal)%j
Day of Month (Decimal)%d
Weekday (Full)%A
Weekday (Abbreviated)%a
Weekday (0=Sunday)%w
Hours (24 Hrs)%H
Hours (12 Hrs)%I
Minutes%M
Seconds%S
Locale-Specific Date & Time%c
Locale-Specific Date%x
Locale-Specific Time%X
Locale-Specific AM/PM%p
Offset from GMT%z
Time Zone%Z

Years Example (%Y-%y)

format(Sys.time(), "%Y-%y")

Months Example (%B-%b-%h-%m)

format(Sys.time(), "%B-%b-%h-%m")

Weeks Example (%U-%W)

format(Sys.time(), "%U-%W")

Days Example (%j-%d)

format(Sys.time(), "%j-%d")

Weekdays Example (%A-%a-%w)

format(Sys.time(), "%A-%a-%w")

Time Example (%H-%I-%M-%S)

format(Sys.time(), "%H-%I-%M-%S")

Get Current Date/Time

format((Sys.Date()), format="%Y-%m-%d")

Set Time Zone

Sys.setenv(TZ = "UTC") # Sys.timezone()

References:

https://stackoverflow.com/questions/21125222/determine-the-df-types-of-a-df-frames-columns

https://stackoverflow.com/q/8855589/1217536

Sabalico Logo
Sabalytics Logo
Senty Logo
SEO Guide Logo
World Map Logo
rStatistics Logo
Day Map Logo
Time Zone Logo
Galaxy View Logo
Periodic Table Logo
My Location Logo
My Weather Logo
Sprite Sheet Logo
Barcode Generator Logo
Test Speed Logo
Website Tools Logo
Image Tools Logo
Color Tools Logo
Text Tools Logo
Finance Tools Logo
File Tools Logo
Data Tools Logo
History of Humanity - History Archive Logo
History of Humanity - History Mysteries Logo
History of Humanity - Ancient Mesopotamia Logo
History of Humanity - Egypt History Logo
History of Humanity - Persian Empire Logo
History of Humanity - Greek History Logo
History of Humanity - Alexander the Great Logo
History of Humanity - Roman History Logo
History of Humanity - Punic Wars Logo
History of Humanity - Golden Age of Piracy Logo
History of Humanity - Revolutionary War Logo
History of Humanity - Mafia History Logo