我正在尝试解决数据阵营R介绍中的问题。问题陈述如下
作为数据分析师,您决定加入Rways,这是一家新航空公司,仅根据数据驱动的决策提供航班和服务。业务进展顺利,管理层正在考虑将报价从经济舱扩展到商务舱和头等舱。您应该处理调查问卷的结果,该问卷调查了受访者的首选类别。其中50个受访者的结果可以在字符向量fly_class中找到。
将fly_class向量转换为因子fly_class_factor。因素 水平应该是"经济","业务"和"第一"。订购因素 如果这在这种情况下有意义的话。
fly类向量包含
> > fly_class
[1] "eco" "bus" "eco" "bus" "fir" "eco" "eco" "bus" "eco" "eco" "fir" "eco"
[13] "eco" "eco" "eco" "bus" "eco" "eco" "eco" "fir" "bus" "eco" "eco" "fir"
[25] "eco" "bus" "eco" "eco" "eco" "eco" "fir" "fir" "eco" "bus" "eco" "eco"
[37] "bus" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco"
[49] "eco" "bus"
当我尝试尝试下面的片段时,R引入了该因子。我需要知道它为何被引入。
> fly_class_factor <- factor(fly_class, ordered = TRUE, levels = c("economy", "business", "first"), labels = c("eco", "bus", "fir"))
> fly_class_factor
[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[16] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[31] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[46] <NA> <NA> <NA> <NA> <NA>
Levels: eco < bus < fir
答案 0 :(得分:4)
尝试切换RestTemplate
和RestTemplate restTemplate = // likely injected into the test
ClientHttpRequestFactory originalRequestFactory = restTemplate.getRequestFactory();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
try {
// use mockServer as usual...
mockServer.verify();
} finally {
restTemplate.setRequestFactory(originalRequestFactory);
}
参数。 levels
应该提供数据的当前值,labels
给出您希望它们显示的值。
levels