My issue relates to adding text to a matrix and finding that the text is converted to a number. 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 } If I look at names[,1], I get the following: names[,1] [1] EQ_Level_UK EQ_Level_EUR EQ_Level_US EQ_Level_Far East [5] IR_PC 1_UK IR_PC 2_UK IR_PC 3_UK Swap_PC 1_UK [9] Swap_PC 2_UK Swap_PC 3_UK FX_Level_EUR FX_Level_US [13] FX_Level_Far East Infl_PC 1_UK Infl_PC 2_UK Infl_PC 3_UK [17] Prop_Level_UK CreditAAA_PC 1_UK CreditAAA_PC 2_UK CreditAAA_PC 3_UK [21] CreditAA_PC 1_UK CreditAA_PC 2_UK CreditAA_PC 3_UK CreditA_PC 1_UK [25] CreditA_PC 2_UK CreditA_PC 3_UK CreditBBB_PC 1_UK CreditBBB_PC 2_UK [29] CreditBBB_PC 3_UK 29 Levels: CreditA_PC 1_UK CreditA_PC 2_UK CreditA_PC 3_UK ... Swap_PC 3_UK But if I look at testOutput[,1], I get: testOutput[,1] [1] "15" "13" "16" "14" "23" "24" "25" "27" "28" "29" "17" "19" "18" "20" "21" [16] "22" "26" "7" "8" "9" "4" "5" "6" "1" "2" "3" "10" "11" "12" "17" [31] NA NA "19" "18" NA NA NA "20" NA NA "21" NA NA "22" NA [46] NA "26" NA NA "7" NA NA "8" NA NA "9" NA NA "4" NA [61] NA "5" NA NA "6" NA NA "1" NA NA "2" NA NA "3" NA [76] NA "10" NA NA "11" NA NA "12" NA NA NA NA NA NA NA [91] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [106] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [121] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [136] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [151] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [166] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [181] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [196] NA NA NA NA NA 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. [[alternative HTML version deleted]]
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. >-- Sarah Goslee http://www.functionaldiversity.org
Hi Jessica, this tells me that your text is saved as a factor. Try : names <- read.csv(file="Names.csv",stringsAsFactors=F) Cheers Joris On Tue, Jun 1, 2010 at 11:04 AM, Jessica Queree <j.j.queree@googlemail.com>wrote:> My issue relates to adding text to a matrix and finding that the text is > converted to a number. > > > > 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 > > > > } > > > > > > If I look at names[,1], I get the following: > > > > names[,1] > > [1] EQ_Level_UK EQ_Level_EUR EQ_Level_US EQ_Level_Far > East > > [5] IR_PC 1_UK IR_PC 2_UK IR_PC 3_UK Swap_PC 1_UK > > [9] Swap_PC 2_UK Swap_PC 3_UK FX_Level_EUR FX_Level_US > > [13] FX_Level_Far East Infl_PC 1_UK Infl_PC 2_UK Infl_PC 3_UK > > [17] Prop_Level_UK CreditAAA_PC 1_UK CreditAAA_PC 2_UK CreditAAA_PC > 3_UK > > [21] CreditAA_PC 1_UK CreditAA_PC 2_UK CreditAA_PC 3_UK CreditA_PC 1_UK > > [25] CreditA_PC 2_UK CreditA_PC 3_UK CreditBBB_PC 1_UK CreditBBB_PC > 2_UK > > [29] CreditBBB_PC 3_UK > > 29 Levels: CreditA_PC 1_UK CreditA_PC 2_UK CreditA_PC 3_UK ... Swap_PC 3_UK > > > > But if I look at testOutput[,1], I get: > > > > testOutput[,1] > > [1] "15" "13" "16" "14" "23" "24" "25" "27" "28" "29" "17" "19" "18" "20" > "21" > > [16] "22" "26" "7" "8" "9" "4" "5" "6" "1" "2" "3" "10" "11" "12" > "17" > > [31] NA NA "19" "18" NA NA NA "20" NA NA "21" NA NA "22" > NA > > [46] NA "26" NA NA "7" NA NA "8" NA NA "9" NA NA "4" > NA > > [61] NA "5" NA NA "6" NA NA "1" NA NA "2" NA NA "3" > NA > > [76] NA "10" NA NA "11" NA NA "12" NA NA NA NA NA NA > NA > > [91] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [106] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [121] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [136] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [151] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [166] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [181] NA NA NA NA NA NA NA NA NA NA NA NA NA NA > NA > > [196] NA NA NA NA NA > > > > 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]