如何在R

时间:2016-09-09 20:37:08

标签: r data.table

我有很多文本文件,所有这些文件都包含一行以相同的注释开头。

"HDR TIME_YMD=2001-02-16 T 00:00:00"
"HDR TIME_YMD=2001-03-18 T 00:00:00" 

我希望能够将该行更改为两列并创建一个数据框,如下所示。

Year    Month
2001    02 (or February)
2001    03 (or March)

我从其他问题中找到了类似的答案。有Extract data between a pattern from a text file in R的好处,我模仿的代码如下,但我无法编码"月"柱。

DT <- DT[, `:=` (Year = as.numeric(gsub('^.*(\\d+{4}).*','\\1', 
                        grep('HDR TIME_YMD=', txt, value = TRUE))), Month=????

我正在寻找的另一个答案;

我想将数据转换为三个数据列'Longitude','Latitude','TWC'

  Year  Month  Longitude  Latitude     TWC
1 2001  02         130.5    -16.5     6.935
2 2001  02         131.5    -16.5    13.912
3 2001  03         132.5    -16.5    13.244
4 2001  03         133.5    -16.5    15.556
5 2001  03         134.5    -16.5    21.380

我非常喜欢R,我可能需要详细解释。谢谢!

谢谢@Psidom。我在滑动的列,“经度”,“纬度”,“TWC”等问题上遇到了麻烦。这是我的代码。

L<-readLines("Document1.txt")
library(data.table)
DT <- data.table(txt = L[!grepl(pattern = '\\*+', L)])
DT[, c('Year', 'Month') := tstrsplit(grep('HDR TIME_YMD=', txt, value = TRUE), "=|-")[2:3]]
DT <- DT[, .SD[20:.N]][]
DT[, c('Longitude','Latitude','TWC') := tstrsplit(txt, '\\s+{3}', type.convert = TRUE)][]
DT[, c('txt') := NULL][]

它给了我这个解决方案。

   Year Month               Longitude Latitude                      TWC
1: 2001    02            137.50 -16.50    18.57            137.50 -16.50
2: 2001    02  138.50 -16.50 32767.000       NA  138.50 -16.50 32767.000
3: 2001    02  139.50 -16.50 32767.000       NA  139.50 -16.50 32767.000

我希望他们看起来像这样。

   Year Month Longitude Latitude       TWC
1: 2001    02    137.50   -16.50    18.570          
2: 2001    02    138.50   -16.50 32767.000       
3: 2001    02    139.50   -16.50 32767.000       

2 个答案:

答案 0 :(得分:2)

如果时间戳的格式与您显示的一样,您可以split并使用index获取年份和月份:

library(data.table)
dt[, c("Year", "Month") := tstrsplit(TimeStamp, "=|-")[2:3]]
dt
#                            TimeStamp Year Month
#1: HDR TIME_YMD=2001-02-16 T 00:00:00 2001    02
#2: HDR TIME_YMD=2001-02-16 T 00:00:00 2001    02
#3: HDR TIME_YMD=2001-02-16 T 00:00:00 2001    02

dt的位置:

dt = data.table(TimeStamp = c("HDR TIME_YMD=2001-02-16 T 00:00:00", 
                              "HDR TIME_YMD=2001-02-16 T 00:00:00", 
                              "HDR TIME_YMD=2001-02-16 T 00:00:00"))
dt
#                             TimeStamp
# 1: HDR TIME_YMD=2001-02-16 T 00:00:00
# 2: HDR TIME_YMD=2001-02-16 T 00:00:00
# 3: HDR TIME_YMD=2001-02-16 T 00:00:00

答案 1 :(得分:0)

一个选项是将其转换为DateTime类,然后提取组件

library(lubridate)
dt[, c("Year", "Month") := {t1 <- ymd_hms(TimeStamp); .(year(t1), month(t1))}]
dt
#                            TimeStamp Year Month
#1: HDR TIME_YMD=2001-02-16 T 00:00:00 2001     2
#2: HDR TIME_YMD=2001-02-16 T 00:00:00 2001     2
#3: HDR TIME_YMD=2001-02-16 T 00:00:00 2001     2

数据

dt = data.table(TimeStamp = c("HDR TIME_YMD=2001-02-16 T 00:00:00", 
                          "HDR TIME_YMD=2001-02-16 T 00:00:00", 
                          "HDR TIME_YMD=2001-02-16 T 00:00:00"))
相关问题