

How to divide a data frame into a list of data frames that have given column nam...
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.

How to divide a data frame into a list of data frames that have given column names?
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
Recommend
-
7
Sub-tracing data frames in R advertisements I'm new to R and learning about subsetting. I have a table and I'm trying to get the size of a sub...
-
10
Create a pandas data frame with the date index and the random values in the column advertisements How do I create a pandas dataframe with da...
-
5
Perl: divide the pages of each file into two advertisements I have written a code which should do the following:
-
13
How to use trigger in mysql to update a column value from a given date to date only for a unique client identifier advertisements ...
-
6
Divide a large data image into smaller segments advertisements I have the following data frame and I want to break it up in...
-
9
Merging disparate data frames advertisements So this issue involves merging two data frames of disparate length.. ex...
-
4
How to Split a GIF Into Individual Frames: 5 Ways By Joy Okumoko Published 17 hours ago Splitting a GIF into ind...
-
6
How to Concatenate Data Frames in Pandas (Fast and Easy) This article is part of in the series Published: Sunday 20...
-
5
Column name from a given column numberOctober 04, 2023 |1.7K ViewsPROBLEM OF THE DAY: 03/10/2023 | Column name from a given column numberProblem of the Day, String, Data Structure and Algorith...
-
8
_BAD_'s blog Divide n people into ce...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK