数据统计与分析基础实验一:基本语法与运算(R语言)

1、编写程序,记录十名学生的信息,至少包括姓名、年龄、出生年、数据统计分析课程实验成绩,程序输出前n个学生的年龄平均值、数据统计分析课程实验成绩平均值,n为函数的输入参数。

源代码
student<-setRefClass("student",fields = list(name="character",age="numeric",both_year="numeric",score="numeric"))
stu1<-student(name="A",age=19,both_year=2002,score=90)
stu2<-student(name="B",age=20,both_year=2001,score=80)
stu3<-student(name="C",age=20,both_year=2001,score=70)
stu4<-student(name="D",age=20,both_year=2001,score=60)
stu5<-student(name="E",age=19,both_year=2000,score=85)
stu6<-student(name="F",age=20,both_year=2001,score=87)
stu7<-student(name="G",age=21,both_year=2000,score=92)
stu8<-student(name="H",age=20,both_year=2001,score=99)
stu9<-student(name="I",age=21,both_year=2000,score=88)
stu10<-student(name="J",age=20,both_year=2001,score=95)

score<-c(stu1$score,
         stu2$score,
         stu3$score,
         stu4$score,
         stu5$score,
         stu6$score,
         stu7$score,
         stu8$score,
         stu9$score,
         stu10$score)
age<-c(stu1$age,
       stu2$age,
       stu3$age,
       stu4$age,
       stu5$age,
       stu6$age,
       stu7$age,
       stu8$age,
       stu9$age,
       stu10$age)

mean_score<-function(n)
{
    new_score<-c(NA)
    new_age<-c(NA)
    for(count in 1:n){
        new_score[count]<-score[count]
        new_age[count]<-age[count]
    }
    print(paste0("平均年龄为:",mean(new_age)," ","平均成绩为:",mean(new_score)))
}
mean_score(2)
mean_score(3)
mean_score(4)
mean_score(10)

逐条执行结果

> mean_score(2)
[1] "平均年龄为:19.5 平均成绩为:85"
> mean_score(3)
[1] "平均年龄为:19.6666666666667 平均成绩为:80"
> mean_score(4)
[1] "平均年龄为:19.75 平均成绩为:75"
> mean_score(10)
[1] "平均年龄为:20 平均成绩为:84.6"
> 
> 

2、找出1至999之间是13的倍数或者前两位数字是13的数字,输出这些数字,并统计有多少个。

源代码
CountAndPrint<-function()
{
    number<-c(NA)
    t<-0;
    for(i in 1:999){
        if(i%%13==0 || (i>=130 && i<=139)){
            number[t]=i;
            t<-t+1
        }
    }
    print(paste0("一共有" ,t,"个数字,分别为: "))
    print(number)
}

CountAndPrint()
逐条执行结果
> CountAndPrint()
[1] "一共有85个数字,分别为: "
 [1]  13  26  39  52  65  78  91 104 117 130 131 132 133 134 135 136 137 138 139 143 156
[22] 169 182 195 208 221 234 247 260 273 286 299 312 325 338 351 364 377 390 403 416 429
[43] 442 455 468 481 494 507 520 533 546 559 572 585 598 611 624 637 650 663 676 689 702
[64] 715 728 741 754 767 780 793 806 819 832 845 858 871 884 897 910 923 936 949 962 975
[85] 988
> 

3、编写成绩转化为绩点的函数,用98,93,89,73,66分别调用函数,生成对应绩点。

源代码
compute_score<-function(n)
{
    if(n>=90){
        print(4.0)
    }
    else{
        if(n>=80){
            print(3+0.1*(n-80))
        }
        else{
            if(n>=70){
                print(2+0.1*(n-70))
            }
            else{
                if(n>=60){
                    print(1+0.1*(n-60))
                }
                else{
                    print(0.0)
                }
            }
        }
    }
}

compute_score(98)
compute_score(93)
compute_score(89)
compute_score(73)
compute_score(66)
逐条执行结果
> compute_score(98)
[1] 4
> compute_score(93)
[1] 4
> compute_score(89)
[1] 3.9
> compute_score(73)
[1] 2.3
> compute_score(66)
[1] 1.6

4、随机生成两个长为100且服从标准正态分布的向量,然后将两向量所有偶数位的数值对调,输出所有的4个向量(对调前的2个,对调后的2个)。

源代码
vector_use<-function(){
    x1<-rnorm(100)
    x2<-rnorm(100)
    x3<-x1
    x4<-x2
    for(i in 1:100){
        if(i%%2==0){
            temp<-x3[i]
            x3[i]<-x4[i]
            x4[i]<-temp
        }
    }
    print("初始的两个向量:")
    print(x1)
    print(x2)
    print("调动偶数位后,新的两个向量:")
    print(x3)
    print(x4)
}

vector_use()
逐条执行结果
> vector_use()
[1] "初始的两个向量:"
  [1]  0.319612070  0.015819871  0.869415469 -1.176999121  0.071135138  0.002999897
  [7] -0.169636344  1.591012725  0.827372292  2.230233882 -1.516781376  1.478354192
 [13]  0.462863889 -0.156409261 -1.355582378  0.674201139  0.748893040 -0.713604424
 [19]  0.313050351 -0.148590846  1.113293579  1.222341899 -1.331542804  1.435167502
 [25]  0.697359933 -1.884273223  0.229524225 -0.055679346 -0.270288788 -0.522594674
 [31]  0.222434078 -1.285773357  0.363372337 -1.052354558  0.714893112  0.537422268
 [37] -0.004273680  0.221294067 -0.136203069  0.786233205  0.309553284 -0.940615158
 [43] -1.527855939 -0.197083840  0.143029852  2.129222688  0.352623891 -1.982332276
 [49]  1.008691808  0.185446018 -0.488159115 -0.383184039  0.209679524 -0.578755388
 [55] -0.973175502 -0.369543045 -0.067756344  0.274504893 -0.909282076  0.415769537
 [61]  0.775164200  0.287887517  0.099896448  0.968901223  0.741993852 -1.208472532
 [67] -0.063377501 -1.181997483 -0.147739997  1.502801666 -1.996553865 -1.030902652
 [73]  2.636829548  0.818082881  2.790901741 -0.577657151  0.696117625  1.077593938
 [79] -0.123296065  0.804158746  1.856532409 -1.037099388  0.605047538 -0.228457302
 [85]  0.681835246  0.266290149  1.198032886 -0.019675263 -0.159019727 -1.904559740
 [91] -0.976426085 -0.762175416 -1.336626827  2.395567742  0.687167431 -1.191623572
 [97]  0.580462629 -1.382811521 -0.699478382  0.064953614
  [1] -1.81870906  1.04257871 -0.10650283 -1.75819176 -0.25885374 -0.96899020  0.40225702
  [8] -2.98534997 -0.80356041  0.72178293 -0.51600527 -1.42371356 -1.19620726  1.12711645
 [15] -0.07717391  1.91559960 -0.27692214  0.11324847  0.85973458  0.42046223  0.21794640
 [22]  0.12911543 -3.04384425  1.90241398 -0.69391919  1.50585291  1.15443741  0.99382990
 [29] -0.34668334  0.26803733 -0.80982230  0.72315893 -0.37180165  0.80548626  1.81133548
 [36]  0.62886876 -0.74793609 -1.04395881 -0.76611761  0.05756699  0.01268761 -0.35335449
 [43]  1.17948907  1.20367693  0.03601567  0.88748965  1.45290872 -1.53258399 -0.98711446
 [50]  2.22898991 -1.46603237 -1.25702472  0.63963586  0.30231780 -0.39316340 -0.04355806
 [57] -0.19199672  0.35118921  0.31553287  0.68773473  0.69390075 -0.54237982  1.68424915
 [64] -0.25431864  0.26953820  0.70440099 -0.15022349  1.39380768  0.60695650 -1.18744209
 [71]  0.34166978 -1.78797519  1.67279154  0.96406088 -0.69872699  1.09033617 -0.58507390
 [78]  1.38314858 -0.10554886  1.46861966  1.13452257 -0.98136379  1.47061413  0.16657006
 [85] -0.27680390  0.06171850  0.32741027 -0.03595515  1.30605720  0.58795543  2.12798676
 [92] -1.92604821 -0.71841657  0.69482668 -2.35087369  0.38025722  1.20806266  0.52068086
 [99]  0.74830498 -0.93846847
[1] "调动偶数位后,新的两个向量:"
  [1]  0.31961207  1.04257871  0.86941547 -1.75819176  0.07113514 -0.96899020 -0.16963634
  [8] -2.98534997  0.82737229  0.72178293 -1.51678138 -1.42371356  0.46286389  1.12711645
 [15] -1.35558238  1.91559960  0.74889304  0.11324847  0.31305035  0.42046223  1.11329358
 [22]  0.12911543 -1.33154280  1.90241398  0.69735993  1.50585291  0.22952422  0.99382990
 [29] -0.27028879  0.26803733  0.22243408  0.72315893  0.36337234  0.80548626  0.71489311
 [36]  0.62886876 -0.00427368 -1.04395881 -0.13620307  0.05756699  0.30955328 -0.35335449
 [43] -1.52785594  1.20367693  0.14302985  0.88748965  0.35262389 -1.53258399  1.00869181
 [50]  2.22898991 -0.48815912 -1.25702472  0.20967952  0.30231780 -0.97317550 -0.04355806
 [57] -0.06775634  0.35118921 -0.90928208  0.68773473  0.77516420 -0.54237982  0.09989645
 [64] -0.25431864  0.74199385  0.70440099 -0.06337750  1.39380768 -0.14774000 -1.18744209
 [71] -1.99655386 -1.78797519  2.63682955  0.96406088  2.79090174  1.09033617  0.69611763
 [78]  1.38314858 -0.12329606  1.46861966  1.85653241 -0.98136379  0.60504754  0.16657006
 [85]  0.68183525  0.06171850  1.19803289 -0.03595515 -0.15901973  0.58795543 -0.97642608
 [92] -1.92604821 -1.33662683  0.69482668  0.68716743  0.38025722  0.58046263  0.52068086
 [99] -0.69947838 -0.93846847
  [1] -1.818709057  0.015819871 -0.106502832 -1.176999121 -0.258853740  0.002999897
  [7]  0.402257020  1.591012725 -0.803560415  2.230233882 -0.516005267  1.478354192
 [13] -1.196207256 -0.156409261 -0.077173906  0.674201139 -0.276922141 -0.713604424
 [19]  0.859734585 -0.148590846  0.217946400  1.222341899 -3.043844254  1.435167502
 [25] -0.693919190 -1.884273223  1.154437407 -0.055679346 -0.346683339 -0.522594674
 [31] -0.809822301 -1.285773357 -0.371801649 -1.052354558  1.811335482  0.537422268
 [37] -0.747936088  0.221294067 -0.766117610  0.786233205  0.012687609 -0.940615158
 [43]  1.179489069 -0.197083840  0.036015673  2.129222688  1.452908720 -1.982332276
 [49] -0.987114458  0.185446018 -1.466032370 -0.383184039  0.639635856 -0.578755388
 [55] -0.393163399 -0.369543045 -0.191996718  0.274504893  0.315532869  0.415769537
 [61]  0.693900751  0.287887517  1.684249146  0.968901223  0.269538202 -1.208472532
 [67] -0.150223486 -1.181997483  0.606956503  1.502801666  0.341669785 -1.030902652
 [73]  1.672791541  0.818082881 -0.698726985 -0.577657151 -0.585073896  1.077593938
 [79] -0.105548862  0.804158746  1.134522570 -1.037099388  1.470614133 -0.228457302
 [85] -0.276803900  0.266290149  0.327410269 -0.019675263  1.306057200 -1.904559740
 [91]  2.127986760 -0.762175416 -0.718416572  2.395567742 -2.350873691 -1.191623572
 [97]  1.208062657 -1.382811521  0.748304982  0.064953614
> 

5、已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。

源代码
n<-list(1,6)

for(x in 1:4){
    for(y in 1:4){
        for(z in n){
            if(x*100+y*100+z*10+y*10+2*z==532){
                print(paste(x," ",y," ",z))
            }
        }
    }
}

逐条执行结果
> n<-list(1,6)
> for(x in 1:4){
+     for(y in 1:4){
+         for(z in n){
+             if(x*100+y*100+z*10+y*10+2*z==532){
+                 print(paste(x," ",y," ",z))
+             }
+         }
+     }
+ }
[1] "3   2   1"
> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值