#分步操作
a <- exp(2)/3+abs(tan(-pi/3))
print(a)
## [1] 4.19507
a<-round(a,3)
print(a)
## [1] 4.195
typeof(a)
## [1] "double"
#合并操作
typeof(round(exp(2)/3+abs(tan(-pi/3)),3))
## [1] "double"
#分步操作
a <- log2(sin(pi/3))
print(a)
## [1] -0.2075187
a<-floor(a)
print(a)
## [1] -1
typeof(a)
## [1] "double"
#合并操作
typeof(floor(log2(sin(pi/3))))
## [1] "double"
#分步操作
a <- cos(pi/4)*sqrt(5)+logb(23,4)
print(a)
## [1] 3.84292
a<-ceiling(a)
print(a)
## [1] 4
as.character(a)
## [1] "4"
#合并操作
as.character(ceiling(cos(pi/4)*sqrt(5)+logb(23,4)))
## [1] "4"
all.equal(sqrt(3),tan(pi/3))
## [1] TRUE
for(i in 1:100)
{
if(i%%2==0 & i%%3==0){
print(i)
}
}
## [1] 6
## [1] 12
## [1] 18
## [1] 24
## [1] 30
## [1] 36
## [1] 42
## [1] 48
## [1] 54
## [1] 60
## [1] 66
## [1] 72
## [1] 78
## [1] 84
## [1] 90
## [1] 96
is.character(1)
## [1] FALSE
is.character("1")
## [1] TRUE
is.character("one")
## [1] TRUE
is.numeric(1)
## [1] TRUE
is.numeric("1")
## [1] FALSE
is.numeric("one")
## [1] FALSE
由代码运行结果知 1 是数值型,”1”, “one”是字符型。
as.numeric("1")
## [1] 1
as.numeric("one")
## Warning: 强制改变过程中产生了NA
## [1] NA
对”1”进行转换的时候不会出错,但对“one”进行转换的时候会出错。后者出错的原因是强制类型转换的原则遵循高等级对象可转换为低等级,而低等级对象不能转换为高等级(转换时会丢失数据信息),“one”中是英文字符,转换时数据缺失,故不能转换成数字类型。
运行以下命令,并解释出现的结果。,如果出错,请修正。
substr('JAN07', 4, 5) + 2000
报错为二元运算符出现非数值参数,即substr执行完是一个字符型数据,不能和 2000 这个数值型数据进行相加。 修正:
as.double(substr('JAN07', 4, 5)) + 2000
## [1] 2007
有字符串x定义如下:
x <- '1, 3; 5'
请把x按照“,”或者“;”拆分,并将结果转化成数值型变量。
x <- '1, 3; 5'
x <- strsplit(x,split=",|;")[[1]]
print(x)
## [1] "1" " 3" " 5"
as.double(x)
## [1] 1 3 5
定义出生日期变量date_bir和发病日期变量date_dis如下:
date_bir <- "1941/3/8"
date_dis <- "2007年1月1日"
Date 和POSIXct日期型。date_bir <- "1941/3/8"
date_dis <- "2007年1月1日"
as.Date(date_bir)
## [1] "1941-03-08"
as.Date(date_dis,format="%Y年%m月%d日")
## [1] "2007-01-01"
as.POSIXct(date_bir)
## [1] "1941-03-08 JST"
as.POSIXct(date_dis,format="%Y年%m月%d日")
## [1] "2007-01-01 CST"
bir <- as.POSIXlt(date_bir)
cat("出生年是:",1900+bir$year,"\n")
## 出生年是: 1941
cat("出生月份是:",bir$mon+1)
## 出生月份是: 3
sink()函数将发病年龄保存在test.txt文件中。sink("test.txt",split=TRUE);date_bir <- "1941/3/8";date_dis <- "2007年1月1日";bir <- as.POSIXlt(date_bir);dis <- as.POSIXlt(date_dis,format="%Y年%m月%d日");if((dis$mon+1)>(bir$mon+1)){age <- dis$year-bir$year;cat("发病年龄是",age,sep="")};if((dis$mon+1)==(bir$mon+1)&(dis$mday)>=(bir$mday)){age <- dis$year-bir$year;cat("发病年龄是",age,sep="")};if(((dis$mon+1)==(bir$mon+1)&(dis$mday)<(bir$mday))|((dis$mon+1)<(bir$mon+1))){age <-dis$year-bir$year-1;cat("发病年龄是",age,sep="")};sink()
## 发病年龄是65
date_dis中发病年月转换成monyy格式,这里mon是指如SEP的英文三字母缩写, yy是两数字的年份。date_dis <- "2007年1月1日"
dis_date <- as.Date(date_dis,format="%Y年%m月%d日")
print(dis_date)
## [1] "2007-01-01"
Sys.setlocale('LC_TIME','C')
## [1] "C"
dis_date <- toupper(format(dis_date,"%b%y"))
print(dis_date)
## [1] "JAN07"
x <- factor(c("男", "女", "男"))
x有几个水平值,分别是什么?x <- factor(c("男", "女", "男"))
levels(x)
## [1] "男" "女"
变量’x’有两个水平值,分别是“男”和“女”。
以下命令实现了什么功能:
x <- as.ordered(x)
将变量’x’的两个水平值通过存储时的对应整数(即“男”对应1,“女”对应2)进行排序。
通过levels()函数把x中的“男”改成“Male”,“女”改成“Female”,并将x通过cat()函数输出到factor.txt文档中。
x <- factor(c("男", "女", "男"))
levels(x) <- c("Male","Female")
#print(x) 将x打出验证修改是否成功
cat(x,file="factor.txt")