Re: [R] Issue with assigning text to matrix

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Tue, 01 Jun 2010 09:16:16 -0600

Jessica,

Two further comments:
1. When you read your data, your character vectors are

    stored as factors; is that what you want? 2. You may be better off using a list.

   -Peter Ehlers

On 2010-06-01 7:47, Sarah Goslee wrote:
> On Tue, Jun 1, 2010 at 5:04 AM, Jessica Queree
> <j.j.queree_at_googlemail.com> wrote:
>> My issue relates to adding text to a matrix and finding that the text is
>> converted to a number.
>>
>
> A matrix can only hold one type of data. Since you started with character,
> that's what you get.
>
> A dataframe can hold different types of data, both character and numeric.
> If you convert your matrix to a dataframe with dataf.frame() it can hold
> your output in the manner you expect.
>
> testOutput<- data.frame(matrix(nrow = 200, ncol = 5))
>
> Sarah
>>
>> This is the section of code I'm having trouble with:
>>
>>
>>
>> # First, I load in a list of names from a .csv file to 'names'
>>
>> names<- read.csv(file("Names.csv"))
>>
>>
>>
>> # Then I define a matrix which will be populated with various test
>> statistics, with several rows for each entry in names
>>
>>
>>
>> testOutput<-matrix(nrow = 200, ncol = 5)
>>
>> for (i in 1:nrow(names)){
>>
>>
>>
>> testOutput[i,1]<- names[i,1]
>>
>> testOutput[i,2]<- names[i,2]
>>
>>
>>
>> # test statistics code here
>>
>>
>>
>> }
>>
>>
>> That is, the names are now converted to numbers. I think this might have
>> something to do with the way I've defined the testOutput matrix, but haven't
>> been able to find any information about how to fix it. Can anyone help?
>>
>>
>>
>> Many thanks.
>>
>



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. Received on Tue 01 Jun 2010 - 15:18:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 15:20:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive