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.
Steve, I tried to replicate your error, but couldn't do it. The code below works just fine for me. Does it for you? # create an example data frame Bridges <- data.frame(CONDITION=1:5, TRAFFIC=rnorm(5), WEATHER=rnorm(5)) # create new variable "LOG_COND" Bridges$LOG_COND = log(Bridges$CONDITION, base = exp(1)) # fit linear model lm.bridges = lm(LOG_COND ~ TRAFFIC + WEATHER, data = Bridges) Jean slavrenz <slavrenz@purdue.edu> wrote on 08/20/2012 02:30:31 PM:> > 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. Ihave> 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 keyvariables> 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 basicR> user right now, so I'm definitely looking for something that is more > functional than elegant. > > Thanks, > > -Steve[[alternative HTML version deleted]]
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.