[R] Giving column names to a matrix

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Oct 29 19:42:28 CEST 2004



jmoreira at fe.up.pt wrote:

> Heloo,
> 
> I have the following problem:
> 
> orig.data	<- NULL
> Inside a loop I have instructions like:
> orig.data <- rbind(orig.data, ...)
> After that I do:
> colnames(orig.data)<-c('Data','InicioViagem', ...)
> Everything works fine.
> For example, the first line of the matrix is:
> 
>>orig.data[1,]
> 
>         Data InicioViagem      ...
>            1        40466      ...
> The problem is: I can't refer the columns by the column names.
> For example:
> 
>>orig.data[1,InicioViagem]
> 
> Error: Object "InicioViagem" not found
> or
> 
>>orig.data[1,]$InicioViagem
> 
> NULL
> but:
> 
>>orig.data[1,2]
> 
> [1] 40466
> works!
> 
> Can someone help me?
> 
> Thanks
> 
> Joao Moreira

Joao,
   The problem is that you are constructing a "matrix" and not a 
"data.frame". So you want:

orig.data[1, "InicioViagem"] # note the quotes

or

orig.data <- as.data.frame(orig.data)
orig.data$InicioViagem[1]

--sundar




More information about the R-help mailing list