简体   繁体   English

如何使用字符串作为df $列调用(R)

[英]How to use a string as df$column call (R)

Lets say I have a large program where we call a certain variable many times in the code. 假设我有一个大型程序,我们在代码中多次调用某个变量。 This variable will change for different analysis. 该变量将根据不同的分析而变化。 At the beginning of the code I want to create a call where I only change the variable once. 在代码的开头我想创建一个调用,我只改变一次变量。 How can I use this to call a df column? 如何使用它来调用df列?

example

df <- stuff

traitofinterest <- color

later on in the code... how do I use this to call stuff$color? 稍后在代码中...我如何使用它来调用东西$ color?

stuff$traitofinterest no workey stuff$traitofinterest没有问题

edit: Yes, that works! 编辑:是的,这有效! I am not sure why I overlooked this basic usage.. Im still relatively new to R. 我不确定为什么我忽略了这个基本用法..我还是比较新的R.

The "yes workey" solution would be to use something like: “是的workey”解决方案是使用类似的东西:

stuff[, traitofinterest]

or 要么

stuff[, traitofinterest, drop = FALSE]

depending on the desired output structure you want when there is only one variable in "traitofinterest" (which it sounds like there will be in general). 取决于你想要的“traitofinterest”中只有一个变量所需的输出结构(听起来通常会有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM