R数据框按变量中的列名过滤

问题描述

注意事项:我对 R 的经验太少了。

我需要从数据框中提取所有列,按值为 2 的列“id”过滤,但在变量中包含列“id”,而不是硬编码。

有几个相关的问题,但我无法从中推断出解决方案:

How to use a string variable to select a data frame column using $ notation

access data frame column using variable

我的代码

df            <- data.frame(id  = c(1,2,3),col_a=c("A1","A2","A3"),col_b=c("B1","B2","B3"))
pk_single_col <- "id"
#how do i translate this instruction NOT to use $id,but the variable pk_single_col?
spameggs      <- df_a[df_a$id %in% 2,] 

在此先感谢您!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)