如何将文本和变量粘贴到 R 中的逻辑表达式中?
我想将变量粘贴到用于子集数据的逻辑表达式中,但粘贴时子集函数不会将它们视为列名称(无论是带 ot 还是不带引号)。
我有一个数据框,其中的列名为 col1、col2 等。我想对 colx < 的行进行子集化。 0.05
这行得通:
subsetdata<-subset(dataframe, col1<0.05)
subsetdata<-subset(dataframe, col2<0.05)
这行不通:
for (k in 1:2){
subsetdata<-subset(dataframe, paste("col",k,sep="")<0.05)
}
for (k in 1:2){
subsetdata<-subset(dataframe, noquote(paste("col",k,sep=""))<0.05)
}
我找不到答案;有什么建议吗?
I want to paste variables in the logical expression that I am using to subset data, but the subset function does not see them as column names when pasted (either with ot without quotes).
I have a dataframe with columns named col1, col2 etc. I want to subset for the rows in which colx < 0.05
This DOES work:
subsetdata<-subset(dataframe, col1<0.05)
subsetdata<-subset(dataframe, col2<0.05)
This does NOT work:
for (k in 1:2){
subsetdata<-subset(dataframe, paste("col",k,sep="")<0.05)
}
for (k in 1:2){
subsetdata<-subset(dataframe, noquote(paste("col",k,sep=""))<0.05)
}
I can't find the answer; any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
子集
会使这件事变得比实际需要的困难得多。请注意,?subset
表示第二个参数(也称为subset)必须是一个表达式,而您没有给它一个表达式:您可以构造一个未计算的表达式,然后在将其传递给
时对其进行计算>子集
,但还有更简单的方法。例如:只需利用<
运算符的矢量化性质。You're making this a lot harder than it needs to be by trying to use
subset
. Note that?subset
says the second argument (also named subset) must be an expression and you're not giving it an expression:You could construct an unevaluated expression then evaluate it as you pass it to
subset
, but there are much easier ways. For example: just take advantage of the vectorized nature of the<
operator.以下是一些更接近 Jasper 方法的选项。首先,您可以将列名称定义为单独的变量,然后使用它从
data.frame
中选择变量,就好像它是一个list
(因为>data.frame
基本上是一个list
):或者您可以这样引用列名称:
最后,您可以使用
subset
,尽管您需要提供一个逻辑表达式(正如 Joshua Ulrich 所指出的):Here are a couple of options that are closer to the Jasper's approach. First, you could define the column name as a separate variable and then use it to select the variable from the
data.frame
as if it were alist
(since adata.frame
is basically alist
):Or you could refer to the column name as such:
Finally, you could use
subset
, although you need to provide a logical expression (as pointed out by Joshua Ulrich):我不太清楚您要做什么,但也许看到
&
和|
在subset
操作中使用会有所帮助。col1
和col2
都小于 0.05:col1
或col2
小于 0.05:Joshua 的答案是一个很好的方法在许多列上更容易地做到这一点。
It's not quite clear to me what you're trying to do but perhaps seeing
&
and|
used in asubset
operation would be helpful.Both
col1
andcol2
less than 0.05:Either
col1
orcol2
less than 0.05:Joshua's answer is a great way of doing this more easily over many columns.