我有一个数据框-其中一列的日期具有两种不同的格式(d-m-Y和d / m / Y),并且还包含很多NA。该列属于字符类。我想将日期格式更改为一种格式(Y-m-d),同时保留原样的NA。您能帮忙-我该如何在R中做到这一点?
答案 0 :(得分:2)
如果日期顺序相同,请使用dmy
中的lubridate
library(lubridate)
dmy(df1$Col)
#[1] "2018-05-25" "2018-05-25"
如果订单也不同,请使用parse_date_time
parse_date_time(df2$Col, c("dmy", "mdy"))
#[1] "2018-05-25 UTC" "2018-05-25 UTC" "2018-05-25 UTC"
df1 <- data.frame(Col = c("25-05-2018", "25/05/2018"), stringsAsFactors = FALSE)
df2 <- data.frame(Col = c("25-05-2018", "25/05/2018", "5/25/2018"),
stringsAsFactors = FALSE)
答案 1 :(得分:0)
您可以将“-”替换为“ /”(或反之),然后使用as.Date:
public function login() {
//print_r($_POST);die;
$this->load->library('session');
$this->load->library('form_validation');
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required');
$this->form_validation->set_rules('pw', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE) {
if (isset($_POST) && ($_POST['login'] == 'Login')) {
$data['login_errors'] = validation_errors();
$this->load->view('Public/register', $data);
}
} else {
$this->load->database();
$where = '';
$where = array('email' => $_POST['user_name'], 'password' => $_POST['pw']);
$rec = $this->db->select('*')->from('user')
->where($where)
->get()->result_array();
if(!empty($rec)){
$this->session->set_userdata('user',$rec);
$this->load->view('Public/profile');
}else{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('Public/register', $data);
}
}
}