将函数应用于data.table的每一行 [英] Applying a function to each row of a data.table

查看:134
本文介绍了将函数应用于data.table的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来有效地将一个函数应用到data.table的每一行。让我们考虑下面的数据表:

  library(data.table)
library(stringr)

x < - data.table(a = c(1:3,1),b = c('12 13','14 15','16 17','18 19'))
> x
ab
1:1 12 13
2:2 14 15
3:3 16 17
4:1 18 19
b
的每个元素(因此产生两个原始数据中的每行的行),并连接结果数据表。对于上面的示例,我需要以下结果:

  a V1 
1:1 12
2 :1 13
3:2 14
4:2 15
5:3 16
6:3 17
7:1 18
8:1 19

如果 a 只有唯一值:

  x [,list(str_split(b, 1]]),by = a] 

除非在原始数据表中有一些相同的行),但是当 x 有很多列和复制列b到结果,这是我想避免的。 / p>

 > x [,list(str_split(b,'')[[1]],by = list(a,b)] 
ab V1
1:1 12 13 12
2:1 12 13 13
3:2 14 15 14
4:2 14 15 15
5:3 16 17 16
6:3 16 17 17
7:1 18 19 18
8:1 18 19 19

什么是最有效率和惯用的

如何:



<$ p $

p x
ab
1:1 12 13
2:2 14 15
3:3 16 17
4:1 18 19

x [,list(a = rep(a,each = 2),V1 = unlist(strsplit(b,)))
a V1
1:1 12
2:1 13
3:2 14
4:2 15
5:3 16
6:3 17
7:1 18
8:1 19

给出注释的广义解决方案:


$ b b

  x [,{s = strsplit(b,); list(a = rep(a,sapply(s,length)),V1 = unlist }] 


I looking for a way to efficiently apply a function to each row of data.table. Let's consider the following data table:

library(data.table)
library(stringr)

x <- data.table(a = c(1:3, 1), b = c('12 13', '14 15', '16 17', '18 19'))
> x
   a     b
1: 1 12 13
2: 2 14 15
3: 3 16 17
4: 1 18 19

Let's say I want to split each element of column b by space (thus yielding two rows for each row in the original data) and join the resulting data tables. For the example above, I need the following result:

   a V1
1: 1 12
2: 1 13
3: 2 14
4: 2 15
5: 3 16
6: 3 17
7: 1 18
8: 1 19

The following would work if column a has only unique values:

x[, list(str_split(b, ' ')[[1]]), by = a]

The following almost works (unless there are some identical rows in the original data table), but is ugly when x has many columns and copies column b to the result, which I would like to avoid.

>     x[, list(str_split(b, ' ')[[1]]), by = list(a,b)]
   a     b V1
1: 1 12 13 12
2: 1 12 13 13
3: 2 14 15 14
4: 2 14 15 15
5: 3 16 17 16
6: 3 16 17 17
7: 1 18 19 18
8: 1 18 19 19

What would be the most efficient and idiomatic way to solve this problem?

解决方案

How about :

x
   a     b
1: 1 12 13
2: 2 14 15
3: 3 16 17
4: 1 18 19

x[,list(a=rep(a,each=2), V1=unlist(strsplit(b," ")))]
   a V1
1: 1 12
2: 1 13
3: 2 14
4: 2 15
5: 3 16
6: 3 17
7: 1 18
8: 1 19

Generalized solution given comment :

x[,{s=strsplit(b," ");list(a=rep(a,sapply(s,length)), V1=unlist(s))}]

这篇关于将函数应用于data.table的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆