rrstudiorda

Error in eval(predvars, data, env) : invalid 'envir' argument of type 'character'


I am newbie to R and started doing Linear Regression in RStudio with a very simple code like the following:

dataset = load("C:\\pathToRDA\\swiss.RDA")

simple.fit = lm(formula = VoteYes ~ age,data=dataset)

The sample code loads in a RDA file which contains an object s in it.

After running this, I got a very common error:

Error in eval(predvars, data, env) :  invalid 'envir' argument of type 'character' 

After googling for a while, I think the error has to do with the

data=dataset

part, since most similar errors that I got on Google is that the dataset is a String instead of a variable reference. But I think in my case dataset is not a String indeed, as in this tutorial:

tutorial

Thanks in advance for any suggestions!


Solution

  • Use load directly

    load("C:\\pathToRDA\\swiss.RDA")
    #Assuming the dataset inside swiss.RDA is called swiss
    simple.fit = lm(formula = VoteYes ~ age, data=swiss) 
    

    dataset = load("C:\\pathToRDA\\swiss.RDA") will create a varible in the Values section called dataset in your Global env with a string vlaue "swiss".

    For example:

    mtest = mtcats
    save(mtest, file = "mt.rda")
    mtc = load("~/mt.RDA") #Will create variable called mtc = "mtest" in Values in Global env.  
    load("~/mt.RDA") #loads the mtest dataset