0

I'm brand new to R and am having difficulty with something very basic. I'm importing data from an excel file like this:

data1 <- read.csv(file.choose(), header=TRUE)

When I try to look at the data in the table by column, R doesn't recognize the column headers as objects. This is what it looks like

summary(Square.Feet)
Error in summary(Square.Feet) : object 'Square.Feet' not found

I need to run a regression and I'm having the same problem. Any help would be much appreciated.

Jarom
  • 1,067
  • 1
  • 14
  • 36

2 Answers2

3

Yes it recognizes, you have to tell R to select the dataframe so:

summary(data1$Square.Feet)

Where "data" is the name of your dataframe, and after the dollar goes the name of the variable

Hope it helps

UPDATE

As suggested below, you can use the following:

data1 <- read.csv(file.choose(), header=TRUE) 
attach(data1)

This way, by doing "attach", you avoid to write everytime the name of the dataset, so we would go from

 summary(data1$Square.Feet)

To this point after attaching the data:

summary(Square.Feet)

However I DO NOT recommend to do it, because if you load other datasets you may mess everything as it's quite common that variables have the same names, among other major problems, see here (Thanks Ben Bolker for your contribution): here , here, here and here

Community
  • 1
  • 1
adrian1121
  • 904
  • 2
  • 9
  • 21
  • 1
    Great! That fixes the problem. Is there a way to not have to always reference the data frame? In some of the resources I'm using they use the same syntax as I used. I'm not sure what they did to not have to reference the dataframe every time they bring up a column. – Jarom May 04 '16 at 16:06
  • 2
    You can use: data1 <- read.csv(file.choose(), header=TRUE) attach(data1) However I DO NOT recommend to do this, as it's very likely than when you load other datasets you'll mess everything, so it's better always to stick with data1$variable, even if it looks ugly or it's annoying. Hope you got what you needed and don't forget to upvote and accept answer! thank you! :) – adrian1121 May 04 '16 at 16:11
  • 1
    @adrian1121, you could add the comment to your answer (along with the important caveat); perhaps also link [here](http://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/) and [here](https://4dpiecharts.com/2011/04/29/friday-function-triple-bill-with-vs-within-vs-transform/) and to this: http://stackoverflow.com/questions/26478727/naming-conflicts-in-r-when-using-attach – Ben Bolker May 04 '16 at 16:20
  • 1
    ... and http://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead ... – Ben Bolker May 04 '16 at 16:44
  • @BenBolker Thank you for your contribution! – adrian1121 May 04 '16 at 16:57
2

if you want a summary of all data fields, then

summary(data1)

or you can use the 'with' helper function

with(data1, summary(Square.Feet))  
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
greengrass62
  • 968
  • 7
  • 19