#用R读入“R8 homework”数据,将该数据以矩阵形式赋值给变量“temp”;
#数据处理:将temp之中凡小于1的数值改为0,
# 将temp中等于1的数值改为0.1,
# 将temp中大于1的数值改为1;
#构造新矩阵:更改之后的矩阵按列对齐,
# 构造出由20个temp叠高出来的新矩阵---temp2,
# 并输出到硬盘上。
#
fileAddress="D:/Rdata/R8 homework.csv"#待处理数据地址
temp=read.csv(fileAddress,header = FALSE)#读入数据源忽略第一行名称
tempmat=tempif=tempifelse=tempswitch=temp
#if语句实现
for (i in 1:nrow(tempif)) {#行遍历
for (j in 1:ncol(tempif)) {#列遍历
if(tempif[i,j]<1) {tempif[i,j]=0}
if(tempif[i,j]==1){tempif[i,j]=0.1} #判断赋值
if(tempif[i,j]>1){tempif[i,j]=1}
}
}
#write.csv(tempif,"D:/Rdata/R8 homework tempif.csv")
temp2=tempif
for (i in 1:19) {
temp2=rbind(temp2,tempif)#列对齐循环叠加
}
write.csv(temp2,"D:/Rdata/temp2-if.csv")
#ifelse语句
for (i in 1:nrow(tempifelse)) {
for (j in 1:ncol(tempifelse)) {
a=tempifelse[i,j]-1#判断参数名化简
#ifelse(test,yes,no)
ifelse(a<=0,if(a==0){tempifelse[i,j]=0.1}else{tempifelse[i,j]=0},if(a>0)tempifelse[i,j]=1)
}
}
#write.csv(tempifelse,"D:/Rdata/R8 homework tempifelse.csv")
temp2=tempifelse
for (i in 1:19) {
temp2=rbind(temp2,tempifelse)
}
write.csv(temp2,"D:/Rdata/temp2-ifelse.csv")
#switch语句实现
for (i in 1:nrow(tempswitch)) {
for (j in 1:ncol(tempswitch)) {
if(tempswitch[i,j]<1)a=1
if(tempswitch[i,j]==1)a=2 #定义case
if(tempswitch[i,j]>1)a=3
switch(a,
{tempswitch[i,j]=0},
{tempswitch[i,j]=0.1}, #case判值
{tempswitch[i,j]=1},
)
}
}
#write.csv(tempswitch,"D:/Rdata/R8 homework tempswitch.csv")
temp2=tempswitch
for (i in 1:19) {
temp2=rbind(temp2,tempswitch)
}
write.csv(temp2,"D:/Rdata/temp2-switch.csv")
#矩阵逻辑实现
#temp=read.csv("D:/Rdata/R8 homework.csv",header = FALSE)
#矩阵元素自判逻辑赋值
tempmat[tempmat<1]=0#小于1的赋0
tempmat[tempmat==1]=0.1#等于1的赋0.1
tempmat[tempmat>1]=1#大于1的赋1
temp2=tempmat
for (i in 1:19) {
temp2=rbind(temp2,tempmat)
}
write.csv(temp2,"D:/Rdata/temp2-mat.csv")
R8 homework.csv下载链接
链接:https://pan.baidu.com/s/1rwo0hyl3MmL-9lCZHd6WdQ
提取码:bf5i