使用`transform`函数对分类变量中的值进行排序

时间:2013-10-05 12:19:21

标签: r

我有一个像这样的测试数据集:

table(spamday)  
FR MO SA SU TH TU WE  
30 96  9  9 45 60 51

结构:

str(spamday)  
'data.frame':   300 obs. of  1 variable:
$ SPAMreceived: Factor w/ 7 levels "FR","MO","SA",..: 2 5 5 5 6 2 6 2 2 6 ...  

我的目标是使用transform函数使分类变量成为具有levels指令强加的顺序的有序因子

所以我运行以下代码:

spamday <- transform(spamday, SPAMreceived <- factor(SPAMreceived, levels = c("MO", "TU", "WE", "TH", "FR", "SA", "SU"), ordered = TRUE))

并猜猜是什么?没有任何反应......使用table时仍然相同:

table(spamday)  
FR MO SA SU TH TU WE  
30 96  9  9 45 60 51

我很困惑......我做错了什么?

1 个答案:

答案 0 :(得分:3)

有时候跟踪使用<-以及何时使用它有点困难,但这是你不想要使用它。换句话说,只需将代码更改为以下内容即可:

spamday <- transform(spamday, SPAMreceived = factor(SPAMreceived, 
    levels = c("MO", "TU", "WE", "TH", "FR", "SA", "SU"), ordered = TRUE)) 

这是一个小例子:

set.seed(1)
spamday <- data.frame(SPAMreceived = sample(c("FR", "MO", "SA", "SU", "TH", "TU", "WE"),
                                            50, replace = TRUE))
table(spamday$SPAMreceived)
# 
# FR MO SA SU TH TU WE 
#  5  7  7  6  9 10  6

temp <- transform(spamday, SPAMreceived = factor(SPAMreceived, 
    levels = c("MO", "TU", "WE", "TH", "FR", "SA", "SU"), ordered = TRUE))
table(temp$SPAMreceived)
# 
# MO TU WE TH FR SA SU 
#  7 10  6  9  5  7  6 

或者,您甚至不需要创建有序因子列。只需在最后阶段对表格进行排序:)

table(spamday$SPAMreceived)[c("MO", "TU", "WE", "TH", "FR", "SA", "SU")]
# 
# MO TU WE TH FR SA SU 
#  7 10  6  9  5  7  6