本文介绍了使用与列名称相同的变量对数据框进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框架,我试图运行一个子集。在我的数据框中,我有一个名为start的列,我试图这样做: sub <子集(数据,数据$ start == 14)
我正确地获取了所有行的子集其中start = 14。
但是,当我这样做:
for(start seq(1,20,by = 1)){
sub< - subset(data,data $ start == start)
print(sub)
}
它没有正确找到子集。它只是打印整个数据框架。
为什么这样,我该如何解决?
解决方案
您还可以指定您正在使用的环境:
x< -data.frame (
start = sample(3,20,replace = TRUE),
someValue = runif(20))
env< -environment()
start< 3
cat(\\\
Defaut scope:)
print(subset(x,start == start))#所有条目,如start == start被评估为TRUE
cat(\\\
Specific environment:)
print(subset,x,start == get('start',env)))第二个启动被替换为前一个环境中的值。相当于子集(x,start == 3)
I have a data frame and I'm trying to run a subset on it. In my data frame, I have a column called "start" and I'm trying to do this:
sub <- subset(data,data$start==14)
and I correctly get a subset of all the rows where start=14.
But, when I do this:
for(start in seq(1,20,by=1)) {
sub <- subset(data,data$start==start)
print(sub)
}
it does not correctly find the subsets. It just prints the entire data frame.
Why is this and how do I fix it?
解决方案
You can also specify the environment you're working with:
x<-data.frame(
start=sample(3,20,replace=TRUE),
someValue=runif(20))
env<-environment()
start<-3
cat("\nDefaut scope:")
print(subset(x,start==start)) # all entries, as start==start is evaluated to TRUE
cat("\nSpecific environment:")
print(subset(x,start==get('start',env))) # second start is replaced by its value in former environment. Equivalent to subset(x,start==3)
这篇关于使用与列名称相同的变量对数据框进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!