[R] Reading two-cloumn CSV file into a hash table

David Winsemius dwinsemius at comcast.net
Mon Sep 26 23:13:01 CEST 2011


On Sep 26, 2011, at 4:16 PM, Khanvilkar, Shashank wrote:

> Sending it again, with correct subject line.
>
>
> Hello All,
> Thanks in advance for all help,
>
> I am trying to read a two column csv file in R, which looks like:
> X,1
> Y,2
> Z,3
>
> I am using R commands:
> tmp = read.csv("test.csv", colClasses=c("character", "character"))
>
> How can make this into a hash table,

Not sure about hash tables. Doubt you are quite ready for them.  After  
you learn a bit more R you can try to set up environments.


> so that I can access, tmp["X"] and it will return me "1"?

Actually you cannot with your current object. You have read it in with  
the default settings for read.csv, so now R "thinks" your column names  
are "X" and "X.1". before you correct that problem,  try:

  str(tmp)

You should use header=FALSE  in your corrective efforts, since you  
don't have a header. Then you can say:

tmp[ which(tmp$V1 == "X") , "V2"] and get 1

You could also take the first column and assign those values to  
rownames(tmp) and then be able to access that '1' with:

tmp["X", 2]

(And I see that Gabor has shown you how to do something similar with a  
named vector).

With his example my suggestion was:
 > rownames(DF) <- DF$letters
 > DF["X", 2]
[1] 1


-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list