I have this dataframe (df):
我有这个数据帧(df):
ID Type Order Revenue
1 apples 1 $100
1 oranges 2 $100
2 grapes 1 $500
2 oranges 2 $500
2 grapes 3 $500
2 grapes 4 $500
I want to transpose it to get this dataframe, where the column headers are the "order" column from the original data frame:
我想转置它以获取此数据帧,其中列标题是原始数据框中的“顺序”列:
ID 1 2 3 4 Revenue
1 apples oranges $100
2 grapes oranges grapes grapes $500
I tried dcast(df, df$ID, df$Order)
我试过dcast(df,df $ ID,df $ Order)
But that doesn't give me what I'm looking for. It gives me the order within the dataframe itself, like this:
但这并没有给我我正在寻找的东西。它给了我数据框本身的顺序,如下所示:
ID 1 2 3 4
1 1 2
2 1 2 3 4
I basically want the "type" to be populated within the dataframe, given a particular ID and order (this combination is unique.)
我基本上希望在给定特定ID和顺序的情况下在数据框中填充“类型”(这种组合是唯一的。)
Thanks so much in advance!!
非常感谢提前!!
EDIT: I would like the revenue column to be added while using dcast
编辑:我想在使用dcast时添加收入列
1 个解决方案
#1
1
dcast
takes a formula
as its second argument. So, you need to provide that along with the value column that needs to fill up the 'wide' format. In this case, the `value.var' is 'Type'
dcast将公式作为第二个参数。因此,您需要提供需要填充“宽”格式的值列。在这种情况下,`value.var'是'Type'
library(reshape2)
dcast(df, ID~Order, value.var='Type', fill='')
# ID 1 2 3 4
#1 1 apples oranges
#2 2 grapes oranges grapes grapes
Or you can use spread
which takes the argument similar to as showed in your post
或者您可以使用带有与您帖子中显示的类似的参数的spread
library(tidyr)
spread(df, Order, Type, fill='')
Update
For the updated dataset, change the 'formula'
对于更新的数据集,请更改“公式”
dcast(dfN, ID+Revenue~Order, value.var='Type', fill='')
# ID Revenue 1 2 3 4
#1 1 $100 apples oranges
#2 2 $500 grapes oranges grapes grapes
data
df <- structure(list(ID = c(1L, 1L, 2L, 2L, 2L, 2L), Type = c("apples",
"oranges", "grapes", "oranges", "grapes", "grapes"), Order = c(1L,
2L, 1L, 2L, 3L, 4L)), .Names = c("ID", "Type", "Order"),
class = "data.frame", row.names = c(NA, -6L))
#1
1
dcast
takes a formula
as its second argument. So, you need to provide that along with the value column that needs to fill up the 'wide' format. In this case, the `value.var' is 'Type'
dcast将公式作为第二个参数。因此,您需要提供需要填充“宽”格式的值列。在这种情况下,`value.var'是'Type'
library(reshape2)
dcast(df, ID~Order, value.var='Type', fill='')
# ID 1 2 3 4
#1 1 apples oranges
#2 2 grapes oranges grapes grapes
Or you can use spread
which takes the argument similar to as showed in your post
或者您可以使用带有与您帖子中显示的类似的参数的spread
library(tidyr)
spread(df, Order, Type, fill='')
Update
For the updated dataset, change the 'formula'
对于更新的数据集,请更改“公式”
dcast(dfN, ID+Revenue~Order, value.var='Type', fill='')
# ID Revenue 1 2 3 4
#1 1 $100 apples oranges
#2 2 $500 grapes oranges grapes grapes
data
df <- structure(list(ID = c(1L, 1L, 2L, 2L, 2L, 2L), Type = c("apples",
"oranges", "grapes", "oranges", "grapes", "grapes"), Order = c(1L,
2L, 1L, 2L, 3L, 4L)), .Names = c("ID", "Type", "Order"),
class = "data.frame", row.names = c(NA, -6L))