R pivot_更长,将第一列行名转换为列名
使用这些数据: 第一列包含行: 自杀人数 男性的 女的 全部的 每100000人的年龄标准化死亡率 男性的 女的 总数 但是,我需要这些作为列标题。这是通过pivot_实现的吗R pivot_更长,将第一列行名转换为列名,r,tidyr,R,Tidyr,使用这些数据: 第一列包含行: 自杀人数 男性的 女的 全部的 每100000人的年龄标准化死亡率 男性的 女的 总数 但是,我需要这些作为列标题。这是通过pivot_实现的吗 谢谢这是一个非常难看的数据文件,但希望它能满足您的需要: library(dplyr) library(tidyr) # After downloading the file in your project folder dat <- readxl::read_excel("suicide-2015-tables
谢谢这是一个非常难看的数据文件,但希望它能满足您的需要:
library(dplyr)
library(tidyr)
# After downloading the file in your project folder
dat <- readxl::read_excel("suicide-2015-tables.xlsx", skip = 2)
dat %>%
select(variables = ...1, `2006`:`2015`) %>% # Remove unneeded/blank columns
mutate(headers = if_else(is.na(`2006`), variables, NA_character_)) %>% # Create a headers variable
fill(headers, .direction = "down") %>% # Fill the headers down
pivot_longer(`2006`:`2015`, names_to = "year", values_to = "counts") %>% # Reshape data from wide to long
drop_na() %>%
unite("headers_vars", headers, variables, sep = " - ") %>% # Create a new variable that combines the headers and the subgroup breakdown
pivot_wider(names_from = headers_vars, values_from = counts) # Reshape back from long to wide
# A tibble: 10 x 15
year `Number of suic~ `Number of suic~ `Number of suic~ `Age-standardis~ `Age-standardis~ `Age-standardis~ `Age-specific r~
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2006 388 138 526 18.6 6.25 12.2 19.7
2 2007 371 116 487 17.4 5.01 11.0 15.2
3 2008 381 139 520 17.6 6.24 11.8 19.5
4 2009 393 117 510 17.9 5.03 11.3 18.1
5 2010 386 149 535 17.3 6.63 11.8 17.7
6 2011 377 116 493 17.0 5.06 10.9 20.1
7 2012 404 146 550 18.1 6.39 12.1 23.0
8 2013 367 147 514 16.0 6.41 11.0 17.8
9 2014 380 130 510 16.5 5.4 10.8 14.1
10 2015 384 143 527 16.4 6.1 11.1 16.9
# ... with 7 more variables: `Age-specific rates by life-stage age group (deaths per 100,000) - 25–44` <dbl>, `Age-specific rates by
# life-stage age group (deaths per 100,000) - 45–64` <dbl>, `Age-specific rates by life-stage age group (deaths per 100,000) - 65+` <dbl>,
# `Age-standardised suicide rates for Māori (deaths per 100,000) - Male` <dbl>, `Age-standardised suicide rates for Māori (deaths per
# 100,000) - Female` <dbl>, `Age-standardised suicide rates for non-Māori (deaths per 100,000) - Male` <dbl>, `Age-standardised suicide
# rates for non-Māori (deaths per 100,000) - Female` <dbl>