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?
Related
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):
I have merged two data sets (data1.csv and data2.csv) and created a data file to run the analyses on. But, when I do the analysis, R cannot recognize some of my columns (variables). My analysis is a multinational logit one.
The code for analysis is:
library(nnet)
mlogit<- multinom(groupadmit~mid_pop, tot_male10, tot_female10, male16,
tot_female16, agemean10, agemean16, IMD_score, IMD_score2, data =
admitqof10_16)
and the error is:
Error in model.frame.default(formula = groupadmit ~ mid_pop, data =
admitqof10_16, :
object 'male16' not found
I tried a couple of ways to solve the problem. For example, I use ls() to see whether the created data.table is in the R or not and it was there. I changed the name of the variable, but again it does not work. I exported the created data and imported it again. But, I have failed.
May someone please help me know why this problem happens and what I can do to resolve it?
Thanks in advance
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.
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!
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)