You are also missing the fact that loops are often not required in R. In this 
case print() has a method for printing data frames and that method is 
automatically used when you simply type the data.frame name or if you 
explicitly call it with print(stockList):

> stockList <- read.table(text="symbol,shares
+ XOM,1000
+ APA,400
+ CVX,200", header = T, sep=",")
> 
> stockList
  symbol shares
1    XOM   1000
2    APA    400
3    CVX    200

You will save yourself hours of frustration by reading some of the official and 
unofficial documentation for R:

http://cran.r-project.org/manuals.html
http://www.r-project.org/other-docs.html
http://cran.r-project.org/other-docs.html

David Carlson

-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of David Winsemius
Sent: Sunday, June 29, 2014 5:58 PM
To: Robert Sherry
Cc: r-help@r-project.org
Subject: Re: [R] A Question about read.table and Data Frames in R


On Jun 29, 2014, at 3:22 PM, Robert Sherry wrote:

> I created the following file:
>
> symbol,shares
> XOM,1000
> APA,400
> CVX,200
>
> I then read the file in R using the command:
>       stockList=read.table("/NotesOnR/stockList", header = T, sep=",")
>
> I would then expect the following expression to evaluate to the simple
> string APA:
>       stockList$symbol[2]
> However when I type that in on the command line, I get:
> [1] APA
> Levels: APA CVX XOM
>
> I would also expect to be able to print out the table by using the  
> command:
>       for( i in 1:3 ) {
>               print( stockList$symbol[i] )
>               print ( stockList$shares[i] )
>       }
>
> However, as part of the output I get:
>       Levels: APA CVX XOM
> I do not understand what is going on. Also, when I try passing the
> expression stockList$symbol to a standard function in R, it does not  
> work.
>
> I think that I am missing something here but I am not sure what I am
> missing.

You are missing the fact that a factor variable was created. If you  
had added stringsAsFactors=FALSE to your read.table code, you would  
ahve gotten what you expected.


> Should I be writing code like:
>       for  ( i in stockList$smybol )
> However,  given the symbol, I am not sure how to get the share count  
> that is
> in the same row.
>
> I thank the group in advance for their responses.
>
> Bob
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to