Giuseppe Petri Giuseppe Petri - 1 year ago 76
R Question

How to extract values from an object in R

I have recorded values at different moments and had a linear interpolation done within those points using the approx() function. I got the predicted values in the "model 1" object (please see the script below), but I couldn't find out how to extract those x,y predicted values from the object. Any help will be really appreciated.

Here is the script.

x <- c(7,56,64,71,84,92,98,106,118,140,148) # observed data
y <-c(0,1,1,1,1,1,1,1,0.891038658,0.727258549,0) # observed data

par(mfrow = c(1,1))
plot(x, y, main = "approx(.)")
points(approx(x, y, method = "linear"), col = 4, pch = "*")
Model1<-approx(x, y, method="linear", xout=7:148, yleft=0, yright=0, rule = 1, f = 0, ties = mean)

Thanks in advance,


Answer Source

approx is a simple function without complicated returns. It gives a list with two variables: x (taken from xout in approx) and y (interpolated values).

You can use Model1$x and Model1$y to access two vectors. If you want a data frame, as @eipi10 says, do:

which uses "list" method of generic function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download