首页 > 代码库 > R语言——任务2
R语言——任务2
---恢复内容开始---
题目要求:
数据处理第二阶段任务(必须全部用R代码实现)
1.数据清洗
2.获取每天的血糖日均值
3.根据数据文件,选择其中的任意三天,获取在哪些时段三天都会出现高血糖的状况(按照血糖值超出9来算)
R代码:
1 getwd(); 2 setwd("F:/R/myfile");#更改工作区间,将data.txt放入该文件夹下。 3 mydata <- read.table("data.txt", header = F, skip = 3, col.names = c("ID", "Date", "Time", "class", "GlucoseHistory", "Remarks"));#将data.txt的数据读入,略去前三行,分别为每列数据添加名称。 4 mydata;#查看数据读入情况。 5 mean(mydata$GlucoseHistory[mydata$ID <= 4992]);#6/2 6 mean(mydata$GlucoseHistory[mydata$ID <= 5199 & mydata$ID >= 4993]);#6/3 7 mean(mydata$GlucoseHistory[mydata$ID <= 5359 & mydata$ID >= 5200]);#6/4 8 mean(mydata$GlucoseHistory[mydata$ID <= 5538 & mydata$ID >= 5360]);#6/5 9 mean(mydata$GlucoseHistory[mydata$ID <= 5704 & mydata$ID >= 5539]);#6/6 10 mean(mydata$GlucoseHistory[mydata$ID <= 5875 & mydata$ID >= 5705]);#6/7 11 mean(mydata$GlucoseHistory[mydata$ID <= 6054 & mydata$ID >= 5876]);#6/8 12 mean(mydata$GlucoseHistory[mydata$ID <= 6114 & mydata$ID >= 6055]);#6/9 13 mean(mydata$GlucoseHistory[mydata$ID <= 6221 & mydata$ID >= 6115]);#6/10 #获取2-10号每天的血糖日平均值。 14 mean(mydata$GlucoseHistory);#获取全部的血糖日平均值。 15 mydata1 <- subset(mydata, mydata$ID <= 5199 & mydata$ID >= 4993);#获取3号子集。 16 mydata2 <- subset(mydata, mydata$ID <= 5359 & mydata$ID >= 5200);#获取4号子集。 17 mydata3 <- subset(mydata, mydata$ID <= 5538 & mydata$ID >= 5360);#获取5号子集。 18 time1 <- mydata1$Time[mydata1$GlucoseHistory > 9];#获取3号符合条件的时间。 19 time2 <- mydata2$Time[mydata2$GlucoseHistory > 9];#获取4号符合条件的时间。 20 time3 <- mydata3$Time[mydata3$GlucoseHistory > 9];#获取5号符合条件的时间。 21 time1; 22 time2; 23 time3;
运行代码视图:
> getwd(); [1] "F:/R/myfile" > setwd("F:/R/myfile");#更改工作区间,将data.txt放入该文件夹下。 > mydata <- read.table("data.txt", header = F, skip = 3, col.names = c("ID", "Date", "Time", "class", "GlucoseHistory", "Remarks"));#将data.txt的数据读入,略去前三行,分别为每列数据添加名称。 > mydata;#查看数据读入情况。 ID Date Time class GlucoseHistory Remarks 1 4858 2017/6/2 9:16 0 15.2 <NA> 2 4859 2017/6/2 9:33 1 14.3 <NA> 3 4861 2017/6/2 9:30 0 14.3 <NA> 4 4862 2017/6/2 9:46 1 15.4 <NA> 5 4864 2017/6/2 9:46 0 15.6 <NA> 6 4865 2017/6/2 10:01 0 16.2 <NA> 7 4866 2017/6/2 10:20 1 15.7 <NA> 8 4868 2017/6/2 10:16 0 16.1 <NA> 9 4869 2017/6/2 10:31 0 15.7 <NA> 10 4870 2017/6/2 10:59 1 16.8 <NA> 11 4872 2017/6/2 10:46 0 16.3 <NA> 12 4873 2017/6/2 11:07 1 17.6 运动 13 4875 2017/6/2 11:01 0 17.2 <NA> 14 4876 2017/6/2 11:22 1 17.6 <NA> 15 4878 2017/6/2 11:29 1 17.5 <NA> 16 4880 2017/6/2 11:16 0 17.8 <NA> 17 4881 2017/6/2 11:37 4 3.0 <NA> 18 4881 2017/6/2 11:37 1 18.3 <NA> 19 4883 2017/6/2 11:32 0 17.9 <NA> 20 4885 2017/6/2 11:50 1 16.9 <NA> 21 4887 2017/6/2 11:59 1 16.2 <NA> 22 4889 2017/6/2 11:46 0 17.0 <NA> 23 4890 2017/6/2 12:06 1 15.7 <NA> 24 4892 2017/6/2 12:14 1 15.0 <NA> 25 4894 2017/6/2 12:01 0 15.9 <NA> 26 4895 2017/6/2 12:17 1 14.7 <NA> 27 4897 2017/6/2 12:20 1 14.3 <NA> 28 4899 2017/6/2 12:26 1 13.9 <NA> 29 4901 2017/6/2 12:17 0 14.5 <NA> 30 4902 2017/6/2 12:43 1 11.8 <NA> 31 4904 2017/6/2 12:31 0 13.1 <NA> 32 4905 2017/6/2 12:46 1 11.7 <NA> 33 4907 2017/6/2 12:47 0 11.1 <NA> 34 4908 2017/6/2 13:02 0 9.1 <NA> 35 4909 2017/6/2 13:17 0 8.4 <NA> 36 4910 2017/6/2 13:32 0 8.7 <NA> 37 4911 2017/6/2 13:47 0 8.9 <NA> 38 4912 2017/6/2 14:02 0 9.1 <NA> 39 4913 2017/6/2 14:23 1 8.4 <NA> 40 4915 2017/6/2 14:18 0 8.8 <NA> 41 4916 2017/6/2 14:33 0 8.9 <NA> 42 4917 2017/6/2 14:48 0 9.8 <NA> 43 4918 2017/6/2 15:03 1 10.7 <NA> 44 4920 2017/6/2 15:15 1 11.6 <NA> 45 4922 2017/6/2 15:03 0 11.0 <NA> 46 4923 2017/6/2 15:22 1 11.9 <NA> 47 4925 2017/6/2 15:28 1 12.1 <NA> 48 4927 2017/6/2 15:18 0 11.9 <NA> 49 4928 2017/6/2 15:33 0 12.2 <NA> 50 4929 2017/6/2 15:48 0 12.4 <NA> 51 4930 2017/6/2 16:04 1 12.5 <NA> 52 4932 2017/6/2 16:14 1 12.1 <NA> 53 4934 2017/6/2 16:14 1 12.1 <NA> 54 4936 2017/6/2 16:04 0 12.1 <NA> 55 4937 2017/6/2 16:19 0 11.8 <NA> 56 4938 2017/6/2 16:34 0 11.6 <NA> 57 4939 2017/6/2 16:55 1 11.6 <NA> 58 4941 2017/6/2 16:49 0 11.2 <NA> 59 4942 2017/6/2 17:13 1 12.0 <NA> 60 4944 2017/6/2 17:04 0 11.8 <NA> 61 4945 2017/6/2 17:19 0 12.9 <NA> 62 4946 2017/6/2 17:34 0 13.1 <NA> 63 4947 2017/6/2 18:00 1 11.4 <NA> 64 4949 2017/6/2 18:00 4 13.0 <NA> 65 4949 2017/6/2 18:00 4 9.0 <NA> 66 4949 2017/6/2 18:00 1 11.4 用药 67 4951 2017/6/2 17:49 0 12.1 <NA> 68 4953 2017/6/2 18:10 1 10.5 <NA> 69 4955 2017/6/2 18:18 1 9.9 <NA> 70 4957 2017/6/2 18:05 0 10.7 <NA> 71 4958 2017/6/2 18:34 1 7.8 <NA> 72 4960 2017/6/2 18:20 0 9.1 <NA> 73 4961 2017/6/2 18:35 0 7.3 <NA> 74 4962 2017/6/2 18:50 0 5.3 <NA> 75 4963 2017/6/2 19:05 0 4.4 <NA> 76 4964 2017/6/2 19:26 1 5.4 <NA> 77 4966 2017/6/2 19:20 0 5.2 <NA> 78 4967 2017/6/2 19:44 1 6.7 <NA> 79 4969 2017/6/2 19:35 0 6.4 <NA> 80 4970 2017/6/2 19:50 0 7.1 <NA> 81 4971 2017/6/2 20:05 0 7.6 <NA> 82 4972 2017/6/2 20:20 0 7.6 <NA> 83 4973 2017/6/2 20:35 0 7.1 <NA> 84 4974 2017/6/2 20:50 0 6.8 <NA> 85 4975 2017/6/2 21:10 1 6.5 <NA> 86 4977 2017/6/2 21:06 0 6.4 <NA> 87 4978 2017/6/2 21:21 0 5.6 <NA> 88 4979 2017/6/2 21:36 0 4.6 <NA> 89 4980 2017/6/2 21:57 1 4.1 <NA> 90 4982 2017/6/2 21:52 0 4.4 <NA> 91 4983 2017/6/2 22:07 0 5.3 <NA> 92 4984 2017/6/2 22:22 0 5.6 <NA> 93 4985 2017/6/2 22:37 0 5.9 <NA> 94 4986 2017/6/2 22:52 0 6.6 <NA> 95 4987 2017/6/2 23:07 0 6.1 <NA> 96 4988 2017/6/2 23:22 0 5.3 <NA> 97 4989 2017/6/2 23:40 1 4.1 <NA> 98 4991 2017/6/2 23:39 0 4.3 <NA> 99 4992 2017/6/2 23:54 0 3.6 <NA> 100 4993 2017/6/3 0:09 0 3.2 <NA> 101 4994 2017/6/3 0:24 0 3.1 <NA> 102 4995 2017/6/3 0:39 0 2.9 <NA> 103 4996 2017/6/3 0:54 0 2.6 <NA> 104 4997 2017/6/3 1:09 0 2.4 <NA> 105 4998 2017/6/3 1:24 0 2.2 <NA> 106 4999 2017/6/3 1:39 0 2.2 <NA> 107 5000 2017/6/3 1:54 0 2.2 <NA> 108 5001 2017/6/3 2:09 0 2.2 <NA> 109 5002 2017/6/3 2:24 0 2.2 <NA> 110 5003 2017/6/3 2:39 0 2.2 <NA> 111 5004 2017/6/3 2:54 0 2.2 <NA> 112 5005 2017/6/3 3:09 0 2.3 <NA> 113 5006 2017/6/3 3:24 0 2.5 <NA> 114 5007 2017/6/3 3:39 0 2.7 <NA> 115 5008 2017/6/3 3:54 0 2.9 <NA> 116 5009 2017/6/3 4:09 0 3.2 <NA> 117 5010 2017/6/3 4:26 1 3.2 <NA> 118 5012 2017/6/3 4:25 0 3.3 <NA> 119 5013 2017/6/3 4:40 0 3.7 <NA> 120 5014 2017/6/3 4:55 0 4.0 <NA> 121 5015 2017/6/3 5:10 0 4.4 <NA> 122 5016 2017/6/3 5:25 0 4.9 <NA> 123 5017 2017/6/3 5:40 0 5.4 <NA> 124 5018 2017/6/3 5:55 0 6.1 <NA> 125 5019 2017/6/3 6:10 0 7.1 <NA> 126 5020 2017/6/3 6:26 1 7.4 <NA> 127 5022 2017/6/3 6:25 0 7.2 <NA> 128 5023 2017/6/3 6:52 1 7.8 <NA> 129 5025 2017/6/3 6:40 0 7.7 <NA> 130 5026 2017/6/3 7:05 1 9.4 <NA> 131 5028 2017/6/3 6:55 0 8.8 <NA> 132 5029 2017/6/3 7:20 1 9.6 <NA> 133 5031 2017/6/3 7:10 0 9.8 <NA> 134 5032 2017/6/3 7:38 1 11.9 <NA> 135 5034 2017/6/3 7:25 0 10.7 <NA> 136 5035 2017/6/3 7:47 1 12.6 <NA> 137 5037 2017/6/3 7:41 0 12.3 <NA> 138 5038 2017/6/3 7:56 0 13.4 <NA> 139 5039 2017/6/3 8:19 1 12.6 <NA> 140 5041 2017/6/3 8:11 0 13.3 <NA> 141 5042 2017/6/3 8:26 0 12.2 <NA> 142 5043 2017/6/3 8:47 1 11.3 <NA> 143 5045 2017/6/3 8:41 0 11.5 <NA> 144 5046 2017/6/3 9:00 1 10.3 <NA> 145 5048 2017/6/3 9:07 1 9.6 <NA> 146 5050 2017/6/3 8:56 0 10.3 <NA> 147 5051 2017/6/3 9:15 1 8.7 <NA> 148 5053 2017/6/3 9:19 1 8.6 <NA> 149 5055 2017/6/3 9:21 1 8.5 <NA> 150 5057 2017/6/3 9:11 0 8.9 <NA> 151 5058 2017/6/3 9:30 1 7.8 <NA> 152 5060 2017/6/3 9:26 0 7.8 <NA> 153 5061 2017/6/3 9:42 1 7.6 <NA> 154 5063 2017/6/3 9:51 1 8.7 <NA> 155 5065 2017/6/3 9:41 0 8.2 <NA> 156 5066 2017/6/3 9:56 0 8.9 <NA> 157 5067 2017/6/3 10:11 1 9.2 <NA> 158 5069 2017/6/3 10:21 1 9.2 <NA> 159 5071 2017/6/3 10:25 1 9.3 <NA> 160 5073 2017/6/3 10:12 0 9.2 <NA> 161 5074 2017/6/3 10:32 1 9.7 <NA> 162 5076 2017/6/3 10:36 1 10.1 <NA> 163 5078 2017/6/3 10:38 1 10.2 <NA> 164 5080 2017/6/3 10:27 0 9.7 <NA> 165 5081 2017/6/3 10:42 0 10.2 <NA> 166 5082 2017/6/3 10:57 0 10.3 <NA> [ reached getOption("max.print") -- omitted 3811 rows ] > mean(mydata$GlucoseHistory[mydata$ID <= 4992]);#6/2 [1] 10.90101 > mean(mydata$GlucoseHistory[mydata$ID <= 5199 & mydata$ID >= 4993]);#6/3 [1] 7.88 > mean(mydata$GlucoseHistory[mydata$ID <= 5359 & mydata$ID >= 5200]);#6/4 [1] 11.97829 > mean(mydata$GlucoseHistory[mydata$ID <= 5538 & mydata$ID >= 5360]);#6/5 [1] 9.499281 > mean(mydata$GlucoseHistory[mydata$ID <= 5704 & mydata$ID >= 5539]);#6/6 [1] 11.81343 > mean(mydata$GlucoseHistory[mydata$ID <= 5875 & mydata$ID >= 5705]);#6/7 [1] 8.695588 > mean(mydata$GlucoseHistory[mydata$ID <= 6054 & mydata$ID >= 5876]);#6/8 [1] 10.91079 > mean(mydata$GlucoseHistory[mydata$ID <= 6114 & mydata$ID >= 6055]);#6/9 [1] 10.92353 > mean(mydata$GlucoseHistory[mydata$ID <= 6221 & mydata$ID >= 6115]);#6/10 #获取2-10号每天的血糖日平均值。 [1] 8.762195 > mean(mydata$GlucoseHistory);#获取全部的血糖日平均值。 [1] 9.791199 > mydata1 <- subset(mydata, mydata$ID <= 5199 & mydata$ID >= 4993);#获取3号子集。 > mydata2 <- subset(mydata, mydata$ID <= 5359 & mydata$ID >= 5200);#获取4号子集。 > mydata3 <- subset(mydata, mydata$ID <= 5538 & mydata$ID >= 5360);#获取5号子集。 > time1 <- mydata1$Time[mydata1$GlucoseHistory > 9];#获取3号符合条件的时间。 > time2 <- mydata2$Time[mydata2$GlucoseHistory > 9];#获取4号符合条件的时间。 > time3 <- mydata3$Time[mydata3$GlucoseHistory > 9];#获取5号符合条件的时间。 > time1; [1] 7:05 7:20 7:10 7:38 7:25 7:47 7:41 7:56 8:19 8:11 8:26 8:47 8:41 9:00 9:07 8:56 10:11 [18] 10:21 10:25 10:12 10:32 10:36 10:38 10:27 10:42 10:57 11:26 11:12 11:31 11:27 11:45 11:43 11:58 12:10 [35] 11:57 12:19 12:24 12:12 12:37 12:29 12:44 12:59 13:14 13:29 13:55 13:43 13:58 14:13 14:29 14:29 14:44 [52] 15:02 15:00 15:15 15:30 15:45 16:06 16:00 16:15 16:43 16:30 16:45 17:04 17:00 17:23 17:28 17:28 17:15 [69] 17:30 17:56 17:45 18:00 1389 Levels: 0:00 0:01 0:03 0:04 0:05 0:06 0:07 0:08 0:09 0:10 0:11 0:12 0:13 0:15 0:16 0:17 0:19 ... 9:59 > time2; [1] 4:35 4:50 5:05 5:31 5:21 5:36 5:51 6:06 6:28 6:21 6:36 6:51 7:06 7:28 7:22 7:37 7:52 [18] 8:07 8:34 8:21 8:47 8:37 8:52 9:08 9:07 9:24 9:22 9:41 9:37 9:58 9:52 10:11 12:00 11:53 [35] 12:17 12:09 12:30 12:23 12:44 12:40 12:55 13:10 13:25 13:40 13:55 14:10 14:25 14:40 14:55 15:22 15:11 [52] 15:26 15:41 15:56 16:11 16:26 16:44 16:40 17:05 16:55 17:10 17:28 17:28 17:26 17:45 17:41 17:59 17:55 [69] 18:14 18:11 18:34 18:26 18:41 19:02 18:57 19:12 19:27 19:42 19:57 20:12 20:27 20:42 20:57 21:12 21:13 [86] 21:35 21:28 21:50 21:43 21:58 22:13 22:35 22:29 22:44 22:59 23:14 23:29 23:44 1389 Levels: 0:00 0:01 0:03 0:04 0:05 0:06 0:07 0:08 0:09 0:10 0:11 0:12 0:13 0:15 0:16 0:17 0:19 ... 9:59 > time3; [1] 0:03 6:16 6:31 6:46 7:13 7:01 7:16 7:31 7:32 7:52 7:47 8:16 8:02 8:17 8:41 8:32 8:47 [18] 9:02 9:03 13:19 13:40 13:35 13:50 14:05 14:20 14:35 15:04 14:50 15:05 15:20 15:21 15:36 15:51 16:19 [35] 16:06 16:21 16:42 16:36 16:56 16:51 17:06 17:26 17:26 17:22 17:50 17:37 17:52 18:10 18:14 18:19 18:07 [52] 18:22 19:53 20:00 20:06 19:52 20:14 20:07 20:32 20:22 20:43 20:38 20:58 20:53 21:08 21:31 21:23 21:48 [69] 21:38 21:53 22:08 22:24 22:27 22:42 1389
结论:
1、总的血糖日平均值:9.791199、6/2血糖平均值:10.90101、6/3血糖平均值:7.88(其余天略)。
2、选取的6/3、6/4、6/5三日,血糖均超过9的时间段是7:00——9:00和13:00——18:00。
R语言——任务2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。