在data.table中按引用比较行

问题描述 投票:1回答:1

在搜索中,我发现了很多关于如何为列执行此操作的问题,而不是针对行的问题。

# Works just fine:
data = as.data.table(iris)
head(data[Species == 'setosa'])

# I want to do it programatically:
varname = 'Species'
head(data[varname == 'setosa']) # Doesn't work
head(data[(varname) == 'setosa']) # Doesn't work
head(data[(varname) == 'setosa', with = F]) # Doesn't work

任何关于正确语法的建议都将非常感激。

r data.table
1个回答
2
投票

我们可以使用get

head(data[get(varname) == 'setosa']) 

或者eval(as.name

head(data[eval(as.name(varname)) == 'setosa']) 
© www.soinside.com 2019 - 2024. All rights reserved.