2

How to divide a data frame into a list of data frames that have given column nam...

 3 years ago
source link: https://www.codesd.com/item/how-to-divide-a-data-frame-into-a-list-of-data-frames-that-have-given-column-names.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to divide a data frame into a list of data frames that have given column names?

advertisements

I would like to build a function, something like splitdf(columnA + columnB ~ ., dataframe)

So that it would return a list of sub data frames for each possible pair of columnA and columnB. It is similar to table or xtabs

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. 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


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK