用= =数据表的每一行 [英] Use by = each row for data table

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

问题描述

我有一个数据表,我试图创建一个新变量,该变量是所有其他列的函数。一个简化的示例是,如果我只是想对所有行求和或取平均值。例如:

I have a data table and I am trying to create a new variable that is a function of all the other columns. A simplified example would be if I simply wanted to sum or take an average across all the rows. For example:

dt <- data.table(a = 1:9, b = seq(10,90,10), c = seq(11:19), d = seq(100, 900, 100))

我想创建一个向量/列,该向量/列只是所有列的平均值。我想到的语法看起来像这样:

I want to create a vector/column that is simply the average of all the columns. The syntax that I think of would look something like this:

dt[, average := mean(.SD)]

但是,这是整个过程的总和。我知道我也可以这样做:

However, this sums the whole thing. I know I can also do:

dt[, average := lapply(.SD, mean)] 

但这给出了单行结果。我本质上是在寻找等效项:

But this gives a single row result. I'm essentially looking for the equivalent of:

dt[, average := lapply(.SD, mean), by = all]

这样,它只需为所有行计算该值,而无需创建 id专栏,然后按该专栏进行所有计算。这可能吗?

such that it simply calculates this for all the rows, without having to create an "id" column and doing all of my calculating by that column. Is this possible?

推荐答案

以下data.table代码对我有用。

The following data.table code worked for me.

 dt[, average := rowMeans(.SD)]

正如@jangorecki指出的那样,只要记住每一行都是一个列表对象,就可以构造自己的函数以按行运行:

As pointed out by @jangorecki, it is possible to construct your own function to run by row as long as you remember that each row is a list object:

# my function, must unlist the argument
myMean <- function(i, ...) mean(unlist(i), ...)

使用 by = seq_len

dt[, averageNew := myMean(.SD), by = seq_len(nrow(dt))]

使用 row.names

dt[, averageOther := myMean(.SD), by = row.names(dt)]

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

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