I'm new to R and am trying to extract the factors of a dataframe using numeric indices (e.g. df[1]) that are input to a function definition instead of the other types of references (e.g. df$out). df[1] is a list(?) whose class is "dataframe". These indexed lists can be printed successfuly but are not agreeable to the plot() and lm() functions shown below as are their df$out references. Reading the documentation for plot and lm hasn't helped yet. Thanks in advance - Stan.> df=data.frame(out=1:4*3,pred1=1:4,pred2=1:4*2) > regression=function(tble,a,b)+ { + plot.new() + plot(tble[a]~tble[b]) + lmm=lm(tble[a]~tble[b]) + abline(lmm) + anova(lmm) + }> df[1]out 1 3 2 6 3 9 4 12> dfout pred1 pred2 1 3 1 2 2 6 2 4 3 9 3 6 4 12 4 8> regression(df,1,3)Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : invalid type (list) for variable 'tble[a]'>[[alternative HTML version deleted]]
This is a subset problem, not a problem with plot or lm. You need to
read eg help("[.data.frame")
Here are two working examples:
# df already means something!
mydf <- data.frame(out=1:4*3,pred1=1:4,pred2=1:4*2)
regression <- function(tble,a,b)
{
plot.new()
plot(tble[,a]~tble[,b])
lmm=lm(tble[,a]~tble[,b])
abline(lmm)
anova(lmm)
}
regression(mydf, 1, 3)
regression <- function(tble,a,b)
{
plot.new()
plot(tble[[a]]~tble[[b]])
lmm=lm(tble[[a]]~tble[[b]])
abline(lmm)
anova(lmm)
}
regression(mydf, 1, 3)
Sarah
On 6/4/07, Stan Hopkins <stanhopkins at comcast.net>
wrote:> I'm new to R and am trying to extract the factors of a dataframe using
numeric indices (e.g. df[1]) that are input to a function definition instead of
the other types of references (e.g. df$out). df[1] is a list(?) whose class is
"dataframe". These indexed lists can be printed successfuly but are
not agreeable to the plot() and lm() functions shown below as are their df$out
references. Reading the documentation for plot and lm hasn't helped yet.
Thanks in advance - Stan.
>
> > df=data.frame(out=1:4*3,pred1=1:4,pred2=1:4*2)
> > regression=function(tble,a,b)
> + {
> + plot.new()
> + plot(tble[a]~tble[b])
> + lmm=lm(tble[a]~tble[b])
> + abline(lmm)
> + anova(lmm)
> + }
> > df[1]
> out
> 1 3
> 2 6
> 3 9
> 4 12
> > df
> out pred1 pred2
> 1 3 1 2
> 2 6 2 4
> 3 9 3 6
> 4 12 4 8
> > regression(df,1,3)
> Error in model.frame(formula, rownames, variables, varnames, extras,
extranames, :
> invalid type (list) for variable 'tble[a]'
> >
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
On 6/4/07, Stan Hopkins <stanhopkins at comcast.net> wrote:> I'm new to R and am trying to extract the factors of a dataframe using numeric indices (e.g. df[1]) that are input to a function definition instead of the other types of references (e.g. df$out). df[1] is a list(?) whose class is "dataframe". These indexed lists can be printed successfuly but are not agreeable to the plot() and lm() functions shown below as are their df$out references. Reading the documentation for plot and lm hasn't helped yet. Thanks in advance - Stan.> > df=data.frame(out=1:4*3,pred1=1:4,pred2=1:4*2) > > regression=function(tble,a,b) > + { > + plot.new() > + plot(tble[a]~tble[b]) > + lmm=lm(tble[a]~tble[b]) > + abline(lmm) > + anova(lmm) > + } > > df[1] > out > 1 3 > 2 6 > 3 9 > 4 12but df[[1]] is the first column of df in its native form. I think of the distinction as like the difference between an element of a set, which is what the "[[" function returns, and the subset consisting of a single element, which is what the "[" function returns.
I think your indexing is wrong in the function. Note
the tble[a] rather than tble[,a].
Try:
regression=function(tble,a,b)
{
plot.new()
plot(tble[,a]~tble[,b])
lmm=lm(tble[,a]~tble[,b])
abline(lmm)
anova(lmm)
}
--- Stan Hopkins <stanhopkins at comcast.net> wrote:
> I'm new to R and am trying to extract the factors of
> a dataframe using numeric indices (e.g. df[1]) that
> are input to a function definition instead of the
> other types of references (e.g. df$out). df[1] is a
> list(?) whose class is "dataframe". These indexed
> lists can be printed successfuly but are not
> agreeable to the plot() and lm() functions shown
> below as are their df$out references. Reading the
> documentation for plot and lm hasn't helped yet.
> Thanks in advance - Stan.
>
> > df=data.frame(out=1:4*3,pred1=1:4,pred2=1:4*2)
> > regression=function(tble,a,b)
> + {
> + plot.new()
> + plot(tble[a]~tble[b])
> + lmm=lm(tble[a]~tble[b])
> + abline(lmm)
> + anova(lmm)
> + }
> > df[1]
> out
> 1 3
> 2 6
> 3 9
> 4 12
> > df
> out pred1 pred2
> 1 3 1 2
> 2 6 2 4
> 3 9 3 6
> 4 12 4 8
> > regression(df,1,3)
> Error in model.frame(formula, rownames, variables,
> varnames, extras, extranames, :
> invalid type (list) for variable 'tble[a]'