Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为r中的时间序列分析重新安排不愉快的实际工作数据帧_R - Fatal编程技术网

为r中的时间序列分析重新安排不愉快的实际工作数据帧

为r中的时间序列分析重新安排不愉快的实际工作数据帧,r,R,这里有一个时间序列分析的理想输入数据示例: 但是,我收到的原始数据如下: raw\u data我们可以使用pivot\u long将数据重新格式化为“long”格式,然后使用unite连接列 library(dplyr) library(tidyr) library(lubridate) raw_data %>% pivot_longer(cols = matches('^[0-9]'), names_to = 'Time') %>% unite(DateTime, d

这里有一个时间序列分析的理想输入数据示例:

但是,我收到的原始数据如下:


raw\u data我们可以使用
pivot\u long
将数据重新格式化为“long”格式,然后使用
unite
连接列

library(dplyr)
library(tidyr)
library(lubridate)
raw_data %>%
  pivot_longer(cols = matches('^[0-9]'), names_to = 'Time') %>% 
  unite(DateTime, date, Time, sep=" ") %>%
  mutate(DateTime = ymd_hm(DateTime))
# A tibble: 12 x 3
#   site  DateTime            value
#   <chr> <dttm>              <dbl>
# 1 A     2015-01-01 00:00:00     1
# 2 A     2015-01-01 01:00:00     2
# 3 A     2015-01-01 02:00:00     3
# 4 B     2015-01-01 00:00:00     4
# 5 B     2015-01-01 01:00:00     5
# 6 B     2015-01-01 02:00:00     6
# 7 A     2015-01-02 00:00:00     1
# 8 A     2015-01-02 01:00:00     2
# 9 A     2015-01-02 02:00:00     3
#10 B     2015-01-02 00:00:00     4
#11 B     2015-01-02 01:00:00     5
#12 B     2015-01-02 02:00:00     6
库(dplyr)
图书馆(tidyr)
图书馆(lubridate)
原始数据%>%
pivot_longer(cols=matches(“^[0-9]”),name_to='Time')%>%
联合(日期时间、日期、时间、九月=”)%>%
变异(DateTime=ymd_hm(DateTime))
#一个tibble:12x3
#站点日期时间值
#                  
#1A 2015-01-01 00:00:00 1
#2A 2015-01-01:00:00 2
#3A 2015-01-01 02:00:00 3
#4B 2015-01-01 00:00:00 4
#5b 2015-01-01:00:00 5
#6b 2015-01-01 02:00:00 6
#7 A 2015-01-02 00:00:00 1
#8a 2015-01-02 01:00:00 2
#9a 2015-01-02 02:00:00 3
#10B 2015-01-02 00:00:00 4
#11 B 2015-01-02 01:00:00 5
#12 B 2015-01-02 02:00:00 6

您可以使用
数据中的
melt
执行此操作。表
包:

library(data.table)

# Mark the data as a data.table
setDT(raw_data)

# Melt it into long format
new_data <- melt(raw_data, id.vars=c('site', 'date'), variable.name='time')

# Put date and time together into a new column, and delete the old ones
new_data[, `:=`(DateTime = paste(date, time),
                date = NULL, time = NULL)]
库(data.table)
#将数据标记为data.table
setDT(原始数据)
#把它融化成长格式
新数据
library(data.table)

# Mark the data as a data.table
setDT(raw_data)

# Melt it into long format
new_data <- melt(raw_data, id.vars=c('site', 'date'), variable.name='time')

# Put date and time together into a new column, and delete the old ones
new_data[, `:=`(DateTime = paste(date, time),
                date = NULL, time = NULL)]