Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将n路列联表转换为R中的数据帧_R - Fatal编程技术网

将n路列联表转换为R中的数据帧

将n路列联表转换为R中的数据帧,r,R,我正在尝试创建一个表,按产品名称、年份和地区列出销售的商品数量。我想要一张如下所示的桌子。有没有一种方法可以在R中实现这一点,而不是使用sqldf函数编写sql查询 Product_Name Region Year Count English Muffins 1 2015 10000 Bagel 1 2015 5601 Croissants ...........

我正在尝试创建一个表,按产品名称、年份和地区列出销售的商品数量。我想要一张如下所示的桌子。有没有一种方法可以在R中实现这一点,而不是使用sqldf函数编写sql查询

Product_Name      Region     Year     Count
English Muffins        1     2015     10000
Bagel                  1     2015      5601
Croissants              ....................
下面是生成示例数据的代码。此虚拟数据与上述样本计数不对应

Product_Name <- c("English Muffins","croissants","Kaiser rolls","Bagels","cinnamon puff","strawberry pastry")
Region_ID <- c(1:6)
Transaction_year <- c(2011:2016)

x <- data.frame()
for(i in 1:6)
  {
  for (j in 1:6)
    { 
    for(k in 1:6)
      {
      x <- rbind(x, data.frame(Product = Product_Name[i], Region = Region_ID[j], Year = Transaction_year[k]))
      }
    }
  }

Product\u Name
Product\u Name
Product\u Name是的,您可以通过使用
data.table
by
语句来执行此操作。非常类似于
SQL
分组方式:

library(data.table)
setDT(x)[,count := .N, by = c("Product","Region","Year") ]
head(x)

           Product Region Year count
1: English Muffins      1 2011     1
2: English Muffins      1 2012     1
3: English Muffins      1 2013     1
4: English Muffins      1 2014     1
5: English Muffins      1 2015     1
6: English Muffins      1 2016     1

是的,您可以使用
data.table
by
语句来执行此操作。非常类似于
SQL
分组方式:

library(data.table)
setDT(x)[,count := .N, by = c("Product","Region","Year") ]
head(x)

           Product Region Year count
1: English Muffins      1 2011     1
2: English Muffins      1 2012     1
3: English Muffins      1 2013     1
4: English Muffins      1 2014     1
5: English Muffins      1 2015     1
6: English Muffins      1 2016     1

这里不需要复杂的代码。您只需要一行代码:

> as.data.frame(table(x))
              Product Region Year Freq
1     English Muffins      1 2011    1
2          croissants      1 2011    1
3        Kaiser rolls      1 2011    1
4              Bagels      1 2011    1
5       cinnamon puff      1 2011    1
6   strawberry pastry      1 2011    1
...

table
函数将列联表生成为三维数组,而
as.data.frame
将列联表转换为所需格式的数据帧。如果
x
包含其他列,请确保仅将其子集为要制表的列。

此处不需要复杂的代码。您只需要一行代码:

> as.data.frame(table(x))
              Product Region Year Freq
1     English Muffins      1 2011    1
2          croissants      1 2011    1
3        Kaiser rolls      1 2011    1
4              Bagels      1 2011    1
5       cinnamon puff      1 2011    1
6   strawberry pastry      1 2011    1
...

table
函数将列联表生成为三维数组,而
as.data.frame
将列联表转换为所需格式的数据帧。如果
x
包含其他列,请确保仅将其子集为要制表的列。

基本函数
as.data.frame.table
将执行此操作。我假设您已经或可以沿着以下几条线创建一个R列联表:

mt <- with(x, table(Product,Region,Year))
另一个有用的表格展平功能是
ftable
。对于三向表,它提供了一个更紧凑的显示器版本,
print.table
将产生:

ftable(mt)
                         Year 2011 2012 2013 2014 2015 2016
Product           Region                                   
English Muffins   1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
                  4              1    1    1    1    1    1
                  5              1    1    1    1    1    1
                  6              1    1    1    1    1    1
croissants        1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
                  4              1    1    1    1    1    1
                  5              1    1    1    1    1    1
                  6              1    1    1    1    1    1
Kaiser rolls      1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
#-----snipped output--------
另一方面,如果请求是通过Count变量复制行数,则会这样做:

#Makes something like your original dataframe:
orig <- structure(list(Product_Name = structure(c(2L, 1L), .Label = c("Bagel", 
"English_Muffins"), class = "factor"), Region = c(1L, 1L), Year = c(2015L, 
2015L), Count = c(5L, 4L)), .Names = c("Product_Name", "Region", 
"Year", "Count"), class = "data.frame", row.names = c(NA, -2L))

xlong <- orig[ rep(rownames(orig), orig$Count) , ]
    > xlong
       Product_Name Region Year Count
1   English_Muffins      1 2015     5
1.1 English_Muffins      1 2015     5
1.2 English_Muffins      1 2015     5
1.3 English_Muffins      1 2015     5
1.4 English_Muffins      1 2015     5
2             Bagel      1 2015     4
2.1           Bagel      1 2015     4
2.2           Bagel      1 2015     4
2.3           Bagel      1 2015     4
#使其与原始数据帧类似:

orig基本函数
as.data.frame.table
将执行此操作。我假设您已经或可以沿着以下几条线创建一个R列联表:

mt <- with(x, table(Product,Region,Year))
另一个有用的表格展平功能是
ftable
。对于三向表,它提供了一个更紧凑的显示器版本,
print.table
将产生:

ftable(mt)
                         Year 2011 2012 2013 2014 2015 2016
Product           Region                                   
English Muffins   1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
                  4              1    1    1    1    1    1
                  5              1    1    1    1    1    1
                  6              1    1    1    1    1    1
croissants        1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
                  4              1    1    1    1    1    1
                  5              1    1    1    1    1    1
                  6              1    1    1    1    1    1
Kaiser rolls      1              1    1    1    1    1    1
                  2              1    1    1    1    1    1
                  3              1    1    1    1    1    1
#-----snipped output--------
另一方面,如果请求是通过Count变量复制行数,则会这样做:

#Makes something like your original dataframe:
orig <- structure(list(Product_Name = structure(c(2L, 1L), .Label = c("Bagel", 
"English_Muffins"), class = "factor"), Region = c(1L, 1L), Year = c(2015L, 
2015L), Count = c(5L, 4L)), .Names = c("Product_Name", "Region", 
"Year", "Count"), class = "data.frame", row.names = c(NA, -2L))

xlong <- orig[ rep(rownames(orig), orig$Count) , ]
    > xlong
       Product_Name Region Year Count
1   English_Muffins      1 2015     5
1.1 English_Muffins      1 2015     5
1.2 English_Muffins      1 2015     5
1.3 English_Muffins      1 2015     5
1.4 English_Muffins      1 2015     5
2             Bagel      1 2015     4
2.1           Bagel      1 2015     4
2.2           Bagel      1 2015     4
2.3           Bagel      1 2015     4
#使其与原始数据帧类似:

谢谢你!这是有用的。我想知道,在不向原始数据集中添加“count”变量的情况下,我们如何做到这一点。@user3897这表明我们所有回答者都误解了您的问题。您是否试图获取原始数据帧并扩展行数,以便每个因子组合都有“计数”行数?(我很确定这一点以前已经被问过并回答过。)@42-您的解决方案是最好的。因为,as.data.frame(表(x))返回6*6*6=216行,这不是很直观。谢谢!这很有用。我想知道,我们如何在不添加“计数”的情况下执行此操作“原始数据集的变量。@user3897这表明我们所有回答者都误解了您的问题。您是否试图获取原始数据帧并扩展行数,以便每个因子组合都有“计数”行数?(我很确定这一点以前已经被问过并回答过。)@42-您的解决方案是最好的。因为,as.data.frame(表(x))返回6*6*6=216行,这不是很直观。谢谢!ftable函数就是我要找的。谢谢!ftable函数就是我要找的。