R读取缩写的月份不是英文的日期 [英] R Read abbreviated month form a date that is not in English

查看:1026
本文介绍了R读取缩写的月份不是英文的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期的文本文件,并希望将其转换为数据表。

I have a text file that includes dates, and want to convert it to a datatable.

转换日期如03-FEB-2011可以用

Converting the dates such as 03-FEB-2011 can be done with

data$fecha <- as.Date(data$textDate , "%d-%b-%Y")

问题是该列是西班牙语,所以我不会得到Jan但Ene或Aug但是阿戈。
如何更改语言环境,以便使用%b缩写适用于西班牙语?有没有其他方法来实现?

The problem is that the column is in Spanish, so I don't get Jan but Ene, or Aug but Ago. How can I change the locale so the %b abbreviation works for Spanish? Is there any other way to achieve this?

推荐答案

作为我以前的评论,这里是一个完整和经过测试的答案。正如我所说,你必须将你的 locale 设置为你的数据(在这种情况下是西班牙语)。

As my previous comment, here is a complete and tested answer. As I've said you have to set your locale to the one right for your data (in this case spanish).

允许您执行此操作的代码如下:

The code that allows you to do that is the following:

Sys.setlocale(locale="es_ES.UTF-8")

您可以看到可用的$ code> locale的完整列表 s与 system(locale -a,intern = TRUE)(不知道它是否在Windows系统上运行良好)。

you can see the complete list of available locales with system("locale -a", intern = TRUE) (not sure if it works well on Windows systems).

这是一个例子:

x <- c("03-Ago-2011", "21-Ene-2012")
as.Date(x, format = "%d-%b-%Y")
[1] "2011-08-03" "2012-01-21"

这篇关于R读取缩写的月份不是英文的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆