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

相关推荐
yaoxin5211231 天前
211. Java 异常 - Java 异常机制总结
java·开发语言·python
Empty_7771 天前
编程之python基础
开发语言·python
疯狂吧小飞牛1 天前
Lua 中的 __index、__newindex、rawget 与 rawset 介绍
开发语言·junit·lua
寻星探路1 天前
Java EE初阶启程记13---JUC(java.util.concurrent) 的常见类
java·开发语言·java-ee
哲Zheᗜe༘1 天前
了解学习Python编程之python基础
开发语言·python·学习
Tiger Z1 天前
《R for Data Science (2e)》免费中文翻译 (第10章) --- Exploratory data
r语言·数据科学·中文翻译
落日漫游1 天前
数据结构笔试核心考点
java·开发语言·算法
寻找华年的锦瑟1 天前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
HY小海1 天前
【C++】AVL树实现
开发语言·数据结构·c++
workflower1 天前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程