как эффективно поместить подкадр данных в список списков, используя два правила - PullRequest
0 голосов
/ 26 января 2019

Буду признателен за любую помощь для эффективного разбиения фрейма данных на несколько фрагментов, которые будут переданы в список списков на основе imput и Weights_x, где x = {1, 2, ..., 10}.

Этот вопрос и моя попытка основаны на этом и этом . Разница в том, что теперь я хочу создать список списков, где каждый из списков имеет только одно значение imput и один столбец переменных Weights_x.

Мой код ниже, расширенный из @DarrenTsai, работает для нескольких столбцов Weights_x, но у меня есть подмножества 100 imput * 1000 weights для создания. Поэтому мне нужен более эффективный подход, который позволяет достичь того же результата без большого количества кода.

мои данные:

dat <- structure(list(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 
3, 3, 4, 4, 4, 4, 4), imput = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 
1, 2, 3, 4, 5, 1, 2, 3, 4, 5), A = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), B = c(1, 1, 1, 1, 1, 0, 
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0), Pass = c(278, 278, 
278, 278, 278, 100, 100, 100, 100, 100, 153, 153, 153, 153, 153, 
79, 79, 79, 79, 79), Fail = c(740, 743, 742, 743, 740, 7581, 
7581, 7581, 7581, 7581, 1231, 1232, 1235, 1235, 1232, 1731, 1732, 
1731, 1731, 1731), Weights_1 = c(4, 3, 4, 3, 3, 1, 2, 1, 2, 1, 
12, 12, 11, 12, 12, 3, 5, 3, 3, 3), Weights_2 = c(3, 3, 3, 3, 
3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3), Weights_3 = c(4, 
3, 3, 3, 3, 1, 2, 1, 1, 1, 12, 12, 11, 12, 12, 3, 3, 3, 3, 3), 
    Weights_4 = c(3, 3, 4, 3, 3, 1, 1, 1, 2, 1, 12, 12, 13, 12, 
    12, 3, 2, 3, 3, 3), Weights_5 = c(3, 3, 3, 3, 3, 1, 0, 1, 
    1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 3), Weights_6 = c(4, 
    3, 3, 3, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 3, 3, 3, 3, 
    3), Weights_7 = c(3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 12, 12, 12, 
    12, 12, 3, 3, 3, 3, 3), Weights_8 = c(3, 3, 3, 3, 3, 1, 1, 
    1, 1, 1, 15, 12, 12, 12, 12, 3, 3, 3, 3, 3), Weights_9 = c(3, 
    3, 3, 4, 3, 1, 1, 1, 1, 1, 12, 12, 12, 12, 12, 2, 3, 3, 3, 
    3), Weights_10 = c(3, 3, 4, 3, 3, 1, 1, 1, 1, 1, 12, 10, 
    12, 12, 12, 3, 3, 3, 3, 3)), class = "data.frame", row.names = c(NA, 
-20L))

мой подход:

##Weights = `Weights_1`
myvars_1 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_1")
dat_1 <- dat[myvars_1]


mylist_1 <- by(dat_1, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_1
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_2`
myvars_2 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_2")
dat_2 <- dat[myvars_2]


mylist_2 <- by(dat_2, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_2
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_3`
myvars_3 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_3")
dat_3 <- dat[myvars_3]


mylist_3 <- by(dat_3, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_3
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_4`
myvars_4 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_4")
dat_4 <- dat[myvars_4]


mylist_4 <- by(dat_4, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_4
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_5`
myvars_5 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_5")
dat_5 <- dat[myvars_5]


mylist_5 <- by(dat_5, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_5
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})


##Weights = `Weights_6`
myvars_6 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_6")
dat_6 <- dat[myvars_6]


mylist_6 <- by(dat_6, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_6
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})


##Weights = `Weights_7`
myvars_7 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_7")
dat_7 <- dat[myvars_7]


mylist_7 <- by(dat_7, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_7
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_8`
myvars_8 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_8")
dat_8 <- dat[myvars_8]


mylist_8 <- by(dat_8, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_8
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_9`
myvars_9 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_9")
dat_9 <- dat[myvars_9]


mylist_9 <- by(dat_9, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_9
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##Weights = `Weights_10`
myvars_10 <- c("id", "imput", "A", "B", "Pass", "Fail", "Weights_10")
dat_10 <- dat[myvars_10]


mylist_10 <- by(dat_10, dat$imput, function(x){
  nn <- x$Fail + x$Pass
  weights <- x$Weights_10
  return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
              id = x$id, P = x$imput, nn = nn, weights = weights))
})

##create list of lists
mylistslist <- list(mylist_1, mylist_2, mylist_3, mylist_4, mylist_5, 
                    mylist_6, mylist_7, mylist_8, mylist_9, mylist_10)

Заранее спасибо за любую помощь.

1 Ответ

0 голосов
/ 26 января 2019

Использование lapply может быть одним решением

myvars <- c("id", "imput", "A", "B", "Pass", "Fail")
cols <- which(names(dat) %in% myvars)
ind <- grep('^Weights_\\d+$', names(dat))
out <- lapply(ind, function (k) {
  dat_1 <- dat[c(cols,k)]
  by(dat_1, dat$imput, function(x){
    nn <- x$Fail + x$Pass
    weights <- .subset2(x, ncol(x))
    return(list(N = nrow(x), ncases = x$Pass, A = x$A, B = x$B,
                id = x$id, P = x$imput, nn = nn, weights = weights))
  })
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...