在R中创建2级透视表

时间:2018-05-29 17:54:03

标签: r

我有一个具有不同值的数据框,我想使用R构建一个“数据透视表”,但是有2个级别,我知道如何将它分组为1级,但不是2级。

这是我期待的结果: 2 level pivot table

Region/Country Sales Asia 3,452,710 China 3,452,710 Europe 2,881,793 Germany 1,846,392 Spain 1,035,401 South America 3,111,193 Argentina 1,059,341 Brazil 2,051,852

这是数据的一个示例,我想将它按区域和国家分组。

国家地区销售 China Asia $109,680 China Asia $141,165 China Asia $77,516 China Asia $154,060 China Asia $38,597 China Asia $125,985 China Asia $91,850 China Asia $90,513 China Asia $51,710 China Asia $53,283 China Asia $77,352 China Asia $5,534 China Asia $6,645 China Asia $135,301 China Asia $176,095 China Asia $146,270 China Asia $54,665 China Asia $62,111 China Asia $64,390 China Asia $30,197 China Asia $132,397 China Asia $165,454 China Asia $113,084 China Asia $40,922 China Asia $161,574 China Asia $16,753 China Asia $54,616 China Asia $117,857 China Asia $193,862 China Asia $23,478 China Asia $16,135 China Asia $103,860 China Asia $72,478 China Asia $45,570 China Asia $81,337 China Asia $174,224

2 个答案:

答案 0 :(得分:0)

如果没有可重复的数据集,很难回答这个问题,但现在就是这样。尝试安装dplyr和hflights库以使用代码示例。

#install.packages("hflights")
#install.packages("dplyr")

library(dplyr)
library(hflights)

head(hflights)

# filter data for 2011 inbound flights to DFW
data_2011_DFW <- filter(hflights, Dest == "DFW", Year == 2011, Month == 1)

# group by Destination Airport and Month
data_2011_DFW_Carrier <- group_by(data_2011_DFW, UniqueCarrier)

# create a custom summary of observations
summarize(data_2011_DFW_Carrier, count = n(), delay = mean(ArrDelay, na.rm = T))

答案 1 :(得分:-1)

这是数据的一个示例,我想将它按区域和国家分组。

Set<String> set = map.keySet();
for(String s : set){
    Log.e(TAG, "Map key = " + s + " value = " + map.get(s));
}