R 如何按列索引将数据帧拆分为多个数据帧
我正在尝试处理下面指定的天气数据。我原以为我在正确的轨道上,但pivot_不再在正确的庄园中使用,导致部分重复 有人能就我如何编辑代码提出建议吗?我想一种方法是在将数据帧拆分为多个数据帧(即第一个数据帧-1月,年,第二个数据帧-2月,年)后执行pivot_的时间更长R 如何按列索引将数据帧拆分为多个数据帧,r,R,我正在尝试处理下面指定的天气数据。我原以为我在正确的轨道上,但pivot_不再在正确的庄园中使用,导致部分重复 有人能就我如何编辑代码提出建议吗?我想一种方法是在将数据帧拆分为多个数据帧(即第一个数据帧-1月,年,第二个数据帧-2月,年)后执行pivot_的时间更长 maxT <- read.table('https://www.metoffice.gov.uk/pub/data/weather/uk/climate/datasets/Tmax/ranked/England_S.txt',
maxT <- read.table('https://www.metoffice.gov.uk/pub/data/weather/uk/climate/datasets/Tmax/ranked/England_S.txt', skip = 5, header = TRUE) %>%
select(c(1:24)) %>%
pivot_longer(cols = seq(2,24,2) , values_to = "year") %>%
mutate_at(c(1:12), ~as.numeric(as.character(.))) %>%
pivot_longer(cols = c(1:12), names_to = "month", values_to = "tmax") %>%
mutate(month = match(str_to_title(month), month.abb),
date = as.Date(paste(year, month, 1, sep = "-"), format = "%Y-%m-%d")) %>%
select(-c("name","year","month")) %>%
arrange(date)
maxT%
选择(c(1:24))%>%
枢轴长度(cols=seq(2,24,2),数值为年)%>%
在(c(1:12),~as.numeric(as.character(.))%>%处进行变异
pivot_更长(cols=c(1:12),名称_to=“month”,值_to=“tmax”)%>%
变异(月=匹配(str_to_title(月),month.abb),
日期=截止日期(粘贴(年、月、1、9月=“-”,格式=“%Y-%m-%d”))%>%
选择(-c(“名称”、“年”、“月”))%>%
安排(日期)
我们可以使用拆分。默认情况下,
拆分两列组成的组
list_df <- split.default(maxT, ceiling(seq_along(maxT)/2))
list\u df这里有一个使用tidyverse
的选项,使用map2
library(dplyr)
library(purrr)
list_df <- maxT %>%
select(seq(1, ncol(.), by = 2)) %>%
map2(maxT %>%
select(seq(2, ncol(.), by = 2)), bind_cols) %>%
imap( ~ .x %>%
rename(!! .y := `...1`, year = `...2`))
库(dplyr)
图书馆(purrr)
列表_df%
选择(序号(1,ncol(.),by=2))%>%
map2(最大%>%
选择(序号(2,ncol(.),by=2)),绑定列%>%
imap(~.x%>%
重命名(!!.y:=`…1`,年份=`…2`)
-输出
map(list_df, head)
#$jan
# A tibble: 6 x 2
# jan year
# <dbl> <int>
#1 9.9 1916
#2 9.8 2007
#3 9.7 1921
#4 9.7 2008
#5 9.5 1990
#6 9.4 1975
#$feb
# A tibble: 6 x 2
# feb year
# <dbl> <int>
#1 11.2 2019
#2 10.7 1998
#3 10.7 1990
#4 10.3 2002
#5 10.3 1945
#6 10 2020
# ...
map(列表头)
#$jan
#一个tibble:6x2
#一月
#
#1 9.9 1916
#2 9.8 2007
#3 9.7 1921
#4 9.7 2008
#5 9.5 1990
#6 9.4 1975
#二月美元
#一个tibble:6x2
#二月份
#
#1 11.2 2019
#2 10.7 1998
#3 10.7 1990
#4 10.3 2002
#5 10.3 1945
#6 10 2020
# ...
数据
maxT%
选择(c(1:24))
map(list_df, head)
#$jan
# A tibble: 6 x 2
# jan year
# <dbl> <int>
#1 9.9 1916
#2 9.8 2007
#3 9.7 1921
#4 9.7 2008
#5 9.5 1990
#6 9.4 1975
#$feb
# A tibble: 6 x 2
# feb year
# <dbl> <int>
#1 11.2 2019
#2 10.7 1998
#3 10.7 1990
#4 10.3 2002
#5 10.3 1945
#6 10 2020
# ...
maxT <- read.table('https://www.metoffice.gov.uk/pub/data/weather/uk/climate/datasets/Tmax/ranked/England_S.txt', skip = 5, header = TRUE) %>%
select(c(1:24))