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 ______________________________________________ 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.