3

I would like to turn data.frame like this one:

dat = data.frame (
    ConditionA = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
    ConditionB = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5),
    X = c(460, 382, 468, 618, 421, 518, 655, 656, 621, 552, 750, 725, 337, 328, 342, 549, 569, 523, 469, 429),
    Y = c(437, 305, 498, 620, 381, 543, 214, 181, 183, 387, 439, 351, 327, 268, 276, 178, 375, 393, 312, 302)
)

into a list of lists like this (or similar):

lst = list(
    list(
        c(460, 382, 468, 618),
        c(437, 305, 498, 620)
    ),
    list(
        c(421, 518, 655, 656, 621),
        c(381, 543, 214, 181, 183)
    ),
    list(
        c(552, 750, 725),
        c(387, 439, 351)
    ),
    list(
        c(337, 328, 342, 549),
        c(327, 268, 276, 178)
    ),
    list(
        c(569, 523, 469, 429),
        c(375, 393, 312, 302)
    )
)

> lst
[[1]]
[[1]][[1]]
[1] 460 382 468 618

[[1]][[2]]
[1] 437 305 498 620


[[2]]
[[2]][[1]]
[1] 421 518 655 656 621

[[2]][[2]]
[1] 381 543 214 181 183


[[3]]
[[3]][[1]]
[1] 552 750 725

[[3]][[2]]
[1] 387 439 351

. . .

What would be the most efficient way to make such a conversion?

2
  • 3
    Why do you have two conditionA? Assuming you only have one of them: lapply(split(dat[, c("X", "Y")], dat$ConditionA), as.list) Commented Jun 10, 2018 at 20:12
  • @Alexis I haven't seen your comment when I posted the answer. Commented Jun 10, 2018 at 20:29

3 Answers 3

4

We can do a split based on the 1st and 2nd columns, use drop=TRUE for removing the combinations with 0 elements and convert to list

lapply(split(dat[-(1:2)], dat[1:2], drop = TRUE), as.list)

Or using tidyverse

library(tidyverse)
dat %>% 
    group_by(ConditionA, ConditionA.1) %>% 
    nest %>%
    mutate(data = map(data, as.list)) %>%
    pull(data) 
Sign up to request clarification or add additional context in comments.

Comments

2

May be this using data.table

Data:

dat = data.frame (
  ConditionA = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
  ConditionB = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5),
  X = c(460, 382, 468, 618, 421, 518, 655, 656, 621, 552, 750, 725, 337, 328, 342, 549, 569, 523, 469, 429),
  Y = c(437, 305, 498, 620, 381, 543, 214, 181, 183, 387, 439, 351, 327, 268, 276, 178, 375, 393, 312, 302)
)

Code:

library('data.table')
setDT(dat)
dat[, list(list(as.list(.SD))),by = .(ConditionA, ConditionB)][, V1]

or this

dat[, list(list(list(.SD))),by = .(ConditionA, ConditionB)][, V1]

Comments

2
c(by(dat[3:4],dat[1:2],as.list))
[[1]]
[[1]]$X
[1] 460 382 468 618

[[1]]$Y
[1] 437 305 498 620


[[2]]
[[2]]$X
[1] 421 518 655 656 621

[[2]]$Y
[1] 381 543 214 181 183


[[3]]
[[3]]$X
[1] 552 750 725

[[3]]$Y
[1] 387 439 351

 . . . . 

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.