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

相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再2 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame