前一段时间还在学校的时候下载了一些气象方面的数据,数据为气象站点的年平均气温,当然咯,相比于美国,我们国家的气象站点分布得没有那么密集。也可能是公布的数据没有那么密集,我收集了近十年来的平均温度数据,数据为cvs格式,包含的信息有:1.地名;2.地点编号;3.温度(美式单位);4.温度(摄氏度)。由于每一年的数据都有差异(存在着某些站点缺失的问题)
所以在进行统计的时候存在一定的问题,通过一些努力,还是完成了对于某个城市的温度统计,下面就如何使用R语言完成部分统计和制图工作进行一些介绍,同时也作为一个记录。
好了由于一共获取了十年的数据,因此需要将数据进行读取,对于CVS格式的数据,R语言是可以直接读取的,这一点类似于Matlab,直接通过Read函数读取数据,整个数据读取的代码为:

1
2
3
4
5
6
7
8
9
10
temperature1 <-read.csv('/home/wuwei/Data/temperature/stations (0).csv')
temperature2 <-read.csv('/home/wuwei/Data/temperature/stations (1).csv')
temperature3 <-read.csv('/home/wuwei/Data/temperature/stations (2).csv')
temperature4 <-read.csv('/home/wuwei/Data/temperature/stations (3).csv')
temperature5 <-read.csv('/home/wuwei/Data/temperature/stations (4).csv')
temperature6 <-read.csv('/home/wuwei/Data/temperature/stations (5).csv')
temperature7 <-read.csv('/home/wuwei/Data/temperature/stations (6).csv')
temperature8 <-read.csv('/home/wuwei/Data/temperature/stations (7).csv')
temperature9 <-read.csv('/home/wuwei/Data/temperature/stations (8).csv')
temperature10 <-read.csv('/home/wuwei/Data/temperature/stations (9).csv')

好了上面一段代码就是读取数据的代码,当然,再数据量很大的情况下采用这样的方式读取数据是很不合理的,再数据文件比较多的情况下就应该循环获取所有文件然后读取了,关于这个问题我们以后再进行讨论,由于这里只有十年的数据,所以直接写代码也并不是一个多大的工作量,因此就这样读取了。
数据读取之后马上就面临着数据提取和筛选的问题,从上面的代码我们可以看出,读取数据后每个年份的数据都在一张表中,这样难以进行数据提取和筛选,如果能够把数据集中到一张表中将会好很多,按照这个思路写了如下的代码:

1
tunion<-rbind(temperature1,temperature2,temperature3,temperature4,temperature5,temperature6,temperature7,temperature8,temperature9,temperature10)

上面的代码很简单就是将所有表按行结合成一张表,并将其赋值给变量tunion,在此情况下表tunion就拥有了所有数据,我们只需要对tunion进行处理就可以了。
现在问题来了,tunion中包含所有数据,我们现在假设要提取某个城市所有数据,最简单的想法就是循环表tunion然后判断城市的名称是不是给定的名称,如果是则得到数据。这个想法看起来很简单,在实现过程中需要遍历所有行,代码实现较为复杂,于是我们换一种思路,首先我们找到所有名字为某一个城市名字的所有下标,然后按下标取出。这个思路看起来比上一个更为复杂,但是实现起来较为简单,因为R的which语句直接给出了一个这样的函数which函数,因此按照这个思路我们提取某一个城市的所有温度数据情况代码为:

1
2
3
4
5
6
7
8
9
10
idxs<-which(p=='BAISE, CH')
newdata <- matrix(1:length(idxs)*2,length(idxs),2)
count = 1;
for (icol in idxs) {
data1=tunion$IMPERIAL[icol]
data2=tunion$METRIC[icol]
newdata[count,1]=data1
newdata[count,2]=data2
count=count+1
}

分析以上代码,主要分为两个步骤,1:提取下标;2:获取数据。首先通过which语句得到所有名字为BAISE, CH的城市的下标,其也对应这tunion中的行的下标,得到下标之后通过循环获取所有数据,最后将数据绘制为折线图:

1
plot(1:length(idxs),newdata[,2],type="b")

R语言有很强的制图功能,关于折线图的参数大家直接看帮助文档就好了,在这里我就不详细说明了,全部的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
temperature1 <-read.csv('/home/wuwei/Data/temperature/stations (0).csv')
temperature2 <-read.csv('/home/wuwei/Data/temperature/stations (1).csv')
temperature3 <-read.csv('/home/wuwei/Data/temperature/stations (2).csv')
temperature4 <-read.csv('/home/wuwei/Data/temperature/stations (3).csv')
temperature5 <-read.csv('/home/wuwei/Data/temperature/stations (4).csv')
temperature6 <-read.csv('/home/wuwei/Data/temperature/stations (5).csv')
temperature7 <-read.csv('/home/wuwei/Data/temperature/stations (6).csv')
temperature8 <-read.csv('/home/wuwei/Data/temperature/stations (7).csv')
temperature9 <-read.csv('/home/wuwei/Data/temperature/stations (8).csv')
temperature10 <-read.csv('/home/wuwei/Data/temperature/stations (9).csv')

tunion<-rbind(temperature1,temperature2,temperature3,temperature4,temperature5,temperature6,temperature7,temperature8,temperature9,temperature10)

idxs<-which(p=='BAISE, CH')
newdata <- matrix(1:length(idxs)*2,length(idxs),2)
count = 1;
for (icol in idxs) {
data1=tunion$IMPERIAL[icol]
data2=tunion$METRIC[icol]
newdata[count,1]=data1
newdata[count,2]=data2
count=count+1
}
plot(1:length(idxs),newdata[,2],type="b")

从上面我们可以看到,区区二十几行代码就完成了所有获取数据,数据筛选到出图的过程,不得不说R还是很强大的。下面展示一下制图结果:
十年温度折线图
上两幅分别为广州和昆明在十年间的温度折线图,从图中可以看到昆明再十年间的温度明显低于广州,另外由于昆明数据不足,因此只有八个年份的数据,而广州在第6个年份的平均温度明显较低,由于数据缺失年份信息所以也不知道具体是哪一年,可能是08年南方冰灾?具体的温度变化情况并不是我们分析的重点,所以在这里就不多做分析,以后有时间获取更多数据后进行详细分析。