[R] A Question about read.table and Data Frames in R

David Winsemius dwinsemius at comcast.net
Mon Jun 30 00:57:57 CEST 2014


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 at 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



More information about the R-help mailing list