繁体   English   中英

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

[英]Find DAY with HIGHEST and LOWEST windspeeds using R, Python, or EXCEL

我有一个带有31个选项卡的Excel文件,它对应于5月中的某天。 每个选项卡或工作表都有3列(高度,速度,目录)。

我想找到风速最大的一天。 我尝试使用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

如果您将数据转换成这样的连续格式:

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在网格的左上方,并包含单词Day Max(Spd)是整个Spd列的最大值。 OffsetMatch是Excel函数。

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

如果可以将R的相同数据作为数据帧加载到R中,则可以执行类似以下subset(df,Spd==max(df[,"Spd"]))$Day其中df是数据的名称通过read.csvread.table或类似内容读入的框架。

可以重复以上两种操作以min代替max以找到最低速度。

如果您无法将其转换为该格式,或者无法使用Excel的INDIRECT ,则最好的解决方案是在Excel中使用简单的VBA遍历工作表。

在所有情况下,您可能都必须考虑如何处理关系,例如在2个或更多不同的天内以相同(最大)速度进行处理。

如果您可以忍受R为重复的列名创建唯一的列名,则无需为单个列名加上日号而烦恼(该帖子对此有点麻烦),然后可以删除“ Day”标题行,像上面一样保留月份的月份列,并将其制成CSV,R可以使用read.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 ...

要获得整个数据帧的最大速度值的列名称,我们首先需要在“ Spd”列上进行操作:

# 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 

但是我们真的只希望列具有整体最大值,因此我们将把applywhich.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))

我只是为了说明它的操作不像解释那样复杂。

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
>>> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM