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
Sabali Mail Logo
Domain Search Logo
Test Speed Logo
Website On Logo
Page Load Logo
Code Editor Logo
Format Code Logo
HTML Validator Logo
CSS Validator Logo
ASCII Table Logo
HTML Symbols Logo
Emoji Symbols Logo
Encode File Logo
Sitemap Generator Logo
Generator Password Logo
QR Code Generator Logo
Barcode Generator Logo
Online Sign Logo
Dictionary Online Logo
Counter Word Logo
Text Convert Logo
Lorem Ipsum Generator Logo
Sprite Sheet Logo
Edit Picture Logo
Resize Image Logo
Image Compress Logo
Image Color Logo
Image Crop Logo
Combine Images Logo
Favicon Generator Logo
Color Palette Logo
Color Picker Logo
Color Mix Logo
Color Convert Logo
CSS Gradient Logo
To-Do List Logo
Calendar Free Logo
Generator Meme Logo
Word Spinner Logo
Phone Country Logo
Sabalytics Logo
Senty Logo
World Map Logo
SEO Guide Logo
Keyword Tool Logo
What is my IP Logo
My Device Logo
My Browser Logo
My Location Logo
Time Zone Logo
Day Map Logo
My Weather Logo
My Galaxy Logo
The Moon Logo
Periodic Table Logo
rStatistics Logo
Unit Convert Logo
Data Convert Logo
Coordinate Converter Logo
Temperature Convert Logo
2020 Election Logo
Sabali Finance Logo
Currency Convert Logo
Free Calculator Logo
Finance Calculator Logo
Loan Calculator Logo
Calculator Mortgage Logo
Stock Calculator Logo
Bond Calculator Logo
Tax Calculator Logo
Tip Calculator Logo
Gas Mileage 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