[R] Using image function to plot character values

Dieter Menne dieter.menne at menne-biomed.de
Fri Apr 25 09:22:07 CEST 2008


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



More information about the R-help mailing list