在搜索中,我发现了很多关于如何为列执行此操作的问题,而不是针对行的问题。
# 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
任何关于正确语法的建议都将非常感激。
我们可以使用get
head(data[get(varname) == 'setosa'])
或者eval(as.name
head(data[eval(as.name(varname)) == 'setosa'])