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

相关推荐
哈哈不让取名字6 小时前
基于C++的爬虫框架
开发语言·c++·算法
花间相见6 小时前
【JAVA开发】—— Nginx服务器
java·开发语言·nginx
扶苏-su6 小时前
Java---Properties 类
java·开发语言
一条咸鱼_SaltyFish8 小时前
远程鉴权中心设计:HTTP 与 gRPC 的技术决策与实践
开发语言·网络·网络协议·程序人生·http·开源软件·个人开发
我即将远走丶或许也能高飞8 小时前
vuex 和 pinia 的学习使用
开发语言·前端·javascript
沐知全栈开发8 小时前
SQL LEN() 函数详解
开发语言
钟离墨笺8 小时前
Go语言--2go基础-->基本数据类型
开发语言·前端·后端·golang
小郭团队9 小时前
1_7_五段式SVPWM (传统算法反正切+DPWM3)算法理论与 MATLAB 实现详解
开发语言·嵌入式硬件·算法·matlab·dsp开发
C+-C资深大佬9 小时前
C++风格的命名转换
开发语言·c++
No0d1es9 小时前
2025年粤港澳青少年信息学创新大赛 C++小学组复赛真题
开发语言·c++