本文介绍了如何基于多个条件提取数据表的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何基于多个条件提取data.table的值?

How extract values of a data.table based on multiple conditions?

我需要一个基于其他两个列返回一列data.table值的函数值:

I need a function that returns a value of a column data.table based on two other column values:

require(data.table)

dt <- data.table(
    "base" = c("of", "of", "of", "lead and background vocals", "save thou me from", "silent in the face"),
    "prediction" = c("the", "set", "course", "from", "the", "of"),
    "count" = c(258586, 246646, 137533, 4, 4, 4)
)

> dt
#                         base prediction  count
#1:                         of        the 258586
#2:                         of        set 246646
#3:                         of     course 137533
#4: lead and background vocals       from      4
#5:          save thou me from        the      4
#6:         silent in the face         of      4

# the function needs to return the "prediction" value based on the max "count" value for the input "base" value.
# giving the input "of" to function:
> prediction("of")
# the desired output is:
> "the"
# or:
> prediction("save thou me from")
> "the"


推荐答案

我们可以指定 i ,基于计数中的 max 值提取预测值

We can specify the i, extract the 'prediction' value based on the max value in 'count'

dt[base == 'of', prediction[which.max(count)]]
#[1] "the"
dt[base == 'save thou me from', prediction[which.max(count)]]
#[1] "the"

它可以包装成一个函数

f1 <- function(val) dt[base == val, prediction[which.max(count)]]
f1("of")
#[1] "the"
f1("save thou me from")
#[1] "the"

注意:最好具有数据集标识符,并且列名也作为参数

NOTE: It is better to have dataset identifier, and column names also as arguments

这篇关于如何基于多个条件提取数据表的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:17