Graphing a piece-wise defined function in RKward

Vojtěch Zeisek vojtech.zeisek at opensuse.org
Mon Dec 19 15:06:34 UTC 2016


Dne pondělí 19. prosince 2016 9:26:09 CET, Philip (Michael) Dykes napsal(a):
> I am very new to both R and Rkward and am trying to plot the following:
> 
> f(x)=    x^2, if x <= 2,
>              6-x, if x > 2.
> 
> How can I do this in Rkward? Also, I would like the graph (plot) to look
> as much as possible like the graph I was in this book (attached).

I'd start with something like

x <- seq(from=-3, to=6.5, by=0.1)

for (i in 1:length(x)) {
  if(x[i] <= 2) {
    y[i] <- x[i]^2
    } else if(x[i] > 2) {
      y[i] <- 6-x[i]
      }
  }

plot(x, y)

There are plenty of possibilities to tune plot look and feel.

-- 
Vojtěch Zeisek

Komunita openSUSE GNU/Linuxu
Community of the openSUSE GNU/Linux

https://www.opensuse.org/
https://trapa.cz/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/rkward-users/attachments/20161219/173bac1b/attachment.sig>


More information about the rkward-users mailing list