从数据框中提取数据

从数据框中提取数据

问题描述:

我有一个数据框,如下所示:

I have a data frame that looks like this:

    Code           ID          X1         X2
1  1000             2         1.6       250.6
2  1000             3         0.15      340.9
3  1001             2         0.53      441.7
4  1001             3         1.8       499.0
5  1002             2         4.4       516.6
6  1003             3         4.9       616.6

我想做的是使用唯一的代码和每个唯一的ID作为列创建一个新的数据帧(有两个唯一的ID:2和3),具有相应的X1和X2值,所以结果应如下所示:

What I would like to do is to create a new data frame with unique codes and each unique ID as a column (there are two unique IDs:2 and 3), with the corresponding X1 and X2 values, so the result should look like this:

    Code           ID2X1       ID2X2      ID3X1       ID3X2
1  1000             1.6        250.6        0.15      340.9
2  1001            0.53        441.7         1.8      499.0
5  1002             4.4        516.6          NA         NA
6  1003             NA            NA         4.9      616.6

我使用唯一功能来提取唯一的代码,所以我有第一列,但不能想到提取数据的有效方法。请注意,一些代码没有ID2或ID3的值。

I used the "unique" function to extract the unique codes so I have the first column, but couldn't think of an efficient way to extract the data. Please note that some of the codes don't have values for either ID2 or ID3.

这是一个基本的 reshape 问题,从长更改为宽。

This is a basic reshape problem, reshaping from "long" to "wide".

尝试以下(假设您的 data.frame 被称为mydf):

Try the following (assuming your data.frame is called "mydf"):

reshape(mydf, idvar="Code", timevar="ID", direction = "wide")
#   Code X1.2  X2.2 X1.3  X2.3
# 1 1000 1.60 250.6 0.15 340.9
# 3 1001 0.53 441.7 1.80 499.0
# 5 1002 4.40 516.6   NA    NA
# 6 1003   NA    NA 4.90 616.6