简体   繁体   English

如何在R中绘制数据帧的子集?

[英]How to plot a subset of a data frame in R?

Is there a simple way to do this in R: 在R中有一个简单的方法吗?

plot(var1,var2, for all observations in the data frame where var3 < 155)

It is possible by creating a new data newdata <- data[which( data$var3 < 155),] but then I have to redefine all the variables newvar1 <- newdata$var1 etc. 可以通过创建新数据newdata <- data[which( data$var3 < 155),]然后我必须重新定义所有变量newvar1 <- newdata$var1等。

with(dfr[dfr$var3 < 155,], plot(var1, var2)) should do the trick. with(dfr[dfr$var3 < 155,], plot(var1, var2))应该可以解决问题。

Edit regarding multiple conditions: 编辑多个条件:

with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2))

Most straightforward option: 最简单的选择:

plot(var1[var3<155],var2[var3<155])

It does not look good because of code redundancy, but is ok for fast n dirty hacking. 它看起来并不好,因为代码的冗余,但确定快速n肮脏的黑客。

This is how I would do it, in order to get in the var4 restriction: 这就是我要做的,为了获得var4限制:

dfr<-data.frame(var1=rnorm(100), var2=rnorm(100), var3=rnorm(100, 160, 10), var4=rnorm(100, 27, 6))
plot( subset( dfr, var3 < 155 & var4 > 27, select = c( var1, var2 ) ) )

Rgds, Rainer Rgds,Rainer

This chunk should do the work: 这个块应该做的工作:

plot(var2 ~ var1, data=subset(dataframe, var3 < 150))

My best regards. 我最诚挚的问候。

How this works: 这是如何工作的:

  1. Fisrt, we make selection using the subset function. Fisrt,我们使用子集函数进行选择。 Other possibilities can be used, like, subset(dataframe, var4 =="some" & var5 > 10). 可以使用其他可能性,例如,子集(dataframe,var4 ==“some”&var5> 10)。 The " & " operator can be used to select all "some" and over 10. Also the operator " | " could be used to select "some" or "over 10". ”运算符可用于选择所有“some” 10以上。此外,运算符“ | ”可用于选择“some” “over 10”。
  2. The next step is to plot the results of the subset, using tilde (~) operator, that just imply a formula, in this case var.response ~ var.independet. 下一步是使用波形符(〜)运算符绘制子集的结果,这只是暗示一个公式,在本例中为var.response~var.independet。 Of course this is not a formula, but works great for this case. 当然这不是一个公式,但对于这种情况很有用。

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

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