R_day1(Introduce and DataType)

Today, we start to learn Computer Language R, R isn't easy to learn, we need do many practices and study theories. This is a long time process. We should insist to learn it. In my opinion, anything both simple and hard, if we could never give in or give up. In the end, we will get progress on these things.【Maybe, have some dear friends{bro or sister} looked my wrote about C articles. I am determinded to learn Big Data, I also like coding, it's a interesting thing for myself. I learn C the reason is it could communicate with other professional coder. I wantn't when they ask me :"Do you learn C?", then I answer them:"Sorry, I never touch C or C++." I think C is coder communicate basic language.】

The R History and why use R

R is created by Ross Ihaka and Robert Gentleman in 1994, the language is named by R. Maybe the name is source from inventor. R is basic on the S language. S-plus is used on business, but R is free【Open Source】. S-plus and R both Statistics Science Computer Language. So we know R is simple to do Data Visualization and Data Analytics. If R installed finish, you could use help() command see some function instructions. Maybe it could help you solve your Errors or Warnings. R comes with GUI window, let you feel high level better than black Dos window. R has many libraries could be used to help you clearify your complex tasks or work. You only need use commandinstall.packages("pkg_name") to install packages by CRAN Sever or install_github("author_name/pkg_name")to download packages from github. Except those above conditions, R still has a strong community used to learn it or communicate problems.

The R Data Type

Now, we look this example is about Integer type and Boolean type【Logical type】.

r 复制代码
num1 = 34
print(class(num1))#Integer type

num2 = 45#class function could explain the data type.

bool_result = num1>num2
bool_result2 <- num1<num2
print(class(bool_result))
print(bool_result)
print(bool_result2)
subtract_result = bool_result + bool_result2
print(subtract_result)#boolean type is could be used to calculate

print(exp(1000))
print(-10/0)
print(exp(1000)/exp(980))
print(exp(20))

Output result:

through the program, we know class() function could describe the data type, Logical type could be used to calculate. Value type also have Inf, -Inf and NaN【Not a Number】three special circumstances,Exponent divid Exponent, result is NaN, isn't Exponent(20).

Next, we see one example is about Character type【char or string type】 and factor type.

r 复制代码
var1 <- c("R is make me happy!!")
var2 <- c('I love R!!')
# print(var2)
fact1 <- factor(var1)
print(fact1)
print(class(fact1))
print(object.size(fact1))#ouput the factor occupy storage space sizes.
print(var1)
print(class(var1))
print(object.size(var1))#ouput the string occupy storage space sizes.

#through the contrast, we know the factor is biger than string on occupy storage space. 

character1 = c("man","woman","man","woman")
character2 <- factor(c("man","woman","man","woman"))
character3 <- factor(character1)
print(character1)
print(object.size(character1))
print(character2)
print(object.size(character2))
print(character3)
print(object.size(character3))

Output result:

through the above program, we know Character type is use single quotation mark or double quotation marks definition. Character type is include Char type and String type in C, R is like python in this regard. We also know factor type occupy space is biger than character type, but factor could classify character. Through character2 and character3 factor convert new character and convert created character isn't change the result.

Then, we look the Time type example.

r 复制代码
DateTime <- c("2019/4/5","2017/5/7","1987/7/3","1874/9/2")
Translate_Time <- as.POSIXct(DateTime)
Translate_Time2 <- as.Date(DateTime)
print(Translate_Time)
print(class(Translate_Time))
print(Translate_Time2)
print(class(Translate_Time2))
Translate_Time3 <- as.POSIXlt(DateTime)
print(Translate_Time3)
print(class(Translate_Time3))

Output result:

from above program, we know the Time type has three ways to express them. POSIXct() function and POSIXlt() function have same role, it both include date, time, timezone. Date() function is convert input data to date.

After, we continue to look this example.

r 复制代码
Time <- c("2016-03-04","2017-02-19","2012-9-23","2019-10-21")
Time_change <- factor(Time)
Time2 <- as.POSIXct(Time)
Time3 = as.Date.numeric(Time_change)
print(Time)
print(Time2)
print(Time_change)
print(Time3)

print(Sys.time())
print(class(Sys.time()))
print(format(Sys.time(),format = "%B %d %Y"))
print(format(Sys.time(),format = "%Y/%B/%a %H:%M:%S"))

Output result:

In above example, we know time could factor convert is used to order. Sys.time() could gain the System current time, it's POSIXct or POSIXt, it also could use format to extract several informations.

Next, we could use commandinstall.packages("lubridate") to install the third party library. It will help us easily to process time type. Please look the below examples.

program1:

r 复制代码
library(lubridate)
data = c(20090101,"2009-01-02","2008 02 04","2007-3-5","Created on 2006 4 7","199704 !!! 04")
Data = ymd(data)
print(Data)

print(mday(as.Date("2017-12-23")))
print(wday(as.Date("2014-9-24")))#the result output is last number of day value.
print(hour(as.POSIXct("2013-5-12 05:43:04")))#the result output is the hour position number.
print(minute(as.POSIXct("2017-6-21 03:21:06")))#the result output is the minute position number.
print(second(as.POSIXct("2019-3-15 18:31:08")))#the result output is the second position number.

Output result:

From the program, we know ymd() function could convert data to time type, mday() function could gain the day value in date, wday() function could return the last number of day value, hour() function could gain the hour position value, minute() function could gain the minute position value, second() function could gain the second position value.

program2:

r 复制代码
Begin_Time <- as.Date("2017-02-12")
End_Time = as.Date("2017-05-3")
Result = End_TIme - Begin_Time#declare to subtract calculation gain days
print(Result)
difftime(End_Time,Begin_Time,units = "weeks")#the subtract result translate to weeks
difftime(End_Time,Begin_Time,units = "hours")#the subtract result translate to hours

Output result:

Through above result, we know Date type could subtract calculate, it also could use difftime() function to finish the calculation and convert to weeks or hours.

At last, I think study is a long time process. If you could insist to do it, I admire you, please remember anything easy or complex, you need serious to treat it, that you will get progress on these things. Maybe today is terrible, tomorrow also terrible, but if you insist to last, you will win, Please continue to learn knowleges, my dear friends, Cheer!!

相关推荐
Auc247 分钟前
OJ判题系统第6期之判题逻辑开发——设计思路、实现步骤、代码实现(策略模式)
java·开发语言·docker·容器·策略模式
向日葵xyz13 分钟前
Qt5与现代OpenGL学习(十一)OpenGL Widget鼠标控制直线旋转
开发语言·qt·学习
智慧地球(AI·Earth)16 分钟前
OpenAI for Countries:全球AI基础设施的“技术基建革命”
开发语言·人工智能·php
不学无术の码农20 分钟前
《Effective Python》第1章 Pythonic 思维总结——编写优雅、高效的 Python 代码
开发语言·python
双叶8361 小时前
(C语言)超市管理系统(测试版)(指针)(数据结构)(二进制文件读写)
c语言·开发语言·数据结构·c++
PXM的算法星球1 小时前
使用CAS操作实现乐观锁的完整指南
开发语言
TDengine (老段)1 小时前
基于 TSBS 标准数据集下 TimescaleDB、InfluxDB 与 TDengine 性能对比测试报告
java·大数据·开发语言·数据库·时序数据库·tdengine·iotdb
rylshe13142 小时前
在scala中sparkSQL连接mysql并添加新数据
开发语言·mysql·scala
小宋加油啊2 小时前
Mac QT水平布局和垂直布局
开发语言·qt·macos
MyhEhud2 小时前
kotlin @JvmStatic注解的作用和使用场景
开发语言·python·kotlin