Skip to content

Unable to reference variable created in dataframe

4 messages · Jean V Adams, slavrenz

#
Hello, I have question about referencing a variable that I created in a
dataframe, and I am hoping the experts on here can be of assistance. I have
a dataset of highway bridges with approximately 10,000 observations. I
imported these into R in the form of a dataframe called "Bridges".

I am interested in running some basic linear models on a few key variables
with the data. Say "CONDITION" is my dependent variable, and "TRAFFIC" and
"WEATHER" are the independent variables I am considering. In order to
capture a possible exponential relationship between "CONDITION", "TRAFFIC",
and "WEATHER", I created the variable "LOG_COND":

Bridges$LOG_COND = log(Bridges$CONDITION, base = exp(1))

I then used the "str" command to verify that the variable shows up
correctly, and is of the correct form (numeric) in the dataframe.

However, when I then attempt to use my new variable, say, for example:

lm.bridges = lm(LOG_COND ~ TRAFFIC + WEATHER, data = Bridges),      (not
sure why the tilde symbol shows up as a minus sign here)

I get "Error in eval(expr, envir, enclos) : object 'LOG_COND' not found"

Can anyone enlighten me as to what I am doing wrong, and if possible, some
basic code that I can use to rectify the situation? I am a pretty basic R
user right now, so I'm definitely looking for something that is more
functional than elegant.

Thanks,

-Steve



--
View this message in context: http://r.789695.n4.nabble.com/Unable-to-reference-variable-created-in-dataframe-tp4640822.html
Sent from the R help mailing list archive at Nabble.com.
#
That simple code works for me as well, but for some reason my actual code
still isn't working. Here is exactly what I'm doing:

- The code for importing my data is:

/Bridges = read.csv(file = "C:/R/Bridges.RData.csv", head=TRUE, sep=",")/


- Within my group of highway bridges, I have defined a subgroup of bridges
that are Interstates, and are made of concrete. The code for this is:

/Interstates_Concrete = Bridges [Bridges$ROUTE_PREFIX_005B == 1 &
Bridges$STRUCTURE_KIND_043A == 1 | 
	Bridges$STRUCTURE_KIND_043A == 2,]/

where "ROUTE_PREFIX_005B" signifies an interstate bridge if it is equal to
1, and "STRUCTURE_KIND_043A" signifies a concrete bridge if it is equal to 1
or 2


- Then, my code for converting the dependent variable to log form is:

/Bridges$LOG_DECK_COND = log(Bridges$DECK_COND_058, base = exp(1))/

where "DECK_COND_058" is the condition of the bridge deck, with values
ranging from 3 to 9


Both of these code snippets run without error. However, when I try to run
the code for the actual model:

/exp.int.con= lm(LOG_DECK_COND ~ CUM_ADTT_2011 + CUM_WSL_2011, data =
Interstates_Concrete)/

where CUM_ADTT_2011 and CUM_WSL_2011 are existing variables in the
dataframe, I get the following:

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


Since:

/lm.int.con= lm(DECK_COND_058 ~ CUM_ADTT_2011 + CUM_WSL_2011, data =
Interstates_Concrete)/

runs without any issues, I have to conclude that there is definitely a
problem with the log conversion; I just don't know what it is. I have tried
running this on multiple machines and in multiple workspaces, and the
problem remains.






--
View this message in context: http://r.789695.n4.nabble.com/Unable-to-reference-variable-created-in-dataframe-tp4640822p4640900.html
Sent from the R help mailing list archive at Nabble.com.
#
Wow, sometimes writing it all out does really help to see the issue. I now
see that I need to run the "log" command before splitting my data into a
subset, or it won't see the newly created variable.

My code now runs without issue. Thanks for everyone who took the time to
read this and/or help. I feel rather silly.



--
View this message in context: http://r.789695.n4.nabble.com/Unable-to-reference-variable-created-in-dataframe-tp4640822p4640901.html
Sent from the R help mailing list archive at Nabble.com.