简体   繁体   English

如何在R或Excel中从列到行转置不同的组

[英]how to transpose different groups from column to row in R or excel

I searched a few answers but it seems none can answer my question 我搜索了一些答案,但似乎没有人可以回答我的问题

I have a data look like this (2 columns, 2 groups group"1" and group"2") 我有一个看起来像这样的数据(2列,2组“ 1”组和“ 2”组)

  • 1 10 1 10
  • 1 20 1 20
  • 1 50 1 50
  • 2 40 2 40
  • 2 30 2 30
  • 2 60 2 60

and I want to transpose it from column to row by each group the result will look like this 我想按每个组将其从列到行进行转置,结果将如下所示

  • 1 1 1 1 1 1
  • 10 20 50 10 20 50
  • 2 2 2 2 2 2
  • 40 30 60 40 30 60

Since I have a lot of groups, I cannot manually do it one by one. 由于我有很多小组,因此我无法一一手动进行。 Could anyone please help me? 谁能帮我吗? Thank you. 谢谢。

Thanks guys, since all groups have the same length (inspired by the comment from Floris), actually I can simply just use matrix to solve the problem 谢谢大家,由于所有组的长度相同(受Floris的评论启发),实际上我可以简单地使用矩阵来解决问题

    x=read.table(header=F,text="10 20 50 40 30 60")
    matrix(x,nrow=2,byrow=T)
x <- read.table(header=F, text="1 10
1 20
1 50
2 40
2 30
2 60")

do.call(rbind, by(x, x$V1, FUN=t))
##     1  2  3
## V1  1  1  1
## V2 10 20 50
## V1  2  2  2
## V2 40 30 60

If you know how to use VBA, it's pretty trivial to write a short sub() that will do this for you. 如果您知道如何使用VBA,则编写一个简短的sub()可以为您做这件事很简单。 I am hard coding the address - you can figure out how to move it around. 我正在对地址进行严格编码-您可以弄清楚如何移动它。 I am assuming your data is on the active sheet when you call this macro, and you want the copies to appear to the right of the original (which are in columns A and B, starting in row 2 - giving you space for a label above). 我假设调用此宏时您的数据在活动工作表上,并且您希望副本显示在原始文件的右侧(它们在A和B列中,从第2行开始-为上方留有一个标签空间) )。 Also I am going to assume that your groups are numbered 1, 2, 3... and that you don't need their values copying. 我还要假设您的组编号为1、2、3 ...,并且您不需要复制其值。 Then the code looks like this (press F-11 to open the VBA editor, insert a module, and copy this) 然后,代码如下所示(按F-11键打开VBA编辑器,插入模块,然后复制它)

Sub bigSwap()
dim R as Range, c as Range
dim grp
dim grpCount(1 To 100) ' assuming no more than 100 groups, or adjust this number
dim i

' just making sure it's properly initialized
for i=1 To 100
  grpCount(ii)=0
next i


Set R = Range("B2", [B2].End(xlDown)) ' this finds all the cells with values
' note - I am using [B2] as shorthand for Range("B2")

For each c in R.cells
  grp = c.offset(0, -1).value  ' find the corresponding group
  [D1].offset(grp, grpCount(grp)).value = c.value
  grpCount(grp) = grpCount(grp)+1  ' keep track of # in this row
next

End Sub

I was not able to test this, but I believe it's good. 我无法对此进行测试,但我相信它很好。 If you run into issues, just leave a comment. 如果遇到问题,请发表评论。

Transpose cluster of cell from column to rows 将单元簇从列转置为行

Assuming that A2:A16 contains the data, try... 假设A2:A16包含数据,请尝试...

C2, copied across and down: C2,上下复制:

=INDEX($A$2:$A$16,ROWS(C$2:C2)*3-3+COLUMNS($C2:C2))

Adjust the references, accordingly. 相应地调整参考。

Try for one row first then do the next and then merge 先尝试一行,然后再进行下一行,然后合并

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

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