R将Datatable不同的列值转换为列名和列值,作为来自另一列的值 [英] R convert Datatable distinct column values to column names and column values as values from another column

查看:474
本文介绍了R将Datatable不同的列值转换为列名和列值,作为来自另一列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的R数据表(实际数据集较大,但是为了便于理解而简化了)

I have an R data table with three columns (The actual dataset is bigger but simplifying for better understanding)

Column_One, Column_Two, Column_Three

A, 1, 4
A, 2, 3
A, 3, 77
B, 1, 44
B, 2, 32
B, 3, 770
C, 1, 43
C, 2, 310
C, 3, 68

我想从上面创建一个新的矩阵(数据表),如下所示.

I want to create a new matrix (data table) from the above as shown below.

A, B, C
4, 44, 43
3, 32, 310
77, 770, 68

请注意,实际数据表中的第一和第二列有数百种不同的值.因此,需要一个通用的解决方案.

Please note in the actual data table there are hundreds of different values for column one and two. Hence a generic solution would be needed.

任何问题,请让我知道.非常感谢任何建议.

Any questions, please let me know. Much appreciative of any suggestions.

在第四列中可能还有另一个级别,零列,它链接了一些列.在这种情况下,我们需要根据第零列创建新的数据表,然后将解决方案应用于每个子数据表的第一列.请提出最快/最简单的方法.

There could be another level as in a fourth column, column zero, which links a few of the column ones. In this case we need to create new data tables based on column zero and then apply the solution to column one on each sub data table. Please suggest the quickest / simplest way possible.

Column_Zero, Column_One, Column_Two, Column_Three

XX,A, 1, 4
XX,A, 2, 3
XX,A, 3, 77
XX,B, 1, 44
XX,B, 2, 32
XX,B, 3, 770
XX,C, 1, 43
XX,C, 2, 310
XX,C, 3, 68       
YY,A1, 1, 4
YY,A1, 2, 3
YY,A1, 3, 77
YY,B1, 1, 44
YY,B1, 2, 32
YY,B1, 3, 770
YY,C1, 1, 43
YY,C1, 2, 310
YY,C1, 3, 68 
YY,D2, 1, 4
YY,D2, 2, 5
YY,D2, 3, 6 

---------等等-----

--------- And so on -----

然后我们需要创建

------数据表一------

------ Data Table one ------

A, B, C
4, 44, 43
3, 32, 310
77, 770, 68

------数据表二------

------ Data Table Two ------

A1, B1, C1, D2
4, 44, 43,4
3, 32, 310,5
77, 770, 68,6

--------等等-----

------ and so on -----

相关问题:

一旦该矩阵被拆分和重铸,了解新数据结构及其组件的维度以及如何分别访问它们就变得很重要,这将在此处进行讨论:

Once this matrix is split and recast, it becomes important to know the dimensions of the new data structure and its components and also how to access them individually, which is discussed here:

R查找多维数组维度尺寸

推荐答案

我们可以使用acast将'long'格式转换为'wide'格式.结果数据集将为matrix.

We can use acast to convert from 'long' to 'wide' format. The resulting dataset will be a matrix.

library(reshape2)
acast(df1, Column_Two~Column_One, value.var="Column_Three")
#   A   B   C
#1  4  44  43
#2  3  32 310
#3 77 770  68


对于第二个数据集,我们可以通过"Column_Zero"进行split,然后像以前一样循环遍历list并执行acast


For the second dataset, we can split by "Column_Zero" and then loop over the list and do the acast as before

 lst <- lapply(split(df2[-1], df2$Column_Zero), function(x) 
         acast(x, Column_Two~Column_One,value.var="Column_Three"))

lst
#$XX
#   A   B   C
#1  4  44  43
#2  3  32 310
#3 77 770  68

#$YY
#  A1  B1  C1 D2
#1  4  44  43  4
#2  3  32 310  5
#3 77 770  68  6

这篇关于R将Datatable不同的列值转换为列名和列值,作为来自另一列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆