如何使用POSIXct变量订购因子水平?

时间:2017-01-01 16:23:38

标签: r

我有这样的数据集

library(forcats)
g <- data.frame(var = as.factor(c("C", "B", "A")),
       dt = as.POSIXct(c("2016-01-01 00:02:20","2016-02-01 00:03:43", "2016-03-01 10:00:00"), format = "%Y-%m-%d %H:%M:%S", tz = "UTC") 
       )

我试图根据dt字段来调整数据,但是我收到了一个错误:

fct_relevel(g$var, order(g$dt))

> fct_relevel(g$var, order(g$dt))
[1] C B A
Levels: A B C
Warning message:
Unknown levels in `f`: 1, 2, 3 
> 

如何解决这个问题?我想使用forcats包进行此操作。

1 个答案:

答案 0 :(得分:1)

我们可以指定&#39; var&#39;的#include "stdafx.h" #include <fstream> #include <iostream> #include <string> #include <Windows.h> using std::string; using std::cout; using std::cin; using std::ifstream; using std::ofstream; int main() { string line; ifstream example; example.open("Name.txt"); getline(example, line); if (line.compare("") == 0) { example.close(); string con; cout << "Welcome to this program!\n"; cout << "Do you want to register? (y/n) "; cin >> con; con[0] = tolower(con[0]); if (con.compare("n") != 0) { string name; ofstream one; one.open("Name.txt"); cout << "What's your name? "; cin >> name; one << name; one.close(); cout << "See you later " << name << "."; Sleep(4000); } } else { cout << "Welcome back " << line << "."; example.close(); Sleep(4000); } } 基于&#39; dt&#39;

levels
order

根据例子,&#39; dt&#39;已经with(g, fct_relevel(var, levels(var)[order(dt)])) #[1] C B A #Levels: A B C 编辑,因此不会更改&{39; var&#39; order的{​​{1}}。