2

Consider the following dataframe:

test.df <- data.frame(a = c("1991-01-01","1991-01-01","1991-02-01","1991-02-01"), b = rnorm(4), c = rnorm(4))

I would like to create a list from test.df. Each element of the list would be a subset dataframe of test.df corresponding to a specific value of column a, i.e. each date. In other words, in this case, column a takes unique values 1991-01-01 and 1991-02-01. Therefore, the resulting list would be comprised of two elements: the subset of test.df when a = 1991-01-01 (excluding column a), and the other element of the list would be the subset of test.df when 1991-02-01 = 2 (excluding column a).

Here is the output I am looking for:

lst <- list(test.df[1:2,2:3], test.df[3:4,2:3]) 

Note that the subset dataframes may not have the same number of rows.

In my real practical example, column a is a date column with many more values.

What can I try next?

0

3 Answers 3

5

You can use split

lst <- split(test.df, test.df$a)

If you want to get rid of column a, use split(test.df[-1], test.df$a) (thanks to @akrun for comment).

Sign up to request clarification or add additional context in comments.

1 Comment

You could use split(test.df[,-1], test.df$a)
2

You can use the following code:

sapply(union(test.df$a,NULL), function(y,x) x[x$a==y,], x=test.df, simplify=FALSE)

Comments

1

You could also use the dlply function in the plyr package:

> library(plyr)

> dlply(test.df, .(a))
$`1991-01-01`
           a          b         c
1 1991-01-01  1.3658775 0.9805356
2 1991-01-01 -0.2292211 2.2812914

$`1991-02-01`
           a          b         c
1 1991-02-01 -0.2678131 0.5323250
2 1991-02-01  0.3736910 0.4988308

Or the data.table package:

> library(data.table)

> setDT(test.df)
> dt <- test.df[, list(list(.SD)), by = a]$V1
> names(dt) <- unique(test.df$a)

> dt
$`1991-01-01`
            b         c
1:  1.3658775 0.9805356
2: -0.2292211 2.2812914

$`1991-02-01`
            b         c
1: -0.2678131 0.5323250
2:  0.3736910 0.4988308

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.