课后作业2:数据管理1
一:上机实验1
# 读入数据
data("CO2") |
# 查看数据集CO2中的变量名称,并将变量Treatment的名称更改为Treat
names(CO2) names(CO2)[names(CO2) == "Treatment"] <- "Treat" names(CO2) |
# 检验CO2中是否存在缺失值,若有,检测缺失值的位置并删除含有缺失值的行
has_na <- any(is.na(CO2)) has_na |
此数据集没有缺失值。如果该数据集有缺失值,则检测缺失值的位置操作如下。
na_positions <- which(is.na(CO2), arr.ind = TRUE) |
删除含有缺失值的行操作如下。
CO2 <- na.omit(CO2) |
# 对数据集CO2按照uptake排序(从大到小和从小到大)
(uptake_descend <- CO2[order(-CO2$uptake),]) (uptake_ascend <- CO2[order(CO2$uptake),]) |
完整结果:
降序(从大到小) |
Plant Type Treat conc uptake 21 Qn3 Quebec nonchilled 1000 45.5 14 Qn2 Quebec nonchilled 1000 44.3 20 Qn3 Quebec nonchilled 675 43.9 19 Qn3 Quebec nonchilled 500 42.9 35 Qc2 Quebec chilled 1000 42.4 18 Qn3 Quebec nonchilled 350 42.1 11 Qn2 Quebec nonchilled 350 41.8 13 Qn2 Quebec nonchilled 675 41.4 42 Qc3 Quebec chilled 1000 41.4 12 Qn2 Quebec nonchilled 500 40.6 17 Qn3 Quebec nonchilled 250 40.3 7 Qn1 Quebec nonchilled 1000 39.7 41 Qc3 Quebec chilled 675 39.6 6 Qn1 Quebec nonchilled 675 39.2 40 Qc3 Quebec chilled 500 38.9 32 Qc2 Quebec chilled 350 38.8 28 Qc1 Quebec chilled 1000 38.7 33 Qc2 Quebec chilled 500 38.6 38 Qc3 Quebec chilled 250 38.1 34 Qc2 Quebec chilled 675 37.5 4 Qn1 Quebec nonchilled 350 37.2 10 Qn2 Quebec nonchilled 250 37.1 49 Mn1 Mississippi nonchilled 1000 35.5 27 Qc1 Quebec chilled 675 35.4 5 Qn1 Quebec nonchilled 500 35.3 31 Qc2 Quebec chilled 250 35.0 3 Qn1 Quebec nonchilled 250 34.8 25 Qc1 Quebec chilled 350 34.6 39 Qc3 Quebec chilled 350 34.0 26 Qc1 Quebec chilled 500 32.5 16 Qn3 Quebec nonchilled 175 32.4 48 Mn1 Mississippi nonchilled 675 32.4 54 Mn2 Mississippi nonchilled 500 32.4 53 Mn2 Mississippi nonchilled 350 31.8 56 Mn2 Mississippi nonchilled 1000 31.5 55 Mn2 Mississippi nonchilled 675 31.1 47 Mn1 Mississippi nonchilled 500 30.9 52 Mn2 Mississippi nonchilled 250 30.6 2 Qn1 Quebec nonchilled 175 30.4 24 Qc1 Quebec chilled 250 30.3 46 Mn1 Mississippi nonchilled 350 30.0 61 Mn3 Mississippi nonchilled 500 28.5 62 Mn3 Mississippi nonchilled 675 28.1 60 Mn3 Mississippi nonchilled 350 27.9 63 Mn3 Mississippi nonchilled 1000 27.8 9 Qn2 Quebec nonchilled 175 27.3 30 Qc2 Quebec chilled 175 27.3 45 Mn1 Mississippi nonchilled 250 26.2 59 Mn3 Mississippi nonchilled 250 25.8 23 Qc1 Quebec chilled 175 24.1 69 Mc1 Mississippi chilled 675 22.2 51 Mn2 Mississippi nonchilled 175 22.0 70 Mc1 Mississippi chilled 1000 21.9 37 Qc3 Quebec chilled 175 21.0 84 Mc3 Mississippi chilled 1000 19.9 68 Mc1 Mississippi chilled 500 19.5 58 Mn3 Mississippi nonchilled 175 19.4 44 Mn1 Mississippi nonchilled 175 19.2 67 Mc1 Mississippi chilled 350 18.9 83 Mc3 Mississippi chilled 675 18.9 66 Mc1 Mississippi chilled 250 18.1 79 Mc3 Mississippi chilled 175 18.0 80 Mc3 Mississippi chilled 250 17.9 81 Mc3 Mississippi chilled 350 17.9 82 Mc3 Mississippi chilled 500 17.9 15 Qn3 Quebec nonchilled 95 16.2 1 Qn1 Quebec nonchilled 95 16.0 36 Qc3 Quebec chilled 95 15.1 65 Mc1 Mississippi chilled 175 14.9 77 Mc2 Mississippi chilled 1000 14.4 22 Qc1 Quebec chilled 95 14.2 76 Mc2 Mississippi chilled 675 13.7 8 Qn2 Quebec nonchilled 95 13.6 74 Mc2 Mississippi chilled 350 13.0 75 Mc2 Mississippi chilled 500 12.5 73 Mc2 Mississippi chilled 250 12.3 50 Mn2 Mississippi nonchilled 95 12.0 72 Mc2 Mississippi chilled 175 11.4 57 Mn3 Mississippi nonchilled 95 11.3 43 Mn1 Mississippi nonchilled 95 10.6 78 Mc3 Mississippi chilled 95 10.6 64 Mc1 Mississippi chilled 95 10.5 29 Qc2 Quebec chilled 95 9.3 71 Mc2 Mississippi chilled 95 7.7 |
升序(从小到大) |
Plant Type Treat conc uptake 71 Mc2 Mississippi chilled 95 7.7 29 Qc2 Quebec chilled 95 9.3 64 Mc1 Mississippi chilled 95 10.5 43 Mn1 Mississippi nonchilled 95 10.6 78 Mc3 Mississippi chilled 95 10.6 57 Mn3 Mississippi nonchilled 95 11.3 72 Mc2 Mississippi chilled 175 11.4 50 Mn2 Mississippi nonchilled 95 12.0 73 Mc2 Mississippi chilled 250 12.3 75 Mc2 Mississippi chilled 500 12.5 74 Mc2 Mississippi chilled 350 13.0 8 Qn2 Quebec nonchilled 95 13.6 76 Mc2 Mississippi chilled 675 13.7 22 Qc1 Quebec chilled 95 14.2 77 Mc2 Mississippi chilled 1000 14.4 65 Mc1 Mississippi chilled 175 14.9 36 Qc3 Quebec chilled 95 15.1 1 Qn1 Quebec nonchilled 95 16.0 15 Qn3 Quebec nonchilled 95 16.2 80 Mc3 Mississippi chilled 250 17.9 81 Mc3 Mississippi chilled 350 17.9 82 Mc3 Mississippi chilled 500 17.9 79 Mc3 Mississippi chilled 175 18.0 66 Mc1 Mississippi chilled 250 18.1 67 Mc1 Mississippi chilled 350 18.9 83 Mc3 Mississippi chilled 675 18.9 44 Mn1 Mississippi nonchilled 175 19.2 58 Mn3 Mississippi nonchilled 175 19.4 68 Mc1 Mississippi chilled 500 19.5 84 Mc3 Mississippi chilled 1000 19.9 37 Qc3 Quebec chilled 175 21.0 70 Mc1 Mississippi chilled 1000 21.9 51 Mn2 Mississippi nonchilled 175 22.0 69 Mc1 Mississippi chilled 675 22.2 23 Qc1 Quebec chilled 175 24.1 59 Mn3 Mississippi nonchilled 250 25.8 45 Mn1 Mississippi nonchilled 250 26.2 9 Qn2 Quebec nonchilled 175 27.3 30 Qc2 Quebec chilled 175 27.3 63 Mn3 Mississippi nonchilled 1000 27.8 60 Mn3 Mississippi nonchilled 350 27.9 62 Mn3 Mississippi nonchilled 675 28.1 61 Mn3 Mississippi nonchilled 500 28.5 46 Mn1 Mississippi nonchilled 350 30.0 24 Qc1 Quebec chilled 250 30.3 2 Qn1 Quebec nonchilled 175 30.4 52 Mn2 Mississippi nonchilled 250 30.6 47 Mn1 Mississippi nonchilled 500 30.9 55 Mn2 Mississippi nonchilled 675 31.1 56 Mn2 Mississippi nonchilled 1000 31.5 53 Mn2 Mississippi nonchilled 350 31.8 16 Qn3 Quebec nonchilled 175 32.4 48 Mn1 Mississippi nonchilled 675 32.4 54 Mn2 Mississippi nonchilled 500 32.4 26 Qc1 Quebec chilled 500 32.5 39 Qc3 Quebec chilled 350 34.0 25 Qc1 Quebec chilled 350 34.6 3 Qn1 Quebec nonchilled 250 34.8 31 Qc2 Quebec chilled 250 35.0 5 Qn1 Quebec nonchilled 500 35.3 27 Qc1 Quebec chilled 675 35.4 49 Mn1 Mississippi nonchilled 1000 35.5 10 Qn2 Quebec nonchilled 250 37.1 4 Qn1 Quebec nonchilled 350 37.2 34 Qc2 Quebec chilled 675 37.5 38 Qc3 Quebec chilled 250 38.1 33 Qc2 Quebec chilled 500 38.6 28 Qc1 Quebec chilled 1000 38.7 32 Qc2 Quebec chilled 350 38.8 40 Qc3 Quebec chilled 500 38.9 6 Qn1 Quebec nonchilled 675 39.2 41 Qc3 Quebec chilled 675 39.6 7 Qn1 Quebec nonchilled 1000 39.7 17 Qn3 Quebec nonchilled 250 40.3 12 Qn2 Quebec nonchilled 500 40.6 13 Qn2 Quebec nonchilled 675 41.4 42 Qc3 Quebec chilled 1000 41.4 11 Qn2 Quebec nonchilled 350 41.8 18 Qn3 Quebec nonchilled 350 42.1 35 Qc2 Quebec chilled 1000 42.4 19 Qn3 Quebec nonchilled 500 42.9 20 Qn3 Quebec nonchilled 675 43.9 14 Qn2 Quebec nonchilled 1000 44.3 21 Qn3 Quebec nonchilled 1000 45.5 |
# 对变量uptake进行从大到小和从小到大的排序
(uptake_sorted_descend <- sort(CO2$uptake, decreasing=T)) (uptake_sorted_ascend <- sort(CO2$uptake)) |
# 设置随机种子
set.seed(123) |
#将CO2随机分成两组数据,第一组和第二组比例为6:4
library(sampling) groups <- sample(2,size,replace=T,prob=c(0.6,0.4)) (sample1 <- CO2[groups==1,]) (sample2 <- CO2[groups==2,]) |
完整运行结果:
第一组sample1 |
Plant Type Treat conc uptake 1 Qn1 Quebec nonchilled 95 16.0 3 Qn1 Quebec nonchilled 250 34.8 6 Qn1 Quebec nonchilled 675 39.2 7 Qn1 Quebec nonchilled 1000 39.7 9 Qn2 Quebec nonchilled 175 27.3 10 Qn2 Quebec nonchilled 250 37.1 12 Qn2 Quebec nonchilled 500 40.6 14 Qn2 Quebec nonchilled 1000 44.3 15 Qn3 Quebec nonchilled 95 16.2 17 Qn3 Quebec nonchilled 250 40.3 18 Qn3 Quebec nonchilled 350 42.1 19 Qn3 Quebec nonchilled 500 42.9 27 Qc1 Quebec chilled 675 35.4 28 Qc1 Quebec chilled 1000 38.7 29 Qc2 Quebec chilled 95 9.3 30 Qc2 Quebec chilled 175 27.3 35 Qc2 Quebec chilled 1000 42.4 36 Qc3 Quebec chilled 95 15.1 38 Qc3 Quebec chilled 250 38.1 39 Qc3 Quebec chilled 350 34.0 40 Qc3 Quebec chilled 500 38.9 41 Qc3 Quebec chilled 675 39.6 42 Qc3 Quebec chilled 1000 41.4 43 Mn1 Mississippi nonchilled 95 10.6 44 Mn1 Mississippi nonchilled 175 19.2 45 Mn1 Mississippi nonchilled 250 26.2 46 Mn1 Mississippi nonchilled 350 30.0 47 Mn1 Mississippi nonchilled 500 30.9 48 Mn1 Mississippi nonchilled 675 32.4 49 Mn1 Mississippi nonchilled 1000 35.5 51 Mn2 Mississippi nonchilled 175 22.0 52 Mn2 Mississippi nonchilled 250 30.6 54 Mn2 Mississippi nonchilled 500 32.4 55 Mn2 Mississippi nonchilled 675 31.1 56 Mn2 Mississippi nonchilled 1000 31.5 57 Mn3 Mississippi nonchilled 95 11.3 60 Mn3 Mississippi nonchilled 350 27.9 62 Mn3 Mississippi nonchilled 675 28.1 63 Mn3 Mississippi nonchilled 1000 27.8 64 Mc1 Mississippi chilled 95 10.5 66 Mc1 Mississippi chilled 250 18.1 70 Mc1 Mississippi chilled 1000 21.9 74 Mc2 Mississippi chilled 350 13.0 75 Mc2 Mississippi chilled 500 12.5 76 Mc2 Mississippi chilled 675 13.7 77 Mc2 Mississippi chilled 1000 14.4 79 Mc3 Mississippi chilled 175 18.0 80 Mc3 Mississippi chilled 250 17.9 81 Mc3 Mississippi chilled 350 17.9 83 Mc3 Mississippi chilled 675 18.9 |
第二组sample2 |
Plant Type Treat conc uptake 2 Qn1 Quebec nonchilled 175 30.4 4 Qn1 Quebec nonchilled 350 37.2 5 Qn1 Quebec nonchilled 500 35.3 8 Qn2 Quebec nonchilled 95 13.6 11 Qn2 Quebec nonchilled 350 41.8 13 Qn2 Quebec nonchilled 675 41.4 16 Qn3 Quebec nonchilled 175 32.4 20 Qn3 Quebec nonchilled 675 43.9 21 Qn3 Quebec nonchilled 1000 45.5 22 Qc1 Quebec chilled 95 14.2 23 Qc1 Quebec chilled 175 24.1 24 Qc1 Quebec chilled 250 30.3 25 Qc1 Quebec chilled 350 34.6 26 Qc1 Quebec chilled 500 32.5 31 Qc2 Quebec chilled 250 35.0 32 Qc2 Quebec chilled 350 38.8 33 Qc2 Quebec chilled 500 38.6 34 Qc2 Quebec chilled 675 37.5 37 Qc3 Quebec chilled 175 21.0 50 Mn2 Mississippi nonchilled 95 12.0 53 Mn2 Mississippi nonchilled 350 31.8 58 Mn3 Mississippi nonchilled 175 19.4 59 Mn3 Mississippi nonchilled 250 25.8 61 Mn3 Mississippi nonchilled 500 28.5 65 Mc1 Mississippi chilled 175 14.9 67 Mc1 Mississippi chilled 350 18.9 68 Mc1 Mississippi chilled 500 19.5 69 Mc1 Mississippi chilled 675 22.2 71 Mc2 Mississippi chilled 95 7.7 72 Mc2 Mississippi chilled 175 11.4 73 Mc2 Mississippi chilled 250 12.3 78 Mc3 Mississippi chilled 95 10.6 82 Mc3 Mississippi chilled 500 17.9 84 Mc3 Mississippi chilled 1000 19.9 |
# 应用tapply()函数,计算不同的植物(Plant)对应的uptake的平均值
tapply(CO2$uptake,CO2$Plant,mean) |
# 应用aggregate()函数,计算不同植物(Plant)、不同的类型(Type)对应的uptake的平均值
aggregate(uptake ~ Plant + Type, data = CO2, FUN = mean) |
# 应用lapply()函数,同时计算conc和uptake的均值
lapply(CO2[, c("conc", "uptake")], mean) |
# 使用grep()函数,查找出植物名称(Plant)中含有" Qn "的行的位置,并将这些行储存于变量Plant_Qn中
grep("Qn", CO2$Plant) -> Plant_Qn Plant_Qn |
# 使用gsub()函数,将CO2中植物名称(Plant)中的字符串" Qn "改为" QN "
CO2$Plant CO2$Plant <- gsub("Qn", "QN", CO2$Plant) CO2$Plant |