R reshape,按块重组数据帧
我正在尝试重塑数据框:
目前它看起来像这样:
ID | Gender |A1 | A2 | A3 | B1 | B2 | B3
ID_1 | m | 3 | 3 | 3 | 2 | 3 | 2
ID_2 | f | 1 | 1 | 1 | 4 | 4 | 4
我想要类似的东西:
ID | Gender | A1 | A2 | A3
ID_1 | m | 3 | 3 | 3 <- this would be columns A1 - A3 for ID 1
ID_1 | m | 2 | 2 | 2 <- this would be columns B1 - B3 for ID 1
ID_2 | f | 1 | 1 | 1 <- this would be columns A1 - A3 for ID 2
ID_2 | f | 4 | 4 | 4 <- this would be columns B1 - B3 for ID 2
(A1和B1/A2和B2是相同的变量(就内容而言),所以例如:A1和B1将是测试 1 的结果以及 A2 和 B2 的两个变量都包含测试 2 的结果。因此,为了对其进行评估,我需要将测试 1 的所有结果放在一列中,并将测试 2 的所有结果放在另一列中。 我尝试用“融化”来解决这个问题,但它只是将数据帧一一融化,而不是作为块融化。 (因为我需要保持前 2 列不变,只重新排列最后 4 列,但作为三块) 还有其他想法吗?谢谢!
I am trying to reshape a dataframe:
Currently it looks like this:
ID | Gender |A1 | A2 | A3 | B1 | B2 | B3
ID_1 | m | 3 | 3 | 3 | 2 | 3 | 2
ID_2 | f | 1 | 1 | 1 | 4 | 4 | 4
I want to have something like:
ID | Gender | A1 | A2 | A3
ID_1 | m | 3 | 3 | 3 <- this would be columns A1 - A3 for ID 1
ID_1 | m | 2 | 2 | 2 <- this would be columns B1 - B3 for ID 1
ID_2 | f | 1 | 1 | 1 <- this would be columns A1 - A3 for ID 2
ID_2 | f | 4 | 4 | 4 <- this would be columns B1 - B3 for ID 2
(A1 and B1 / A2 and B2 are the same variables (with regard to the content), so for example: A1 and B1 would be both variables for the result of Test 1 and A2 and B2 both contain the result of Test 2. So in order to evaluate it I need all the result of Test1 in one column and all of Test 2 in another column.
I tried to solve this with "melt", but it only melts down the dataframe one by one, not as chunks. (since I need to keep the first 2 columns the way they are and only rearrange the last 4 columns, but as chunks of three)
Any other ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用来自 R 基础的
reshape
的衬垫。One liner using
reshape
from base R.正如@Andrie所说,第一步是将数据与给定的列(ID和性别)融合。正如您所说,您的问题是确定哪些列“放在一起”。这是一种方法,最初将该信息编码在列名称中,然后从那里将其提取出来。
首先是一些虚拟数据
请注意,我为这些列命名的名称系统地指示了哪个测试以及它属于哪个组。
使用
reshape2
包融化数据,然后获取其中有两条信息的
variable
列(测试和组),并将这两条信息分成两个独立的列。现在很容易铸造,因为测试和组是分开的。
这给出了
As @Andrie said, the first step is melting the data with your given columns (ID and gender). Your problem, as you say, is identifying what columns then "go together". Here is one approach, originally encoding that information in column names, and then pulling it out from there.
First some dummy data
Note that I've named the columns with a name that systematically indicates which test and which group it is part of.
Using the
reshape2
packageMelt the data, and then take the
variable
column which has two pieces of information in it (test and group), and split those two bits of info into two separate columns.Now it is easy to cast since the test and the group are separate.
Which gives
我更喜欢布莱恩的回答,但这里有一种使用基本包来做到这一点的方法。虽然在我看来很丑。
您的数据框:
代码
I like Brian's answer better but here's a way to do it with the base package. Pretty ugly though in my opinion.
Your dataframe:
Code
怎么样:
How about: