[R] factor levels on import

Stephen C. Upton supton at referentia.com
Sat Apr 22 09:00:59 CEST 2006


I have a data frame that I plan on importing from a file (there are 
other columns of numeric data):
 > xy
  x y
1 1 1
2 1 1
3 1 1
4 1 1
5 2 2
6 2 2
7 2 2
8 2 2
 >

where x is a column of factors, and y is a column of factors, that have 
different levels, e.g.,
 > x
[1] low low low low hi  hi  hi  hi
Levels: low hi
 > y
[1] med   med   med   med   medhi medhi medhi medhi
Levels: med medhi

How do I get the columns to use the combined levels  
c("low","med","medhi","high") so that the data frame is actually:
 > xy
  x y
1 1 2
2 1 2
3 1 2
4 1 2
5 4 3
6 4 3
7 4 3
8 4 3
 >

I'm sure there's a way to lapply or some such, but haven't broken the 
code. I'm also sure I'm missing something simple.

thanx
steve


R 2.2.1
Windows XP




More information about the R-help mailing list