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!!

相关推荐
倔强的石头1068 分钟前
【C++指南】vector(一):从入门到详解
开发语言·c++
预测模型的开发与应用研究20 分钟前
R语言实现轨迹分析--traj和lcmm包体会
机器学习·数据分析·r语言
ITLiu_JH21 分钟前
scikit-surprise 智能推荐模块使用说明
开发语言·数据分析·智能推荐
User_芊芊君子29 分钟前
【Java】——数组深度解析(从内存原理到高效应用实践)
java·开发语言
一 乐1 小时前
网红酒店|基于java+vue的网红酒店预定系统(源码+数据库+文档)
java·开发语言·数据库·毕业设计·论文·springboot·网红酒店预定系统
DARLING Zero two♡2 小时前
C++底层学习精进:模板进阶
开发语言·c++·模板
勘察加熊人2 小时前
c++生成html文件helloworld
开发语言·c++·html
xyliiiiiL3 小时前
从责任链模式聊到aware接口
java·开发语言
Elec_z4 小时前
网络深处的守门人
开发语言·网络
闪电麦坤956 小时前
C#:Time.deltaTime
开发语言·c#