-1

I know there are several questions about this topic, but none of them seem to answer my specific question.

I have a dataset with five independent variables and I want to add two dummy variables to my regression in R. I have my data in Excel and importing the dataset is not a problem (I use read.csv2). Now, when I want to see my dummy variables, D1 and D2, I can't. I can see all the other variables. The two dummy variables both vary from 0 and 1 through the dataset.

I can easily see a summary of all my data, including D1 and D2 (with median, mean, etc.), and I can call each of the 5 variables separately without any problems at all, but I can't do that with D1 and D2.

> str(tilskuere) 'data.frame': 180 obs. of 7 variables: 
$ ATT : int 3166 4315 7123 6575 7895 7323 3579 9571 5345 6595 ... 
$ PRICE : int 80 95 120 100 105 115 80 130 105 100 ... 
$ viewers: int 41000 43000 56000 66000 157000 91000 51000 30000 36000 72000 ... 
$ CB1 : int 10 10 5 2 7 2 3 1 10 1 ... 
$ CB2 : num 1 1 1 0 0.33 ... 
$ D1 : int 0 0 0 1 0 0 0 0 0 0 ... 
$ D2 : int 1 0 0 0 0 1 1 0 0 0 ... 
> summary(tilskuere) 
> mean(ATT) [1] 6856.372 
> mean(D1) Fejl i 
mean(D1) : object 'D1' not found 

To sum up: I can run regressions in R without D1 and D2, but I can't include these as dummy variables as R can't find these variables, when I run them. R simply says "object D1 not found."

I hope someone can help. Thank you in advance.

Kind regards Mikkel

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Mikkel
  • 23
  • 3
  • 2
    Welcome to SO! Please add a minimal, [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and show what you have tried. Thanks! – Henrik Sep 26 '13 at 12:38
  • Please provide a reproducible example, with code. It's hard to guess exactly what you're doing from your description. – Tyler Sep 26 '13 at 12:40
  • 1
    It is not clear what it means to "see" the variables here. When you read in a csv, the entire table is wrapped in a data.frame, so if `dat <- read.csv(...)` then you should look at `dat$D1`. – Frank Sep 26 '13 at 12:48
  • Sure. I have tried this so far: > str(tilskuere) 'data.frame': 180 obs. of 7 variables: $ ATT : int 3166 4315 7123 6575 7895 7323 3579 9571 5345 6595 ... $ PRICE : int 80 95 120 100 105 115 80 130 105 100 ... $ viewers: int 41000 43000 56000 66000 157000 91000 51000 30000 36000 72000 ... $ CB1 : int 10 10 5 2 7 2 3 1 10 1 ... $ CB2 : num 1 1 1 0 0.33 ... $ D1 : int 0 0 0 1 0 0 0 0 0 0 ... $ D2 : int 1 0 0 0 0 1 1 0 0 0 ... > summary(tilskuere) > mean(ATT) [1] 6856.372 > mean(D1) Fejl i mean(D1) : object 'D1' not found CB is short for competitive balance – Mikkel Sep 26 '13 at 12:50
  • Please add _reproducible data_ and the code of what you have tried to your question - much easier to format it nicely. – Henrik Sep 26 '13 at 12:52
  • @Frank: if I do what you suggest "dat$D1" I can see the data for D1. Maybe I should just use "dat$D1" in my regression. – Mikkel Sep 26 '13 at 12:55
  • PRICE ATT CB1 CB2 D2 viewers D1 1 80 3166 10 1.0000 1 41000 0 Here you can see the variables too. – Mikkel Sep 26 '13 at 13:00
  • @Mikkel: use `data=dat` in your regression. – Ben Bolker Sep 26 '13 at 13:03
  • You may also wish to have a look at `?Extract` – Henrik Sep 26 '13 at 13:09

1 Answers1

0

I added the material in your comment to the text , added some linefeeds, and it is now clear that you don't understand that columns are not first class objects in R. Try:

mean(tilskuere$D1)

You can see what objects are in your workspace with:

ls()

You appear to have an object named ATT in your workspace as well as a length-180 column by the same name in the object named tilskuere.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • When I enter ls(tilskuere) I get all of my variables listed correctly. "tilskuere" is what I call my model. "ATT" is my dependent variable. – Mikkel Sep 26 '13 at 13:27
  • ATT is in two places in your workspace. The is a numeric vector named ATT and then within the dataframe tilskuere there is another. They might be the same or they might not be. From the workspace environment you cannot see D1 but within tilskuere you can see it. You now need to read further in "Introduction to R" about how to use regression functions with `data=tilskuere` as a parameter so that the columns can be accessed via formulas. – IRTFM Sep 26 '13 at 13:32
  • I think the reason why ATT appears in my workspace and also in the object "tilskuere", is because I tried out different things earlier and I made a model called "modelatt". How can you, @DWin, see my workspace btw? Also, it's working now with the dummy variables - so thanks guys – Mikkel Sep 26 '13 at 13:39
  • 2
    I cannot see your workspace. I was making reasonable inferences from the code and output. – IRTFM Sep 26 '13 at 13:42