Hello. I am a newbie to R. If I should be reading some FAQ or manual that could help answer my question please tell me and I will go there. Problem: I have a spreadsheet that contains a character code in each cell. The columns in the spreadsheet represent time and the rows represent people. I want to use the image function to display this information, but the image function requires numeric data. I know how to read in the spreadsheet into a data frame, but if I use data.matrix to convert data frame into a numeric matrix, each column gets converted separately, so that the same character code may get converted to a different numeric code depending on the column. How do I apply the character to numeric conversion so that each character gets converted to the same numeric value? A secondary problem is that after I do the conversion, I need to know what codes were converted to what numeric values. Example: In the example below, "B" gets converted to 2 in the first column, but gets converted to 1 in the second column.> dv <- read.csv( SourceFileName, header=TRUE) > dvm1 m2 1 A B 2 B C 3 C D> data.matrix(dv)m1 m2 [1,] 1 1 [2,] 2 2 [3,] 3 3 Thanks in advance, --eric
Eric Bell <eric <at> ericjbell.com> writes:> A secondary problem is that after I do the conversion, I need to know > what codes were converted to what numeric values. >Column-wise factor conversion can be handy, but often it bites. # Assume you have the data file m1 m2 A B B C C D #--------------- # make sure that no column-wise factor conversion is done dv <- read.csv( "a.txt", header=TRUE,sep=" ",stringsAsFactors=FALSE) # find unique levels in the whole data set faklevels = unique(unlist(dv)) dv[] = lapply(dv,function(x) factor(x,levels=faklevels)) # now we have common levels for all columns, we can convert to numerics dv[] = lapply(dv,as.numeric) # or combine the two above #dv[] = lapply(dv,function(x) as.numeric(factor(x,levels=faklevels))) Dieter
Eric Bell wrote:> Hello. I am a newbie to R. If I should be reading some FAQ or manual > that could help answer my question please tell me and I will go there. > > Problem: > > I have a spreadsheet that contains a character code in each cell. The > columns in the spreadsheet represent time and the rows represent people. > I want to use the image function to display this information, but the > image function requires numeric data. I know how to read in the > spreadsheet into a data frame, but if I use data.matrix to convert data > frame into a numeric matrix, each column gets converted separately, so > that the same character code may get converted to a different numeric > code depending on the column. How do I apply the character to numeric > conversion so that each character gets converted to the same numeric > value? > > A secondary problem is that after I do the conversion, I need to know > what codes were converted to what numeric values. >Hi Eric, Using your example data frame, matrix(as.numeric(unlist(dv)),nrow=dim(dv)[1]) gives you the matrix you want, and levels(unlist(dv)) unique(as.numeric(unlist(dv))) gives you the correspondence between character codes and numbers. Jim