基于变量列名的子集 [英] Subset based on variable column name

查看:21
本文介绍了基于变量列名的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不知道要测试的列的名称,我想知道如何使用 subset 函数.场景是这样的:我有一个 Shiny 应用程序,用户可以在其中选择一个变量来过滤(子集)数据表.我从 webapp 接收列名作为输入,我想根据该列的值进行子集化,如下所示:

I'm wondering how to use the subset function if I don't know the name of the column I want to test. The scenario is this: I have a Shiny app where the user can pick a variable on which to filter (subset) the data table. I receive the column name from the webapp as input, and I want to subset based on the value of that column, like so:

subset(myData, THECOLUMN == someValue)

除了 THECOLUMNsomeValue 都是变量.是否有将所需列名作为字符串传递的语法?

Except where both THECOLUMN and someValue are variables. Is there a syntax for passing the desired column name as a string?

似乎想要一个作为列名的裸字,而不是一个包含列名的变量.

Seems to want a bareword that is the column name, not a variable that holds the column name.

推荐答案

这就是为什么 subset 不是交互式使用的糟糕工具的原因:

This is precisely why subset is a bad tool for anything other than interactive use:

d <- data.frame(x = letters[1:5],y = runif(5))
> d[d[,'x'] == 'c',]
  x         y
3 c 0.3080524

从根本上说,在 R 中提取事物是围绕 [ 构建的.使用它.

Fundamentally, extracting things in R is built around [. Use it.

这篇关于基于变量列名的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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