如何删除特定列中的重复值而不删除相关行

时间:2018-05-22 19:17:04

标签: r highcharts shiny plotly

要删除特定列中的重复值而不删除与重复列值相关的行,如下例所示:

Input
-----
    Date    Market      Quantity
4/2/2018    Indonesia   1000
4/2/2018    Australia   500
4/2/2018    India       300
4/2/2018    USA         500
4/2/2018    Germany     200
5/2/2018    India       400
5/2/2018    Japan       400
5/2/2018    Russia      457
6/2/2018    Austria     260
6/2/2018    Swiss       700
6/2/2018    USA         1200
6/2/2018    Indonesia   400


output
------
    Date    Market      Quantity
4/2/2018    Indonesia   1000
            Australia   500
            India       300
            USA         500
            Germany     200
5/2/2018    India       400
            Japan       400
            Russia      457
6/2/2018    Austria     260
            Swiss       700
            USA         1200
            Indonesia   400

如果可能的话,如何绘制相同输出的图形(条形/列)(如给定的那样)? Sample Graph

1 个答案:

答案 0 :(得分:0)

我会将此添加到评论中,但我还没有权利......

我认为您实际上并不想更改数据,但正如评论中提到的那样,有很简单的方法可以做到这一点。

如果您只是试图以图形方式显示多维数据并且您只是不熟悉库语法,请尝试以下代码...

df <- data.frame(Date = c('2018/04/02','2018/04/02','2018/04/02','2018/04/02','2018/04/02','2018/05/02','2018/05/02','2018/05/02','2018/06/02','2018/06/02','2018/06/02','2018/06/02'),
  Market = c('Indonesia','Australia','India','USA','Germany','India','Japan','Russia','Austria','Swiss','USA','Indonesia'),
  Quantity = c(1000,500,300,500,200,400,400,457,260,700,1200,400),
  stringsAsFactors = F)

plotly::ggplotly(
      ggplot2::ggplot(df, ggplot2::aes(x=Market, y=Quantity)) +
        ggplot2::geom_col(ggplot2::aes(fill=Market))+
        ggplot2::facet_grid(~Date,scale='free_x') +
        ggthemes::theme_tufte()
      )