R - 数据帧 - 按列分组

时间:2018-01-20 07:01:41

标签: r dataframe dplyr aggregate

我尝试了下面的代码,它可以按月和变量的总和来计算。

trades_test = read.csv("trades_test.csv", header=TRUE)
trades_test

trades_test %>%
  group_by(Trade_date, Country_code) %>%
  summarise(a_sum=sum(Trade_value_local))

> trades_test = read.csv("trades_test.csv", header=TRUE)
> trades_test
     Sedol                   Description Trans_type Trade_date  Quantity Price_local CCY_local Trade_value_local Trade_type Country_code
1  B01NPJ1 TATA CONSULTANCY SERVICES LTD        BUY  11-Jan-18    38,164       40.88       INR         1,560,044    Buy New           IN
2  B012W42                 PUBLIC BK BHD        BUY  11-Jan-18   221,400        4.92       MYR         1,089,969   Buy More           MY
3  6288190            AU OPTRONICS CORP.        BUY  11-Jan-18 2,210,000        0.42       TWD           923,639    Buy New           TW
4  6491318            KINGBOARD CHEMICAL        BUY  11-Jan-18   138,500        5.54       HKD           767,200    Buy New           HK
5  6205122                   INFOSYS LTD        BUY  12-Jan-18    48,855       15.30       INR           747,548    Buy New           IN
6  6196152                 CITIC LIMITED       SELL  12-Jan-18   -81,000        1.41       HKD          -113,985  Sell Some           HK
7  6451055              HYUNDAI MOTOR CO       SELL  11-Jan-18      -786      147.42       KRW          -115,870   Sell All           KR
8  6868398              TELEKOM MALAYSIA       SELL  12-Jan-18   -83,100        1.47       MYR          -122,119  Sell Some           MY
9  6243586                      SATS LTD       SELL  11-Jan-18   -33,500        3.90       SGD          -130,632  Sell Some           SG
10 6253767               INDIAN OIL CORP       SELL  13-Jan-18   -21,571        6.06       INR          -130,824   Sell All           IN
> 
> trades_test %>%
+   group_by(Trade_date, Country_code) %>%
+   summarise(a_sum=sum(Trade_value_local))
Error in summarise_impl(.data, dots) : 
  Evaluation error: <U+0091>sum<U+0092> not meaningful for factors.

然而,当我尝试对我的trade_test数据做同样的事情时,我无法获得与此工作代码一样的预期输出。感谢是否有人可以告知错误,我无法获得与上述示例相同的预期输出。

谢谢。

@Entity
public class Book {

@Id
@Column(name="bookName")
private String bookName;

@Column(name="author")
private String author;

@Column(name="copiesTotal")
private int    copiesTotal;

@Column(name="copiesAvailable")
private int    copiesAvailable;

@Column(name="people")
//should we use any other annotation here?
private JSONObject people;
//setters and getters omitted

public Book() {

}

public Book(String bookName, String author, int copiesTotal, int copiesAvailable, JSONObject people) {
    super();
    this.bookName = bookName;
    this.author = author;
    this.copiesTotal = copiesTotal;
    this.copiesAvailable = copiesAvailable;
    this.people = people;

}

1 个答案:

答案 0 :(得分:3)

键入class(trades_test$Trade_value_local),您会看到它是factor,而不是数字,并且汇总factors没有意义。因此,您必须首先将其转换为数字,然后删除逗号,然后解析为numeric。你可以这样做:

trades_test %>%
  mutate(Trade_value_local = as.numeric(gsub(',','',Trade_value_local)))
  group_by(Trade_date, Country_code) %>%
  summarise(a_sum=sum(Trade_value_local))