Error in eval(expr, envir, enclos) : object 'RM' not found - r

I'm Trying to do a simple linear regresion by calling a .CSV file where i have 14 different variables...which were first in a Excel file.
In Excel i named the variables by just putting a name above the data, but i don't really believe that means that that names the whole column by that variable.
(Of course i first call the .CSV file from R:
> datos<-read.table("datos3.csv",header=T,sep=";",dec=",")
So that must be why when i call them by name in R like:
regresion<-lm(RM~MEDV)
R says: Error in eval(expr, envir, enclos) : object 'RM' not found
The thing i find strange is that when i change the order of the variables (say MEDV~RM) i get the same error but saying that MEDV was not found this time. Is it because is the first thing it reads and detects?
Is there a way from Excel or R that i can name the variables for me to call them without errors or is the problem anywhere else?
UPDATED CODE
rm(list=ls())
datos<-read.table("datos3.CSV",header=T,sep=";",dec=",")
View(datos)
regresion<-lm(MEDV,RM,data=datos)
After this i get
Error in stats::model.frame(formula = MEDV, data = datos, subset = RM, :
object 'MEDV' not found

Basically the problem is that since i was using first an excel file for my data, i had the variables named included in the first row, so when passing it to .CSV format and trying to read it in R, it transformed those characters into data as factors. So when trying to do a regresion it did not have only number values.
After that it was the "dec"argument in the read.csv causing trouble and printing the same error. So after removing that and typing:
datos <- read.csv("datos3.csv")
I had no trouble making the regresion and could analyze the data i wanted.
(special thanks to #user20650 for helping me out)

Related

Running a MCMC analysis with a new tree I made using BM, anyone know what this error would mean?

tree_mvBM <- read.nexus("C:/Users/Zach/Desktop/tree_mvBM.tre")
View(tree_mvBM)
dat <- data$Tp; names(dat) <- rownames(data)
Error in data$Tp : object of type 'closure' is not subsettable
You're trying to refer to an object called data in your global workspace, presumably a data frame. The object doesn't exist (you forgot to read it in,or you called it something else, or ... ?), so R is instead finding the built-in function data. It is trying to "subset" it (i.e. $Tp tells R to extract the element named "Tp"), which is not possible because you can't extract an element of a function. (Functions are called "closures" in R for technical reasons.)
This is one reason (probably the main reason) that you shouldn't give your variables names that match the names of built-in R objects (like I, t, c, data, df, ...). If you had called your data my_data instead the error message would be
Error: object 'my_data' not found
which might be easier to understand.
This is such a common error that there are jokes about it (image search the error message):

Error in eval(predvars, data, env) : object not found However, the object does appear to exist

I am new to R and to actually asking a question in Stack Overflow. I looked at several similar questions, but they did not seem to apply to help my situation. I am trying to make a linear model with the lm() function, but I get an error returned that states one of my data frame columns is able to be found:
library(SemiPar) # Contains Janka data
data(janka)
names(janka) <- c("Density", "Hardness")
janka.ls1 <- lm(hardness~dens, Data = janka)
I get the following error after running the final line:
Error in eval(predvars, data, env) : object 'Hardness' not found
As you can see, I named one of the two columns 'Hardness' and when I view the data I also see that the names do match. Further, when I try running the code using the original data column name, it still has the same issue.
Thanks for your help!!

Error in eval(expr, envir, enclos) : object 'score' not found

We have always been an SPSS shop but we're trying to learn R. Been doing some basics. I am trying to do a simple t-test, just to learn that code.
Here's my code, and what's happening:
Code screenshot
I don't get why it says "score" isn't found. It's in the table, and the read.csv code defaults to assuming the first row contains headers. I can't see why it's not "finding" the column for score. I'm sure it's something simple, but would appreciate any help.
You didn't store your imported csv file in a variable. It printed to the console because it had nowhere else to go - it just gets printed and then thrown away. You need to assign it for it to be saved in memory:
my_data_frame <- read.csv("ttest.csv")
Now your data exists in the variable my_data_frame and you can use it by supplying it as the data argument:
t.test(score ~ class, mu=0, alt="two.sided", conf=0.95, var.eg=F, paired=F, data=my_data_frame)
Also, in general, I would recommend using read_csv from the package readr over the default read.csv - it's faster.
Finally, when you ask questions, please provide your code as text, not a picture. You should also provide your data or a toy dataset - you can use the function dput to print code that will create your data, or just provide the csv file or some code that creates toy data.

dcast() gives me an error message

I am trying to use dcast() to change a long format data into a wide format data. I am using the data frame "airquality" that is built into R.
When I run the code below, I get this error message:
Error in eval(expr, envir, enclos) : object 'variable' not found.
I have tried to search for an answer here but I have not seen one that is specific to the function dcast().
airquality_cast1 <- dcast(airquality, month + day ~ variable)
I see that the problem is with the last bit of the code, but I can't seem to figure out what it is. Was I supposed to assign variable to some object before running this code? Or is there some package I need to install for this to work?
Thank you!

Trouble using attach() in R

I am having trouble assigning columns to the strings above them. I believe the proper way to do this is use the attach() function. I have a csv file loaded with columns of data on Flight, O.ring, and Temp. I have tried to clear previously attached data by using the detach() function, but have not had any luck.
### Files saved in the directory below
setwd("/Users/newUser/desktop/programming")
data <- read.table("Challenger.csv", header=TRUE)
attach(data)
O.ring
Error: object 'O.ring' not found
Flight
Error: object 'Flight' not found
Temp
Error: object 'Temp' not found
fit1 <- glm(O.ring ~ Temp + Pressure, family=binomial(logit))
Error in eval(expr, envir, enclos) : object 'O.ring' not found
fit1
Error: object 'fit1' not found
Edit: I also need help accessing the columns stored in data to use them to model. Any idea what the problem is with my fit1?
Don't use attach(). Ever. Forget it exists.
glm() has a data argument. Using that proves much less stressful.
glm(O.ring ~ Temp + Pressure, family = binomial(logit), data = data)
If you want to know why attach() is not advisable, see Why is it not advisable to use attach() in R and what should I use instead?

Resources