简体   繁体   English

如何将数据框拆分为给定列名的数据框列表?

[英]How to split a data frame into a list of data frame given column names?

I would like to build a function, something like splitdf(columnA + columnB ~ ., dataframe) 我想构建一个函数,比如splitdf(columnA + columnB ~ ., dataframe)

So that it would return a list of sub data frames for each possible pair of columnA and columnB . 这样它就会为每个可能的columnA and columnB对返回一个子数据帧列表。 It is similar to table or xtabs 它类似于tablextabs

So, for 因此对于

> a <- data.frame(x=1:3, y=1:3, z=1:9)
> a
  x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 1 1 4
5 2 2 5
6 3 3 6
7 1 1 7
8 2 2 8
9 3 3 9

I am expecting: 我期待:

x = 1, y = 1, 
      x y z
     1 1 1
     1 1 4
     1 1 7

x = 2, y = 2, 
      x y z
     2 2 2
     2 2 5
     2 2 8

x = 3, y = 3, 
      x y z
     3 3 3
     3 3 6
     3 3 9

Update 更新

I realized that split and dlply could work. 我意识到splitdlply可以工作。 However neither of them provide me with a good way to manipulate category names? 但是,它们都没有为我提供操作类别名称的好方法吗? How do I make these names to be meaningful? 如何让这些名字变得有意义? Like I would like to see x = 1, y = 1 instead of $ 1.1 就像我想看到x = 1, y = 1而不是$ 1.1

Is this what you want? 这是你想要的吗?

> library (plyr)
> dlply(a, .(x, y))
$`1.1`
  x y z
1 1 1 1
2 1 1 4
3 1 1 7

$`2.2`
  x y z
1 2 2 2
2 2 2 5
3 2 2 8

$`3.3`
  x y z
1 3 3 3
2 3 3 6
3 3 3 9

update 更新

> z <- dlply(a, .(x, y))
> names(z) <- dlply(a, .(x, y), function(x) sprintf("x = %d, y = %d", x$x[1], x$y[1]))
> z
$`x = 1, y = 1`
  x y z
1 1 1 1
2 1 1 4
3 1 1 7

$`x = 2, y = 2`
  x y z
1 2 2 2
2 2 2 5
3 2 2 8

$`x = 3, y = 3`
  x y z
1 3 3 3
2 3 3 6
3 3 3 9

Using base split function 使用基本split功能

a <- data.frame(x = 1:3, y = 1:3, z = 1:9)
split(a, f = list(a$x, a$y))
## $`1.1`
##   x y z
## 1 1 1 1
## 4 1 1 4
## 7 1 1 7
## 
## $`2.1`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`3.1`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`1.2`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`2.2`
##   x y z
## 2 2 2 2
## 5 2 2 5
## 8 2 2 8
## 
## $`3.2`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`1.3`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`2.3`
## [1] x y z
## <0 rows> (or 0-length row.names)
## 
## $`3.3`
##   x y z
## 3 3 3 3
## 6 3 3 6
## 9 3 3 9
## 

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

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