更改data.frame中所有列/变量的级别顺序

时间:2013-08-20 16:04:54

标签: r dataframe

很抱歉提出另一个问题,但我彻底搜查了,并没有找到满意的答案。

我得到了一个包含6列的data.frame子集,这些列都是factors,其中包含5个级别的缩放比例数据:

strongly disagree - disagree - neutral - agree - strongly agree 

问题是,它们没有正确排序,在一种情况下缺少一个级别。 所以我知道如何分别纠正列的顺序:

data_frame$somecolumn <- 
ordered(data_frame$somecolumn, levels = c("strdisag", "disagree",  "neutral","agree","stragree"))

我的问题是如何将data_frame$somecolumn替换为一个表达式,该表达式将影响data.frame中的所有列,而不仅仅是单个变量。

1 个答案:

答案 0 :(得分:1)

我认为你的意思是这样......

# Make some reproducible data
set.seed(123)
df <- data.frame( replicate( 6 , sample( c("strdisag", "disagree",  "neutral","agree","stragree") , 5 , repl = TRUE ) ) )

#  Unordered factor variable
df[,1]
[1] disagree agree    neutral  stragree stragree
Levels: agree disagree neutral stragree

# Make list of ordered factor variables
out <- lapply( df , function(x) ordered(x, levels = c("strdisag", "disagree",  "neutral","agree","stragree") ) )

#  Combine into data.frame
res <- do.call( data.frame , out )
        X1       X2       X3       X4       X5       X6
1 disagree strdisag stragree stragree stragree    agree
2    agree  neutral  neutral disagree    agree  neutral
3  neutral stragree    agree strdisag    agree  neutral
4 stragree  neutral  neutral disagree stragree disagree
5 stragree  neutral strdisag stragree    agree strdisag

#  Check result
res[,1]
[1] disagree agree    neutral  stragree stragree
Levels: strdisag < disagree < neutral < agree < stragree