- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在适应一份新工作,我与同事分享的大部分工作都是通过 MS Excel 完成的。我经常使用数据透视表,因此需要“堆叠”数据,正是 melt()
的输出reshape
中的函数(reshape2) R 中的包,我已经开始依赖它。
任何人都可以让我开始使用 VBA 宏来完成此操作,或者是否已经存在?
宏的轮廓是:
require(reshape)
melt(your.unstacked.dataframe, id.vars = 1:4)
# unstacked data
> df1
Year Month Country Sport No_wins No_losses High_score Total_games
2 2010 5 USA Soccer 4 3 5 9
3 2010 6 USA Soccer 5 3 4 8
4 2010 5 CAN Soccer 2 9 7 11
5 2010 6 CAN Soccer 4 8 4 13
6 2009 5 USA Soccer 8 1 4 9
7 2009 6 USA Soccer 0 0 3 2
8 2009 5 CAN Soccer 2 0 6 3
9 2009 6 CAN Soccer 3 0 8 3
# stacking the data
> require(reshape)
> melt(df1, id.vars=1:4)
Year Month Country Sport variable value
1 2010 5 USA Soccer No_wins 4
2 2010 6 USA Soccer No_wins 5
3 2010 5 CAN Soccer No_wins 2
4 2010 6 CAN Soccer No_wins 4
5 2009 5 USA Soccer No_wins 8
6 2009 6 USA Soccer No_wins 0
7 2009 5 CAN Soccer No_wins 2
8 2009 6 CAN Soccer No_wins 3
9 2010 5 USA Soccer No_losses 3
10 2010 6 USA Soccer No_losses 3
11 2010 5 CAN Soccer No_losses 9
12 2010 6 CAN Soccer No_losses 8
13 2009 5 USA Soccer No_losses 1
14 2009 6 USA Soccer No_losses 0
15 2009 5 CAN Soccer No_losses 0
16 2009 6 CAN Soccer No_losses 0
17 2010 5 USA Soccer High_score 5
18 2010 6 USA Soccer High_score 4
19 2010 5 CAN Soccer High_score 7
20 2010 6 CAN Soccer High_score 4
21 2009 5 USA Soccer High_score 4
22 2009 6 USA Soccer High_score 3
23 2009 5 CAN Soccer High_score 6
24 2009 6 CAN Soccer High_score 8
25 2010 5 USA Soccer Total_games 9
26 2010 6 USA Soccer Total_games 8
27 2010 5 CAN Soccer Total_games 11
28 2010 6 CAN Soccer Total_games 13
29 2009 5 USA Soccer Total_games 9
30 2009 6 USA Soccer Total_games 2
31 2009 5 CAN Soccer Total_games 3
32 2009 6 CAN Soccer Total_games 3
最佳答案
我的博客上有两篇关于在 Excel/VBA 中执行此操作的帖子,其中包含可用代码和可下载的工作簿:
http://yoursumbuddy.com/data-normalizer
http://yoursumbuddy.com/data-normalizer-the-sql/
这是代码:
'Arguments
'List: The range to be normalized.
'RepeatingColsCount: The number of columns, starting with the leftmost,
' whose headings remain the same.
'NormalizedColHeader: The column header for the rolled-up category.
'DataColHeader: The column header for the normalized data.
'NewWorkbook: Put the sheet with the data in a new workbook?
'
'NOTE: The data must be in a contiguous range and the
'columns that will be repeated must be to the left,
'with the columns to be normalized to the right.
Sub NormalizeList(List As Excel.Range, RepeatingColsCount As Long, _
NormalizedColHeader As String, DataColHeader As String, _
Optional NewWorkbook As Boolean = False)
Dim FirstNormalizingCol As Long, NormalizingColsCount As Long
Dim ColsToRepeat As Excel.Range, ColsToNormalize As Excel.Range
Dim NormalizedRowsCount As Long
Dim RepeatingList() As String
Dim NormalizedList() As Variant
Dim ListIndex As Long, i As Long, j As Long
Dim wbSource As Excel.Workbook, wbTarget As Excel.Workbook
Dim wsTarget As Excel.Worksheet
With List
'If the normalized list won't fit, you must quit.
If .Rows.Count * (.Columns.Count - RepeatingColsCount) > .Parent.Rows.Count Then
MsgBox "The normalized list will be too many rows.", _
vbExclamation + vbOKOnly, "Sorry"
Exit Sub
End If
'You have the range to be normalized and the count of leftmost rows to be repeated.
'This section uses those arguments to set the two ranges to parse
'and the two corresponding arrays to fill
FirstNormalizingCol = RepeatingColsCount + 1
NormalizingColsCount = .Columns.Count - RepeatingColsCount
Set ColsToRepeat = .Cells(1).Resize(.Rows.Count, RepeatingColsCount)
Set ColsToNormalize = .Cells(1, FirstNormalizingCol).Resize(.Rows.Count, NormalizingColsCount)
NormalizedRowsCount = ColsToNormalize.Columns.Count * .Rows.Count
ReDim RepeatingList(1 To NormalizedRowsCount, 1 To RepeatingColsCount)
ReDim NormalizedList(1 To NormalizedRowsCount, 1 To 2)
End With
'Fill in every i elements of the repeating array with the repeating row labels.
For i = 1 To NormalizedRowsCount Step NormalizingColsCount
ListIndex = ListIndex + 1
For j = 1 To RepeatingColsCount
RepeatingList(i, j) = List.Cells(ListIndex, j).Value2
Next j
Next i
'We stepped over most rows above, so fill in other repeating array elements.
For i = 1 To NormalizedRowsCount
For j = 1 To RepeatingColsCount
If RepeatingList(i, j) = "" Then
RepeatingList(i, j) = RepeatingList(i - 1, j)
End If
Next j
Next i
'Fill in each element of the first dimension of the normalizing array
'with the former column header (which is now another row label) and the data.
With ColsToNormalize
For i = 1 To .Rows.Count
For j = 1 To .Columns.Count
NormalizedList(((i - 1) * NormalizingColsCount) + j, 1) = .Cells(1, j)
NormalizedList(((i - 1) * NormalizingColsCount) + j, 2) = .Cells(i, j)
Next j
Next i
End With
'Put the normal data in the same workbook, or a new one.
If NewWorkbook Then
Set wbTarget = Workbooks.Add
Set wsTarget = wbTarget.Worksheets(1)
Else
Set wbSource = List.Parent.Parent
With wbSource.Worksheets
Set wsTarget = .Add(after:=.Item(.Count))
End With
End If
With wsTarget
'Put the data from the two arrays in the new worksheet.
.Range("A1").Resize(NormalizedRowsCount, RepeatingColsCount) = RepeatingList
.Cells(1, FirstNormalizingCol).Resize(NormalizedRowsCount, 2) = NormalizedList
'At this point there will be repeated header rows, so delete all but one.
.Range("1:" & NormalizingColsCount - 1).EntireRow.Delete
'Add the headers for the new label column and the data column.
.Cells(1, FirstNormalizingCol).Value = NormalizedColHeader
.Cells(1, FirstNormalizingCol + 1).Value = DataColHeader
End With
End Sub
Sub TestIt()
NormalizeList ActiveSheet.UsedRange, 4, "Variable", "Value", False
End Sub
关于r - 使用VBA在excel中融化/ reshape ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921791/
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
我无法清楚地理解theano的reshape。我有一个形状的图像矩阵: [batch_size, stack1_size, stack2_size, height, width] ,其中有 s
如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。 另一个例子是“aabbaab”返回 1,因为“aabb”可以被 r
这是原始数据 a=[[1,2,3,4,5,6], [7,8,9,10,11,12]] 我想把它转换成这样的格式: b=[[1,2,3,7,8,9], [4,5,6,10,11,12]] a
我目前正在学习 CS231 作业,我意识到一些令人困惑的事情。在计算梯度时,当我第一次 reshape x 然后得到转置时,我得到了正确的结果。 x_r=x.reshape(x.shape[0],-1
这个问题在这里已经有了答案: Reshaping multiple sets of measurement columns (wide format) into single columns (lon
我有一个包含超过 1500 列的宽格式数据集。由于许多变量都是重复的,我想将其 reshape 为长形式。然而,r 抛出一个错误: Error in guess(varying) : Failed
我有一个长格式的数据框狗,我正在尝试使用 reshape() 函数将其重新格式化为宽格式。目前看起来是这样的: dogid month year trainingtype home scho
这个问题在这里已经有了答案: how to reshape an N length vector to a 3x(N/3) matrix in numpy using reshape (1 个回答)
我对 ndarray.reshape 的结构有疑问.我读过 numpy.reshape()和 ndarray.reshape是 python 中用于 reshape 数组的等效命令。 据我所知,num
所以这是我的麻烦:我想将一个长格式的数据文件改成宽格式。但是,我没有唯一的“j”变量;长格式文件中的每条记录都有几个关键变量。 例如,我想这样做: | caseid | gender | age |
Whis 这个数据框, df df id parameter visit value sex 1 01 blood V1 1 f 2 01 saliva V
我有一个列表,其中包含几个不同形状的 numpy 数组。我想将这个数组列表 reshape 为一个 numpy 向量,然后更改向量中的每个元素,然后将其 reshape 回原始数组列表。 例如: 输入
我有一个形状为 (1800,144) 的数组 (a) 其中 a[0:900,:] 都是实数,后半部分数组 a[900:1800,:] 全部为零。我想把数组的后半部分水平地放在前半部分旁边,然后将它们推
我有一个如下所示的数组: array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2
我正在创建一个 tf.Variable(),然后使用该变量创建一个简单的函数,然后我使用 tf.reshape() 展平原始变量,然后我在函数和展平变量之间使用了 tf.gradients()。为什么
我有一个名为 data 的数据框,我试图从中识别任何异常价格。 数据框头部看起来像: Date Last Price 0 29/12/2017 487.74 1 28/
我有一个 float vec 数组,我想对其进行 reshape vec.shape >>> (3,) len(vec[0]) # all 3 rows of vec have 150 columns
tl;dr 我可以在不使用 numpy.reshape 的情况下将 numpy 数组的 View 从 5x5x5x3x3x3 reshape 为 125x1x1x3x3x3 吗? 我想对一个体积(大小
set.seed(123)data <- data.frame(ID = 1:10, weight_hus = rnorm(10, 0, 1),
我是一名优秀的程序员,十分优秀!