使用R,Python或EXCEL以最高和最低风速查找天

我有一个excel文件,有31个选项卡,对应于5月份的一天。 每个标签或表格有3列(高度,速度,方向)。

我想find风速最大的一天。 我试着用excel的函数MAX=MAX(wind1:wind31!C1:C17)find它,但它只给出了一个最大值。 有没有一种方法可以确定整个月中风速最高的一天,而不仅仅是一个最大值,看高度起了一个作用。 我必须做一些统计杂耍(饶恕行话)吗?

我有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是网格的左上angular并包含单词DayMax(Spd)是整个Spd列的最大值。 OffsetMatch是Excel函数。

另一个解决scheme是在每个工作表中命名Spd数据的范围,比如Spd_1Spd_2 ,等等。 然后,Excel函数MAX(INDIRECT("Spd_1"))MAX(INDIRECT("Spd_2"))等可以在单个表单中以string表示的命名范围上使用。 然后你可以使用一个单一的maxfunctionfind相应的一天。

如果你可以在R加载相同的数据作为数据框,那么你可以做这样的subset(df,Spd==max(df[,"Spd"]))$Day其中df是数据的名称你通过read.csv或者read.table或者类似的东西读入框架。

以上两点都可以用min代替max来重复find最低速度。

如果你不能使用这种格式,或者不能使用Excel的INDIRECT ,那么最好的解决办法是在Excel中使用简单的VBA循环遍历表单。

在所有情况下,你可能不得不考虑你将如何处理关系 – 就像在同一(最高)速度的两个或更多不同的日子里。

如果你可以和R一起为重复的列名创build独特的列名,那么你就不需要费力地将第#天放入单独的列名中(对于这篇文章来说这是一个很大的麻烦),然后你可以删除“日”标题行,如上所述将读月份的列留在一起,并将其转换为R可以用read.csv()读取的CSV。

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

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

为了获得整个dataframe的最大速度值的列名,我们需要首先在“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模块是一个可能的解决scheme:

 #! /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 >>>