使用R,Python或EXCEL查找具有最高和最低风速的DAY

时间:2014-08-26 02:09:23

标签: python r excel

我有一个包含31个标签的excel文件,对应于5月份的某一天。每个选项卡或工作表都有3列(Height,Spd,Dir)。

我想找到最大风速的那一天。我尝试使用excel的函数MAX=MAX(wind1:wind31!C1:C17)来查找它,但它只给出了最大值。有没有办法确定整个月风速最高的那一天而不仅仅是一个最大值,看高度起作用。我是否必须做一些统计杂耍(原谅该术语)?

我有R软件和Python,但我主要是新手。

这些是31张纸中的3张的数据。

        Day 1               Day 2               Day 3    and so on
Height  Dir Spd     Height  Dir Spd     Height  Dir Spd
139     333 6.5     110     254 3.6     157     341 6.9
790     343 5.9     767     264 4.3     814     357 6.2
1492    343 5.7     1471    274 6.6     1522    0   5.6
3079    297 9.4     3061    284 14.9    3127    317 10.3
4311    293 19      4291    289 21.9    4375    309 14.9
5731    291 28.6    5706    292 30.4    5809    306 19.1
7406    288 38.7    7381    294 42.8    7498    299 22.4
9462    286 47.6    9440    294 56      9550    290 22.5
10694   285 47.9    10679   293 61      10777   288 22.4
12129   281 46.9    12130   296 60.6    12207   292 23.8
13940   279 33.8    13936   296 40.4    13994   282 25.4
16473   279 13.8    16464   282 13.7    16517   286 11.7
18673   278 3       18665   324 2.9     18716   323 2.6
20786   63  2.3     20775   61  2.9     20824   59  4.1
24036   100 6       24015   104 4.4     24072   96  6.9
26676   85  5.5     26656   73  4       26719   83  7.9
31287   103 6.9     31253   102 7.9     31335   101 10.2

3 个答案:

答案 0 :(得分:3)

如果您将数据转换为如下所示的连续格式:

Day Height  Dir Spd
1   139    333  6.5
1   790    343  5.9
1   1492   343  5.7
.   .      .    .
.   .      .    .
.   .      .    .
2   110    254  3.6
2   767    264  4.3
.   .      .    .
.   .      .    .
31  26719   83  7.9
31  31335  101  10.2

您可以在Excel OFFSET(A1,MATCH(MAX(Spd),Spd,0),0)中使用此公式,其中单元格A1位于网格的左上角,并包含单词DayMax(Spd)是整个Spd列的最大值。 OffsetMatch是Excel函数。

另一种解决方案是在每张工作表中为Spd数据的范围命名,例如Spd_1Spd_2,等等。然后可以在单个工作表中表示为字符串的命名范围上使用Excel函数MAX(INDIRECT("Spd_1"))MAX(INDIRECT("Spd_2"))等。然后,您可以使用单个max函数查找相应的日期。

如果您可以在R中作为数据框加载相同的数据,那么您可以执行类似的操作 subset(df,Spd==max(df[,"Spd"]))$Day其中df是您通过read.csvread.table或类似内容读取的数据框的名称。

以上两个都可以用min代替max重复以找到最低速度。

如果您无法使用该格式,或者无法使用Excel的INDIRECT,那么最好的解决方案是在Excel中使用简单的VBA来循环显示这些格式。

在所有情况下,您可能需要考虑如何处理关系 - 例如在相同(最大)速度的2天或更多天内。

答案 1 :(得分:0)

如果您可以使用R为重复的列名称创建唯一的列名称,那么您将不需要将#c日记入各个列名称(对于此帖子来说有点多了)并且您然后可以删除" Day"标题行,将上面的一些读数列放在一起就像上面一样,并将其转换为R可以使用read.csv()读取的CSV。

这是从上面数据片段中读取的R数据帧结构:

dat <- structure(list(Height = c(139L, 790L, 1492L, 3079L, 4311L, 5731L, 
        7406L, 9462L, 10694L, 12129L, 13940L, 16473L, 18673L, 20786L, 
        24036L, 26676L, 31287L), Dir = c(333L, 343L, 343L, 297L, 293L, 
        291L, 288L, 286L, 285L, 281L, 279L, 279L, 278L, 63L, 100L, 85L, 
        103L), Spd = c(6.5, 5.9, 5.7, 9.4, 19, 28.6, 38.7, 47.6, 47.9, 
        46.9, 33.8, 13.8, 3, 2.3, 6, 5.5, 6.9), Height.1 = c(110L, 767L, 
        1471L, 3061L, 4291L, 5706L, 7381L, 9440L, 10679L, 12130L, 13936L, 
        16464L, 18665L, 20775L, 24015L, 26656L, 31253L), Dir.1 = c(254L, 
        264L, 274L, 284L, 289L, 292L, 294L, 294L, 293L, 296L, 296L, 282L, 
        324L, 61L, 104L, 73L, 102L), Spd.1 = c(3.6, 4.3, 6.6, 14.9, 21.9, 
        30.4, 42.8, 56, 61, 60.6, 40.4, 13.7, 2.9, 2.9, 4.4, 4, 7.9), 
            Height.2 = c(157L, 814L, 1522L, 3127L, 4375L, 5809L, 7498L, 
            9550L, 10777L, 12207L, 13994L, 16517L, 18716L, 20824L, 24072L, 
            26719L, 31335L), Dir.2 = c(341L, 357L, 0L, 317L, 309L, 306L, 
            299L, 290L, 288L, 292L, 282L, 286L, 323L, 59L, 96L, 83L, 
            101L), Spd.2 = c(6.9, 6.2, 5.6, 10.3, 14.9, 19.1, 22.4, 22.5, 
            22.4, 23.8, 25.4, 11.7, 2.6, 4.1, 6.9, 7.9, 10.2)), .Names = c("Height", 
        "Dir", "Spd", "Height.1", "Dir.1", "Spd.1", "Height.2", "Dir.2", 
        "Spd.2"), class = "data.frame", row.names = c(NA, -17L))

并且,这里描述的格式略好一些:

str(dat)

## 'data.frame':    17 obs. of  9 variables:
##  $ Height  : int  139 790 1492 3079 4311 5731 7406 9462 10694 12129 ...
##  $ Dir     : int  333 343 343 297 293 291 288 286 285 281 ...
##  $ Spd     : num  6.5 5.9 5.7 9.4 19 28.6 38.7 47.6 47.9 46.9 ...
##  $ Height.1: int  110 767 1471 3061 4291 5706 7381 9440 10679 12130 ...
##  $ Dir.1   : int  254 264 274 284 289 292 294 294 293 296 ...
##  $ Spd.1   : num  3.6 4.3 6.6 14.9 21.9 30.4 42.8 56 61 60.6 ...
##  $ Height.2: int  157 814 1522 3127 4375 5809 7498 9550 10777 12207 ...
##  $ Dir.2   : int  341 357 0 317 309 306 299 290 288 292 ...
##  $ Spd.2   : num  6.9 6.2 5.6 10.3 14.9 19.1 22.4 22.5 22.4 23.8 ...

要获取整个数据框的最大速度值的列名,我们首先需要先处理&#34; Spd&#34;专栏:

# only work with "Spd" columns

tmp <- dat[,which(grepl("Spd", names(dat)))]

# showing what we have left

str(tmp)

## 'data.frame':    17 obs. of  3 variables:
##  $ Spd  : num  6.5 5.9 5.7 9.4 19 28.6 38.7 47.6 47.9 46.9 ...
##  $ Spd.1: num  3.6 4.3 6.6 14.9 21.9 30.4 42.8 56 61 60.6 ...
##  $ Spd.2: num  6.9 6.2 5.6 10.3 14.9 19.1 22.4 22.5 22.4 23.8 ...

然后获取每列的最大值:

# get max value in each "Spd" column
apply(tmp, 2, max)

##   Spd Spd.1 Spd.2 
##  47.9  61.0  25.4 

但我们真的只想要具有总体最大值的列,因此我们将apply提供给which.max

# which one of those has the max value (returns name & position)
which.max(apply(tmp, 2, max))

## Spd.1 
##     2 

并保留列名称/#和最大值。

所有这些都可以在一条可怕的,不可读的线上完成:

which.max(apply(dat[, which(grepl("Spd", names(dat)))], 2, max))

我只是为了表明它并不像操作那么复杂,因为解释可能会让它看起来像是这样。

答案 2 :(得分:0)

Python和pandas模块是一种可能的解决方案:

#! /usr/bin/env python      
import pandas as pd

# Export the tabs as csv-files: day1.csv, day2.csv, ..., day31.csv.
# Assume the first line is a header line and that columns are
# separated by ',':
#
# Height ,  Dir , Spd
# 139    ,  333 , 6.5
# 790    ,  343 , 5.9
# ...
#

# Use or own column names and skip header.
column_names = ['height', 'direction',  'speed']

# Read in the data for each day.
alldays = []
for d in range(1, 32):
    fname = "day{}.csv".format(d)
    frame = pd.read_csv(fname, names=column_names, header=0)
    frame['day'] = d
    alldays.append(frame)

# Concatenate all days into DataFrame.
data = pd.concat(alldays, ignore_index=True)

# Get index for max and use it to retrieve the day and the speed.
idx_max = data.speed.idxmax()
max_row = data.ix[idx_max]
print("Maximum wind speed {} on day {}".format(max_row.speed, int(max_row.day)))

# Same as above but for the minimum.
idx_min = data.speed.idxmin()
min_row = data.ix[idx_min]
print("Minimum wind speed {} on day {}".format(min_row.speed, int(min_row.day)))

将其另存为脚本highlow.py。使用ipython和提供的示例数据,我得到以下结果:

>>> run highlow
Maximum wind speed 61.0 on day 2
Minimum wind speed 2.3 on day 1
>>> data.speed.describe()
count    51.000000
mean     18.209804
std      16.784853
min       2.300000
25%       5.800000
50%      10.300000
75%      24.600000
max      61.000000
dtype: float64
>>>