使用“级别”关于跨数据框列的折叠因子的快速问题。下面是基本数据帧的代码。
var keepIt = true;
var sample = [1,2,3,4].filter(function(){
return keepIt; //It will be true always,
//so all the items will be preserved.
//If it is false, then all the elements will be ignored.
});
我可以使用以下代码通过组合“满意”和“中立”轻松折叠级别。
Service<-c("AS","AS","PS","PS","RS","RS","ES","ES")
Year<-c("2015","2016","2015","2016","2015","2016","2015","2016")
Q1<-c("Dissatisfied","Satisfied","Satisfied","Neutral","Dissatisfied","Dissatisfied","Satisfied","Neutral")
Q2<-c("Dissatisfied","Neutral","Satisfied","Dissatisfied","Dissatisfied","Neutral","Satisfied","Satisfied")
Example<-data.frame(Service,Year,Q1,Q2)
但是,对于列Q1和Q2,如何在类似的列上同时执行此操作?我知道使用lapply或apply有一种简单的方法,但我似乎无法使它工作。
答案 0 :(得分:4)
我认为你的手术不可行。我以为我记得这种崩溃因子水平被严重弃用以至于会引发错误,但由于它适用于一列,它应该(并且确实)适用于多列。
Example[3:4] <- lapply(Example[3:4], 'levels<-', c("Dissatisfied","Satisfied","Satisfied") )
Example
Service Year Q1 Q2
1 AS 2015 Dissatisfied Dissatisfied
2 AS 2016 Satisfied Satisfied
3 PS 2015 Satisfied Satisfied
4 PS 2016 Satisfied Dissatisfied
5 RS 2015 Dissatisfied Dissatisfied
6 RS 2016 Dissatisfied Satisfied
7 ES 2015 Satisfied Satisfied
8 ES 2016 Satisfied Satisfied
答案 1 :(得分:0)
我们可以使用lapply
。创建要进行子集化的列索引。这里是以&#34; Q&#34;开头的列名。后跟数字(^Q\\d+
)。使用grep
获取索引,然后在这些列上使用lapply
,并使用recode
中的library(car)
更改levels
。
nm1 <- grep("^Q\\d+", names(Example))
Example[nm1] <- lapply(Example[nm1], recode,
'c("Satisified", "Neutral")= "Satisfied"')
因为我们只是改变了#34;中立&#34; &#34;满意&#34;,而'#34;满意&#34;应保持原样,我们也可以这样做
Example[nm1] <- lapply(Example[nm1], recode, '"Neutral"= "Satisfied"')