我有一个数据框,其中包含位置($ awk '/^delete_job:/{printf "%s", $0 OFS; next}1' file
),位置内的距离($ awk '{ORS=/^delete_job:/?OFS:RS}1' file
),值(loc
)和年份(dist
)列。
我想比较距离之间的比率,确定哪个比率更高并找到一年,当一个区域的比率高于另一个区域的比率时,如下所示(在第2年,距离“ 100”大于目标区域的距离)距离“ npr”)
cumRate
这似乎很简单,但我真的不知道从哪里开始...谢谢您的建议!
虚拟数据:
year
绘图数据
[![enter code here][1]][1]
所需的输出
loc = rep(c("a","b"), each = 6)
dist = rep(c("npr", "100", "npr", "100"), each = 3)
cumRate = c(0,0,4,0,1,2,0,0,1,3,5,7)
year = rep(c(1,2,3), 4)
df = data.frame(loc, dist, cumRate, year)
loc dist cumRate year
1 a npr 0 1
2 a npr 0 2
3 a npr 4 3
4 a 100 0 1
5 a 100 1 2
6 a 100 2 3
7 b npr 0 1
8 b npr 0 2
9 b npr 1 3
10 b 100 3 1
11 b 100 5 2
12 b 100 7 3
答案 0 :(得分:3)
这是一种不扩散的方法:
library(dplyr)
df %>% group_by(loc, year) %>%
filter(max(cumRate) != min(cumRate)) %>%
arrange(loc, year, desc(cumRate)) %>%
group_by(loc) %>%
slice(1)
# # A tibble: 2 x 4
# # Groups: loc [2]
# loc dist cumRate year
# <fctr> <fctr> <dbl> <dbl>
# 1 a 100 1 2
# 2 b 100 3 1
首先,我们删除cumRate
中没有变化的年份,然后按照位置,年份和降序暨速率对数据进行排序,并在每个位置中获取第一行。
答案 1 :(得分:2)
我认为您需要取消枢纽Sub ImportInformation()
WorksheetLoop
End Sub
Function WorksheetLoop()
Dim wb As Workbook
Dim ws As Worksheet
Dim foundCell As Range
Dim strFind As String
Dim fRow, fCol As Integer
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' This allows you to use excel functions by typing wf.<function name>
Set wf = WorksheetFunction
'Set the name of your output file, I assume its fixed in the Master File
‘Please note that I am running this out of the master file and I want it all in the Noi tab
Set NOI = ThisWorkbook.Worksheets("NOI")
'Retrieve Target File Path From User
Set FilePicker = Application.FileDialog(msoFileDialogFolderPicker)
‘This only selects a folder, however I would like it to select a SPECIFIC FILE
With FilePicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
' initialize the starting cell for the output file
pasterow = 4
‘I need this to be referring to the file that I choose
For Each ws In wb.Worksheets
If ws.Name <> "Funds" And ws.Name <> "Investments" Then
Next ws
Wb.Worksheets.Range.("F9").Copy
NOI.Range("C" & pasterow).PasteSpecial xlPasteValues, Transpose:=False
'Get find String
strFind = NOI.Range("C2").Value
'Find string in Row 16 of each row of current ACTIVE worksheet
Set foundCell = wb.Worksheets.Range("A16:IT16").Find(strFind, LookIn:=xlValues)
'If match cell is found
If Not foundCell Is Nothing Then
'Get row and column
fRow = foundCell.Row
fCol = foundCell.Column
'Copy data from active data worksheet “data” and copy over 300 columns (15 years).
‘ This is needed to find what specific date to start at. This portion works, I just need it to loop through each worksheet.
wb.Worksheets.active.Range(Cells(fRow + 1, fCol).Address & ":" & Cells(fRow + 1, fCol + 299).Address).Copy
'Paste in NOI tab of mater portfolio
NOI.Range("E" & pasterow).PasteSpecial xlPasteValues, Transpose:=False
wb.Application.CutCopyMode = False
Else
Call MsgBox("Try Again!” vbExclamation, "Finding String")
End If
Next Ws
wb.Close SaveChanges:=False
End Function
列:
dist