R lapply更新带有列表的data.tables列表 - 在级别1没有这样的索引

时间:2018-05-19 21:05:06

标签: r list data.table lapply

我正在尝试使用列表更新date.tables列表,看起来它应该像在此示例中一样工作:

#include "stdafx.h"
#include<iostream>
using namespace std;

class Snode
{
public:
    char data;
    int count;
    Snode *next;
    Snode(char d, int c)
    {
        data = d;
        count = c;
        next = NULL;
    }
};

class set
{
private:
    Snode *head;
public:
    set()
    {
        head = NULL;
        tail = NULL;
    }
    ~set();
    void insert(char value);
    bool isAvailable(char value);
};

set::~set()
{
    Snode *t = head;
    while (t != NULL)
    {
        head = head->next;
        delete t;
    }
}

bool set::isAvailable(char value)
{
    Snode *floatingNode = new Snode(char d, int c);
    while(floatingNode != NULL)
    {
        return (value == floatingNode);
        floatingNode->next = floatingNode;
    }
}

void set::insert(char value)
{
    Snode *newNode = new Snode(char d, int c);
    data = value;
    if (head == NULL)
    {
        newNode->next = NULL;
        head = newNode;
        newNode->count++;
    }
    else
    {
        if(isAvailable)
        {
            //IDK what should i do here +_+
        }
        else
        {
            tail->next= newNode;
            newNode->next = NULL;
            tail = newNode;
        }
    }
}

现在有一个更新列表(有人说dt1是第1组,dt2是第2组,组需要在结果中):

group1&lt; - list(1,2)

lapply update:

set.seed(1965)
dt_lst <- list(dt1 <- data.table(a = rnorm(1:4),
b = c(4,3,2,1)), dt2 <- data.table(c = rnorm(1:5),
d = letters[1:5]))

> dt_lst
[[1]]
        a b
1:  0.8428429 4
2:  0.2958355 3
3: -1.0520980 2
4:  0.9628192 1

[[2]]
         c d
1: -0.05033855 a
2: -0.94065157 b
3:  1.20459624 c
4: -0.47791557 d
5: -0.30362496 e

我的数据的完美和特征,我从来不知道数据有多大。我可以拥有(nrows),也不知道它应该属于哪个'group' 直到事后,因此更新。

现在,我的数据量非常少:

dt_lst_tst <- lapply(seq_along(dt_lst),
function(x)
dt_lst[[x]][, group:= group1[[x]]])

> dt_lst_tst
[[1]]
        a b group
1:  0.8428429 4     1
2:  0.2958355 3     1
3: -1.0520980 2     1
4:  0.9628192 1     1

[[2]]
         c d group
1: -0.05033855 a     2
2: -0.94065157 b     2
3:  1.20459624 c     2
4: -0.47791557 d     2
5: -0.30362496 e     2

使用与上面类似的lapply:

> dput(combine_sub1)
list(structure(list(smp = 1:4, x = c(491, 491, 491, 491), y = c(798, 
798, 798, 798)), .Names = c("smp", "x", "y"), class = c("data.table", 
"data.frame"), row.names = c(NA, -4L), .internal.selfref = <pointer: 
0x2b859d8>), 
structure(list(smp = 1:6, x = c(650, 650, 650, 650, 650, 
650), y = c(437, 437, 437, 437, 437, 437)), .Names = c("smp", 
"x", "y"), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x2b859d8>), structure(list(
    smp = 1:5, x = c(480, 485, 540, 572, 589), y = c(462, 
    462, 455, 451, 450)), .Names = c("smp", "x", "y"), class = 
c("data.table", 
"data.frame"), row.names = c(NA, -5L), .internal.selfref = <pointer: 
0x2b859d8>))

> combine_sub1
[[1]]
   smp   x   y
1:   1 491 798
2:   2 491 798
3:   3 491 798
4:   4 491 798

[[2]]
   smp   x   y
1:   1 650 437
2:   2 650 437
3:   3 650 437
4:   4 650 437
5:   5 650 437
6:   6 650 437

[[3]]
   smp   x   y
1:   1 480 462
2:   2 485 462
3:   3 540 455
4:   4 572 451
5:   5 589 450

group3_lst <- list(1,2,3)

> group3_lst
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

我无法弄清楚为什么会有这种差异。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

问题似乎是由函数调用中使用变量x引起的,它会干扰x中data.tables中的group3_lst列。使用不在这些data.tables中的差异变量名称,它将正常工作,例如使用icombine_sub1_tst <- lapply(seq_along(combine_sub1), function(i) combine_sub1[[i]][ , group := group3_lst[[i]]])