如何在R中将一行转换为多列

如何在R中将一行转换为多列,r,data.table,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) 我从其他问题中找到了类似的答案。我模仿的代码的优点如下,但我无法对“月”列进行编码 我非常不懂,我可能需

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

"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)
我从其他问题中找到了类似的答案。我模仿的代码的优点如下,但我无法对“月”列进行编码

我非常不懂,我可能需要一个详细的解释。谢谢


谢谢@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.570          
2: 2001    02    138.50   -16.50 32767.000       
3: 2001    02    139.50   -16.50 32767.000       

如果时间戳的格式与您所显示的一样,您只需将其
拆分
并使用
索引
即可获得年份和月份:

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

一个选项是将其转换为
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

谢谢@Psidom。我对“经度”、“纬度”和“TWC”列的滑动有问题。这是我的密码<代码>>Llibrary(data.table)>DT[,c('Year','Month'):=tstrsplit(grep('HDR TIME_-YMD=',txt,value=TRUE),“=|-”[2:3]]]>DT[,c('Longitude','Latitude','TWC'):=tstrsplit(txt,\\s+{3}',type.convert=TRUE)][]>DT c('txt'):=NULL]谢谢@akrun,我将很快检查您的解决方案。对于
经度
纬度
部分,为什么要指定
\\s+{3}
作为分隔符?你想谈什么?这就是重点。我不知道,只是模仿其他的解决方案。@Drolatiatus Maximus你对解决方案有什么建议吗?我试过了,但是>DT[,c('Longitude','lation','TWC'):=tstrsplit(txt,\\s+{3}',type.convert=TRUE)]对我不起作用。“\\s+{2}”代表什么?
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 = 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
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"))