[R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

Mark Difford mark_difford at yahoo.co.uk
Tue Jun 16 11:07:34 CEST 2009


Hi Mao,

>> I am confused. And, I want to know how to assign a wanted order to factor 
>> levels, intentionally?

You want ?relevel. Although the documentation leads one to think that it can
only be used to set a reference level, with the other levels being moved
down, presently it can in fact be used to set any order you wish. For a
factor with just a few levels you could simply use an index into the default
order.

##
new_d <- d
c(5,1,6:10,2:4)
new_d$population <- relevel(d$population,
levels(d$population)[c(5,1,6:10,2:4)])

Ignore the warning. Note that relevel can also be used "on-the-fly," so
without permanently changing level-order.

Regards, Mark.


Mao Jianfeng wrote:
> 
> Dear R-helpers,
> 
> I want to make a series of boxplots on several numeric univariates with
> two
> group variables (species and population, population nested in species, and
> with population as the X-axis). In order to get a proper order of the
> individual populations in X-axis, I need to assign a wanted order to the
> factor (population). I used the levels() function to do this assignment,
> but
> it seemed levels() function not only changed the levels of the factor, but
> also the correlations of the  factor and the numeric variables.
> 
> I am confused. And, I want to know how to assign a wanted order to factor
> levels, intentionally? I think assignment is also indispensible for others
> who are do data analysing using R. Can you help me?
> 
> Thank you a lot in advance.
> 
> Best regards,
> Mao J-F
> 
> data, code, and results I used and got are as followed:
> (You can find that the correlations of the  factor and the numeric
> variables
> changed, before and after the levels() was performed.)
> 
> 
>> d<-read.delim("All.txt",header=T)
>> d
>    species population conlen tscale fscale tseen    w100s nfsee
> 1       Py     YXPy01   8.60    153     69   111 1.680851    94
> 2       Py     YXPy01   8.10    173     74   139 1.848485   133
> 3       Py     YLPy01   6.50    138     58    99 1.520833    48
> 4       Py     YLPy01   5.90    153     67   118 1.355140   107
> 5       Py     KMPy01   6.10    113     48    75 1.470588    51
> 6       Py     KMPy01   5.10    129     54   100 1.176471    68
> 7       Py     KMPy01   3.90    109     37    30 1.500000    22
> 8       Py     KMPy01   5.00    128     55    71 1.468750    64
> 9       Py     KMPy01   4.70    132     54    32 1.500000    28
> 10      Py     KMPy01   5.80    113     52    65 1.136364    45
> 11      Py     KMPy01   4.70    114     42    71 1.131148    61
> 12      Py     KMPy01   5.00    120     77   131 1.403361   119
> 13      Py     GSPy02   6.20    152     59   102 1.348837    43
> 14      Py     GSPy02   6.20    111     41    64 2.805556    36
> 15      Py     GSPy02   6.70    130     56    67 1.757576    33
> 16      Py     GSPy02   6.60    115     47    78 1.603175    63
> 17      Py     GSPy02   8.90    137     61   102 1.767677    99
> 18      Py     GSPy02   6.20    157     68   115 1.459016    61
> 19      Py     BCPy01   5.30     91     39    24 1.263158    19
> 20      Py     BCPy01   6.10    100     46    53 1.117647    17
> 21      Py     BCPy01   4.50     81     32    46 1.320000    25
> 22      Py     LJPy01   6.60    170     65    72 2.035714    56
> 23      Py     LJPy01   6.90    104     46    58 1.800000    55
> 24      Py     LJPy01   8.60    161     66    38 1.794118    34
> 25      Py     LJPy01   5.40    123     40    22 2.428571    21
> 26      Py     LJPy01   6.80    123     54    57 2.044444    46
> 27      Py     LJPy01   8.60    166     77    77 1.847458    59
> 28      Py     LJPy01   6.00    132     51    91 1.119048    84
> 29      Py     LJPy01   6.80    108     45    27 1.814815    27
> 30      Py     LJPy01   6.20    115     48    70 1.765957    47
> 31      Py     LJPy01   8.00    168     80   132 2.036364   111
> 32      Pd     CYPd01   6.70    138     57    23 1.555556     9
> 33      Pd     CYPd01   6.80    121     46    53 1.973684    38
> 34      Pd     CYPd01   5.90    114     52    60 1.250000    12
> 35      Pd     CYPd01   5.20    119     53    53 1.432432    37
> 36      Pd     CYPd01   7.60    118     46    63 2.000000    23
> 37      Pd     CYPd01   6.10    144     61    24 1.428571    14
> 38      Pd     CYPd01   5.50    130     46    62 1.320000    54
> 39      Pd     CYPd01   6.60    153     57    83 1.558442    77
> 40      Pd     CYPd02   5.90    111     32    51 1.300000    10
> 41      Pd     CYPd02   7.10    121     51    80 1.451613    31
> 42      Pd     CYPd02   7.30    150     68   127 1.681416   113
> 43      Pd     CYPd02   5.60    121     38    64 1.228571    36
> 44      Pd     CYPd02   7.20    140     62    88 1.585366    41
> 45      Pd     CYPd02   6.10    113     54    91 1.256757    74
> 46      Pd     CYPd03   4.60    109     45    57 1.093750    32
> 47      Pd     CYPd03   4.90    115     44    45 1.235294    17
> 48      Pd     CYPd03   6.40    134     44    64 1.209302    45
> 49      Pd     CYPd03   4.60     96     42    41 1.150000    21
> 50      Pd     CYPd03   5.60    131     43    45 1.771429    35
> 51      Pd     CYPd03   6.10    124     48    59 1.578947    38
> 52      Pd     CYPd03   5.20    110     57    71 1.340426    47
> 53      Pd     CYPd03   5.50    118     57    83 1.625000    48
> 54      Pd     CYPd03   6.10    106     61    95 1.559322    60
> 55      Pd     CYPd03   6.20    121     64   100 1.707692    65
> 56      Pd     CYPd03   5.10     99     38    28 1.430000    20
> 57      Pd     CYPd03   5.10    132     45    47 1.791667    24
> 58      Pd     YLPd01   6.15    120     43    46 1.446000    21
> 59      Pt     BXPd01   4.60     64     18    23 2.166667    18
> 60      Pt     BXPd01   5.10     87     26    38 2.250000    32
> 61      Pt     BXPd01   4.80     89     27    50 2.130435    46
> 62      Pt     BXPd01   6.00     97     29    31 2.684211    19
> 63      Pt     BXPd01   5.20     98     32    54 2.292683    41
> 64      Pt     GYPt01   4.30     98     27     8 4.000000     5
> 65      Pt     GYPt01   4.00     82     27    51 2.781250    32
> 66      Pt     GYPt01   5.00    106     35     8 4.333333     6
> 67      Pt     GYPt01   5.10     86     24    25 3.375000    16
> 68      Pt     GYPt01   4.60     79     25    21 2.631579    19
> 69      Pt     GYPt01   5.00     80     30    23 2.823529    17
> 70      Pt     NSPt01   5.30    107     27    37 2.850000    33
> 71      Pt     NSPt01   5.40     85     26    38 2.270000    32
> 72      Pt     NSPt01   5.40    102     31    50 5.320000    40
> 73      Pt     NSPt01   5.10     84     23    29 5.320000    23
> 74      Pt     NSPt01     NA     NA     NA    NA       NA    NA
> 75      Pt     NSPt01   4.10     57     17    24 2.700000    18
>> levels(d$population)
>  [1] "BCPy01" "BXPd01" "CYPd01" "CYPd02" "CYPd03" "GSPy02" "GYPt01"
> "KMPy01"
> "LJPy01" "NSPt01"
> [11] "YLPd01" "YLPy01" "YXPy01"
>> levels(d$population)<-c("YXPy01", "KMPy01", "YLPy01", "GSPy02", "BCPy01",
> "LJPy01", "GYPt01", "YLPd01", "CYPd01", "CYPd02", "CYPd03", "BXPd01",
> "NSPt01")
>> levels(d$population)
>  [1] "YXPy01" "KMPy01" "YLPy01" "GSPy02" "BCPy01" "LJPy01" "GYPt01"
> "YLPd01"
> "CYPd01" "CYPd02"
> [11] "CYPd03" "BXPd01" "NSPt01"
>> d
>    species population conlen tscale fscale tseen    w100s nfsee
> 1       Pt     NSPt01   8.60    153     69   111 1.680851    94
> 2       Pt     NSPt01   8.10    173     74   139 1.848485   133
> 3       Pt     BXPd01   6.50    138     58    99 1.520833    48
> 4       Pt     BXPd01   5.90    153     67   118 1.355140   107
> 5       Pt     YLPd01   6.10    113     48    75 1.470588    51
> 6       Pt     YLPd01   5.10    129     54   100 1.176471    68
> 7       Pt     YLPd01   3.90    109     37    30 1.500000    22
> 8       Pt     YLPd01   5.00    128     55    71 1.468750    64
> 9       Pt     YLPd01   4.70    132     54    32 1.500000    28
> 10      Pt     YLPd01   5.80    113     52    65 1.136364    45
> 11      Pt     YLPd01   4.70    114     42    71 1.131148    61
> 12      Pt     YLPd01   5.00    120     77   131 1.403361   119
> 13      Pt     LJPy01   6.20    152     59   102 1.348837    43
> 14      Pt     LJPy01   6.20    111     41    64 2.805556    36
> 15      Pt     LJPy01   6.70    130     56    67 1.757576    33
> 16      Pt     LJPy01   6.60    115     47    78 1.603175    63
> 17      Pt     LJPy01   8.90    137     61   102 1.767677    99
> 18      Pt     LJPy01   6.20    157     68   115 1.459016    61
> 19      Pt     YXPy01   5.30     91     39    24 1.263158    19
> 20      Pt     YXPy01   6.10    100     46    53 1.117647    17
> 21      Pt     YXPy01   4.50     81     32    46 1.320000    25
> 22      Pt     CYPd01   6.60    170     65    72 2.035714    56
> 23      Pt     CYPd01   6.90    104     46    58 1.800000    55
> 24      Pt     CYPd01   8.60    161     66    38 1.794118    34
> 25      Pt     CYPd01   5.40    123     40    22 2.428571    21
> 26      Pt     CYPd01   6.80    123     54    57 2.044444    46
> 27      Pt     CYPd01   8.60    166     77    77 1.847458    59
> 28      Pt     CYPd01   6.00    132     51    91 1.119048    84
> 29      Pt     CYPd01   6.80    108     45    27 1.814815    27
> 30      Pt     CYPd01   6.20    115     48    70 1.765957    47
> 31      Pt     CYPd01   8.00    168     80   132 2.036364   111
> 32      Py     YLPy01   6.70    138     57    23 1.555556     9
> 33      Py     YLPy01   6.80    121     46    53 1.973684    38
> 34      Py     YLPy01   5.90    114     52    60 1.250000    12
> 35      Py     YLPy01   5.20    119     53    53 1.432432    37
> 36      Py     YLPy01   7.60    118     46    63 2.000000    23
> 37      Py     YLPy01   6.10    144     61    24 1.428571    14
> 38      Py     YLPy01   5.50    130     46    62 1.320000    54
> 39      Py     YLPy01   6.60    153     57    83 1.558442    77
> 40      Py     GSPy02   5.90    111     32    51 1.300000    10
> 41      Py     GSPy02   7.10    121     51    80 1.451613    31
> 42      Py     GSPy02   7.30    150     68   127 1.681416   113
> 43      Py     GSPy02   5.60    121     38    64 1.228571    36
> 44      Py     GSPy02   7.20    140     62    88 1.585366    41
> 45      Py     GSPy02   6.10    113     54    91 1.256757    74
> 46      Py     BCPy01   4.60    109     45    57 1.093750    32
> 47      Py     BCPy01   4.90    115     44    45 1.235294    17
> 48      Py     BCPy01   6.40    134     44    64 1.209302    45
> 49      Py     BCPy01   4.60     96     42    41 1.150000    21
> 50      Py     BCPy01   5.60    131     43    45 1.771429    35
> 51      Py     BCPy01   6.10    124     48    59 1.578947    38
> 52      Py     BCPy01   5.20    110     57    71 1.340426    47
> 53      Py     BCPy01   5.50    118     57    83 1.625000    48
> 54      Py     BCPy01   6.10    106     61    95 1.559322    60
> 55      Py     BCPy01   6.20    121     64   100 1.707692    65
> 56      Py     BCPy01   5.10     99     38    28 1.430000    20
> 57      Py     BCPy01   5.10    132     45    47 1.791667    24
> 58      Py     CYPd03   6.15    120     43    46 1.446000    21
> 59      Pd     KMPy01   4.60     64     18    23 2.166667    18
> 60      Pd     KMPy01   5.10     87     26    38 2.250000    32
> 61      Pd     KMPy01   4.80     89     27    50 2.130435    46
> 62      Pd     KMPy01   6.00     97     29    31 2.684211    19
> 63      Pd     KMPy01   5.20     98     32    54 2.292683    41
> 64      Pd     GYPt01   4.30     98     27     8 4.000000     5
> 65      Pd     GYPt01   4.00     82     27    51 2.781250    32
> 66      Pd     GYPt01   5.00    106     35     8 4.333333     6
> 67      Pd     GYPt01   5.10     86     24    25 3.375000    16
> 68      Pd     GYPt01   4.60     79     25    21 2.631579    19
> 69      Pd     GYPt01   5.00     80     30    23 2.823529    17
> 70      Pd     CYPd02   5.30    107     27    37 2.850000    33
> 71      Pd     CYPd02   5.40     85     26    38 2.270000    32
> 72      Pd     CYPd02   5.40    102     31    50 5.320000    40
> 73      Pd     CYPd02   5.10     84     23    29 5.320000    23
> 74      Pd     CYPd02     NA     NA     NA    NA       NA    NA
> 75      Pd     CYPd02   4.10     57     17    24 2.700000    18
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/confusion-on-levels%28%29-function%2C-and-how-to-assign-a-wanted-order-to-%09factor-levels%2C-intentionally--tp24048978p24050425.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list