简体   繁体   English

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

[英]reshaping data frame in R: changing a column to row name

I am working in R. I have a data frame with three columns. 我在R中工作。我有一个包含三列的数据框。 Column A contains Company names column b contains date and column c contains prices A栏包含公司名称,b栏包含日期,c栏包含价格

> >

   A           B          C
  Apple     2012/06/01   410
  Coke      2012/06/01   210
  Pepsi     2012/06/01   152
  Apple     2012/06/02   420
  Coke      2012/06/02   220
  Pepsi     2012/06/02   142
  Apple     2012/06/03   440
  Coke      2012/06/03   260
  Pepsi     2012/06/03   122

I want to reshape my data frame so that Company name becomes row names and date becomes column names and price is in the corresponding cell 我想重塑数据框,以使公司名称变为行名称,日期变为列名称,而价格在相应的单元格中

             Apple     Coke    Pepsi

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

I tried using melt and dcast function, but couldn't find a solution. 我尝试使用melt和dcast函数,但找不到解决方案。

You could use tidyr::spread 您可以使用tidyr::spread

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

output 产量

           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

data 数据

d <- read.table(text="   A           B          C
  Apple     2012/06/01   410
  Coke      2012/06/01   210
  Pepsi     2012/06/01   152
  Apple     2012/06/02   420
  Coke      2012/06/02   220
  Pepsi     2012/06/02   142
  Apple     2012/06/03   440
  Coke      2012/06/03   260
  Pepsi     2012/06/03   122",head=TRUE)

You can reshape the data from "long" to "wide" format with the the dcast() function from the reshape2 package: 您可以使用reshape2包中的dcast()函数将数据从“长”格式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

data 数据

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))

This webpage is an example of a source that provides an explanation of the command that has been used. 网页是源示例,提供了有关已使用命令的说明。 It also describes other possibilities to convert data formats. 它还描述了转换数据格式的其他可能性。

We can use the function reshape from the base package stats : 我们可以使用基本包stats的函数reshape功能:

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

Output: 输出:

           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

Data: 数据:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM