How to plot a subset of a data frame in R?

前端 未结 4 2124
温柔的废话
温柔的废话 2020-12-13 05:48

Is there a simple way to do this in R:

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

It is possible by creat

相关标签:
4条回答
  • 2020-12-13 06:04

    with(dfr[dfr$var3 < 155,], plot(var1, var2)) should do the trick.

    Edit regarding multiple conditions:

    with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2))
    
    0 讨论(0)
  • 2020-12-13 06:10

    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. Other possibilities can be used, like, subset(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".
    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. Of course this is not a formula, but works great for this case.
    0 讨论(0)
  • 2020-12-13 06:20

    This is how I would do it, in order to get in the var4 restriction:

    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

    0 讨论(0)
  • 2020-12-13 06:21

    Most straightforward option:

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

    It does not look good because of code redundancy, but is ok for fastndirty hacking.

    0 讨论(0)
提交回复
热议问题