重塑R中的数据框:将列更改为行名称

重塑R中的数据框:将列更改为行名称,r,R,我在R工作。我有一个三列的数据框。A列包含公司名称b列包含日期c列包含价格 > 我想重塑我的数据框,使公司名称变为行名称,日期变为列名,价格在相应的单元格中 Apple Coke Pepsi 2012/06/03 410 210 152 2012/06/03 420 220 142 2012/06/03 460 260 162 我尝试使用melt和dcast函数,但找不到解决方案。

我在R工作。我有一个三列的数据框。A列包含公司名称b列包含日期c列包含价格

>

我想重塑我的数据框,使公司名称变为行名称,日期变为列名,价格在相应的单元格中

             Apple     Coke    Pepsi

2012/06/03   410      210      152
2012/06/03   420      220      142
2012/06/03   460      260      162

我尝试使用melt和dcast函数,但找不到解决方案。

您可以使用
tidyr::spread

library(tidyr)
spread(d,A,C)
输出

           B Apple Coke Pepsi
1 2012/06/01   410  210   152
2 2012/06/02   420  220   142
3 2012/06/03   440  260   122
资料


d您可以使用
tidyr::spread

library(tidyr)
spread(d,A,C)
输出

           B Apple Coke Pepsi
1 2012/06/01   410  210   152
2 2012/06/02   420  220   142
3 2012/06/03   440  260   122
资料


d您可以使用
restrape2
包中的
dcast()
函数将数据从“长”格式重新格式化为“宽”格式:

library(reshape2)
dcast(df1, B ~ A , value.var = "C")
#           B Apple Coke Pepsi
#1 2012/06/01   410  210   152
#2 2012/06/02   420  220   142
#3 2012/06/03   440  260   122
数据

df1 <- structure(list(A = c("Apple", "Coke", "Pepsi", "Apple", "Coke", 
           "Pepsi", "Apple", "Coke", "Pepsi"), 
           B = c("2012/06/01", "2012/06/01", "2012/06/01", "2012/06/02", 
                 "2012/06/02", "2012/06/02", "2012/06/03", "2012/06/03", 
                 "2012/06/03"), 
            C = c(410L, 210L, 152L, 420L, 220L, 142L, 440L, 260L, 122L)),
           .Names = c("A", "B", "C"),
            class = "data.frame", row.names = c(NA, -9L))

df1您可以使用
restrape2
包中的
dcast()
函数将数据从“长”格式重新格式化为“宽”格式:

library(reshape2)
dcast(df1, B ~ A , value.var = "C")
#           B Apple Coke Pepsi
#1 2012/06/01   410  210   152
#2 2012/06/02   420  220   142
#3 2012/06/03   440  260   122
数据

df1 <- structure(list(A = c("Apple", "Coke", "Pepsi", "Apple", "Coke", 
           "Pepsi", "Apple", "Coke", "Pepsi"), 
           B = c("2012/06/01", "2012/06/01", "2012/06/01", "2012/06/02", 
                 "2012/06/02", "2012/06/02", "2012/06/03", "2012/06/03", 
                 "2012/06/03"), 
            C = c(410L, 210L, 152L, 420L, 220L, 142L, 440L, 260L, 122L)),
           .Names = c("A", "B", "C"),
            class = "data.frame", row.names = c(NA, -9L))

df1我们可以从基本包
stats
中使用函数
重塑

reshape(df, idvar='B', timevar='A', direction='wide')
输出:

           B C.Apple C.Coke C.Pepsi
1 2012/06/01     410    210     152
4 2012/06/02     420    220     142
7 2012/06/03     440    260     122
数据:

df <- structure(list(A = c("Apple", "Coke", "Pepsi", "Apple", "Coke", 
           "Pepsi", "Apple", "Coke", "Pepsi"), 
           B = c("2012/06/01", "2012/06/01", "2012/06/01", "2012/06/02", 
                 "2012/06/02", "2012/06/02", "2012/06/03", "2012/06/03", 
                 "2012/06/03"), 
            C = c(410L, 210L, 152L, 420L, 220L, 142L, 440L, 260L, 122L)),
           .Names = c("A", "B", "C"),
            class = "data.frame", row.names = c(NA, -9L))

df我们可以使用基本包
stats
中的函数
重塑

reshape(df, idvar='B', timevar='A', direction='wide')
输出:

           B C.Apple C.Coke C.Pepsi
1 2012/06/01     410    210     152
4 2012/06/02     420    220     142
7 2012/06/03     440    260     122
数据:

df <- structure(list(A = c("Apple", "Coke", "Pepsi", "Apple", "Coke", 
           "Pepsi", "Apple", "Coke", "Pepsi"), 
           B = c("2012/06/01", "2012/06/01", "2012/06/01", "2012/06/02", 
                 "2012/06/02", "2012/06/02", "2012/06/03", "2012/06/03", 
                 "2012/06/03"), 
            C = c(410L, 210L, 152L, 420L, 220L, 142L, 440L, 260L, 122L)),
           .Names = c("A", "B", "C"),
            class = "data.frame", row.names = c(NA, -9L))

df您不需要先融化。您的输入和输出不匹配…可能的重复您不需要先融化。您的输入和输出不匹配…可能的重复