Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用data.tables对多列进行有效的矢量化更新?_R_Dataframe_Data.table - Fatal编程技术网

R 如何使用data.tables对多列进行有效的矢量化更新?

R 如何使用data.tables对多列进行有效的矢量化更新?,r,dataframe,data.table,R,Dataframe,Data.table,我有以下使用data.frames的代码,我想知道如何使用data.tables,使用最高效、最矢量化的代码来编写它 data.frame代码: set.seed(1) to <- cbind(data.frame(time=seq(1:5),bananas=sample(100,5),apples=sample(100,5)),setNames(data.frame(matrix(sample(100,90,replace=T),nrow=5)),paste0(1:18))) from

我有以下使用data.frames的代码,我想知道如何使用data.tables,使用最高效、最矢量化的代码来编写它

data.frame代码:

set.seed(1)
to <- cbind(data.frame(time=seq(1:5),bananas=sample(100,5),apples=sample(100,5)),setNames(data.frame(matrix(sample(100,90,replace=T),nrow=5)),paste0(1:18)))
from <- cbind(data.frame(time=seq(1:5),blah=sample(100,5),foo=sample(100,5)),setNames(data.frame(matrix(sample(100,90,replace=T),nrow=5)),paste0(1:18)))
from
to

rownames(to) <- to$time
to[as.character(from$time),paste0(1:18)] <- from[,paste0(1:18)]
to
如果可能的话,我也更喜欢使用矢量化语法,而不是某种for循环,即尽量避免以下情况:

for( i in 1:18 ) {
    to[from, sprintf("%d",i) := i.sprintf("%d",i)]
}
我通读了faq小插曲和datatable简介小插曲,尽管我承认我可能还没有100%理解所有内容

我看了,但我不能说我100%理解它,它似乎说我需要使用for循环


在8374816的底部似乎有某种暗示,可能只是使用数据帧语法,添加
with=FALSE
?但是,由于data.frame过程正在对行名称进行黑客攻击,我不确定这是否有效,我想知道这在多大程度上利用了data.table的效率?

好问题。已显示的基本构造:

to[as.character(from$time),paste0(1:18)] <- from[,paste0(1:18)]

to[from,paste0(1:18):=from[,paste0(1:18),with=FALSE],mult="first"]
   time bananas apples  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2      37     94 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3      57     65 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4      89     62 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5      20      6 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79
注意:我使用的是最新的v1.8.3,这是选项1工作所必需的(
.GRP
刚刚添加,不再需要外部
with=FALSE

2。使用一个列表列存储长度为18的向量,而不是18列

to = data.table( time=seq(1:5),
                 bananas=sample(100,5),
                 apples=sample(100,5),  
                 v18=replicate(5,sample(100,18),simplify=FALSE))
from =  data.table( time=seq(1:5),
                    blah=sample(100,5),
                    foo=sample(100,5),
                    v18=replicate(5,sample(100,18),simplify=FALSE))
setkey(to,time)
setkey(from,time)

from
   time blah foo                 v18
1:    1   56  97   88,47,1,71,69,18,
2:    2   69  40   96,99,60,3,33,27,
3:    3   65  84 100,38,56,72,84,55,
4:    4   98  74 91,69,24,63,27,100,
5:    5   46  52    65,4,59,41,8,51,

to
   time bananas apples                 v18
1:    1      66     73 100,36,74,77,68,46,
2:    2      19     37   84,88,92,8,37,52,
3:    3      94     77   37,94,13,7,93,43,
4:    4      88      2  27,93,71,16,46,66,
5:    5      91     91   85,94,58,49,19,1,

to[from,v18:=i.v18]
to
   time bananas apples                 v18
1:    1      66     73   88,47,1,71,69,18,
2:    2      19     37   96,99,60,3,33,27,
3:    3      94     77 100,38,56,72,84,55,
4:    4      88      2 91,69,24,63,27,100,
5:    5      91     91    65,4,59,41,8,51,
如果不用于列出列打印,则后面的逗号表示该向量中有更多项。只打印前6个

3。在
data.table上使用
data.frame
语法

to = as.data.table(to)
from = as.data.table(from)
setkey(to,time)
setkey(from,time)

from
   time blah foo  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1   66  22 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2   35  13 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3   27  47 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4   97  90 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5   61  58 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79

to
   time bananas apples  1   2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 21  50 94 39 49 67 83 79 48 10 92 26 34 90 44 21 24 80
2:    2      37     94 18  72 22  2 60 80 65  3 87 32 30 48 84 87 72 72  6 46
3:    3      57     65 69 100 66 39 50 11 79 48 44 52 46 77 35 39 40 13 65 42
4:    4      89     62 39  39 13 87 19 73 56 74 25 67 34  9 34 78 33 25 88 82
5:    5      20      6 77  78 27 35 83 42 53 70  8 41 66 88 48 97 76 15 78 61

to[from, paste0(1:18)] <- from[,paste0(1:18),with=FALSE]
to
   time bananas apples  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2      37     94 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3      57     65 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4      89     62 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5      20      6 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79
to=as.data.table(to)
from=as.data.table(from)
设置键(到,时间)
设置键(从,时间)
从…起
时间一二三四五六七八九一一一一二一四一五一一一七一八
1:    1   66  22 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2   35  13 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3   27  47 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4   97  90 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5   61  58 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79
到
时间香蕉苹果1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 18
1:    1      27     90 21  50 94 39 49 67 83 79 48 10 92 26 34 90 44 21 24 80
2:    2      37     94 18  72 22  2 60 80 65  3 87 32 30 48 84 87 72 72  6 46
3:    3      57     65 69 100 66 39 50 11 79 48 44 52 46 77 35 39 40 13 65 42
4:    4      89     62 39  39 13 87 19 73 56 74 25 67 34  9 34 78 33 25 88 82
5:    5      20      6 77  78 27 35 83 42 53 70  8 41 66 88 48 97 76 15 78 61

到[from,paste0(1:18)],您遇到的困难中至少有一半是使用数字作为列名。如果您改为在每一列前面加一个字母,使它们成为“有效”的列名,您会发现整个操作要容易得多。@Justin,我同意在列前面加字母前缀,如果这样做可以解决问题,否则将无法解决。
to[from,paste0(1:18):=from[,paste0(1:18),with=FALSE],mult="first"]
   time bananas apples  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2      37     94 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3      57     65 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4      89     62 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5      20      6 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79
to = data.table( time=seq(1:5),
                 bananas=sample(100,5),
                 apples=sample(100,5),  
                 v18=replicate(5,sample(100,18),simplify=FALSE))
from =  data.table( time=seq(1:5),
                    blah=sample(100,5),
                    foo=sample(100,5),
                    v18=replicate(5,sample(100,18),simplify=FALSE))
setkey(to,time)
setkey(from,time)

from
   time blah foo                 v18
1:    1   56  97   88,47,1,71,69,18,
2:    2   69  40   96,99,60,3,33,27,
3:    3   65  84 100,38,56,72,84,55,
4:    4   98  74 91,69,24,63,27,100,
5:    5   46  52    65,4,59,41,8,51,

to
   time bananas apples                 v18
1:    1      66     73 100,36,74,77,68,46,
2:    2      19     37   84,88,92,8,37,52,
3:    3      94     77   37,94,13,7,93,43,
4:    4      88      2  27,93,71,16,46,66,
5:    5      91     91   85,94,58,49,19,1,

to[from,v18:=i.v18]
to
   time bananas apples                 v18
1:    1      66     73   88,47,1,71,69,18,
2:    2      19     37   96,99,60,3,33,27,
3:    3      94     77 100,38,56,72,84,55,
4:    4      88      2 91,69,24,63,27,100,
5:    5      91     91    65,4,59,41,8,51,
to = as.data.table(to)
from = as.data.table(from)
setkey(to,time)
setkey(from,time)

from
   time blah foo  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1   66  22 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2   35  13 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3   27  47 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4   97  90 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5   61  58 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79

to
   time bananas apples  1   2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 21  50 94 39 49 67 83 79 48 10 92 26 34 90 44 21 24 80
2:    2      37     94 18  72 22  2 60 80 65  3 87 32 30 48 84 87 72 72  6 46
3:    3      57     65 69 100 66 39 50 11 79 48 44 52 46 77 35 39 40 13 65 42
4:    4      89     62 39  39 13 87 19 73 56 74 25 67 34  9 34 78 33 25 88 82
5:    5      20      6 77  78 27 35 83 42 53 70  8 41 66 88 48 97 76 15 78 61

to[from, paste0(1:18)] <- from[,paste0(1:18),with=FALSE]
to
   time bananas apples  1  2   3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18
1:    1      27     90 98  2 100 46 58 60 69 46 62 19 29 42 64 90 30 19 72 60
2:    2      37     94 74 72  50 52  8 57 61 18 56 53 90  7 85 65 20 76 39 12
3:    3      57     65 36 11  49 21  4 53 24 75 33  8 45 34 86 75 89 73 11 85
4:    4      89     62 44 45  18 23 65 99 26 11 46 28 78 73 40 61 51 95 93 32
5:    5      20      6 15 65  76 60 93 51 73 87 51 22 89 34 39 91 88 55 29 79