- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如标题所说,我想将自由之家索引从 excel 转换为 R 中的整洁格式。FHI 可以在 https://freedomhouse.org/reports/publication-archives 下下载然后是国家和地区评级和状态,1973-2021,看起来像这样:
FHI in excel
我已经使用以下代码完成了它,但我认为我的解决方案不是很优雅,更像是分解和组装。所以我正在寻找另一种解决方案,充其量是在 tidyverse 中。提前致谢。
#load packages
library(tidyverse)
#load data
library(readxl)
fh <- read_excel("Data/Country_and_Territory_Ratings_and_Statuses_FIW1973-2021.xlsx",
sheet = "Country Ratings, Statuses ",
col_names = FALSE, na = "-")
# remove survey edition and years
fh_raw <- fh %>%
filter(...1 != c("Survey Edition",
"Year(s) Under Review"))
# save country names
cty <- unlist(fh_raw[1]) %>%
unname()
fh_raw <- fh_raw %>%
select(!...1)
# variable
pr <- seq(to = length(fh_raw), by = 3)
cl <- seq(from = 2, to = length(fh_raw), by = 3)
status <- seq(from = 3, to = length(fh_raw), by = 3)
# select variables and transform into long-format
fh_pr <- fh_raw[pr] %>%
pivot_longer(cols = 1:length(pr))
fh_pr <- unlist(fh_pr[2]) %>%
unname() %>% as.numeric()
fh_cl <- fh_raw[cl] %>%
pivot_longer(cols = 1:length(cl))
fh_cl <- unlist(fh_cl[2]) %>%
unname() %>% as.numeric()
fh_status <- fh_raw[status] %>%
pivot_longer(cols = 1:length(status))
fh_status <- unlist(fh_status[2]) %>%
unname()
cty <- rep(cty, each = length(cl))
year = 1972:2020
year <- rep(year[year != 1981], times = 205) # 1981 is skipped
#create FH data frame
fh_long <- tibble(country = cty,
year = year,
pr = fh_pr,
cl = fh_cl,
status = fh_status)
最佳答案
进入tidyxl
的魔法世界和 unpivotr
;-)
library(tidyverse)
library(tidyxl)
library(unpivotr)
file.to.read <- "./Country_and_Territory_Ratings_and_Statuses_FIW1973-2021.xlsx"
sheet.to.read <- "Country Ratings, Statuses "
#read sheet's contents (take a look at it to see what you actrually just read in)
cells <- tidyxl::xlsx_cells( file.to.read, sheet = sheet.to.read)
ans <- cells %>%
# Drop empty cells
dplyr::filter(!is_blank) %>%
# Setup headers from top and left side
unpivotr::behead("up", "Survey_Edition") %>%
unpivotr::behead("up", "year") %>%
unpivotr::behead("up", "item") %>%
unpivotr::behead("left", "country") %>%
# There are unwanted training spaces in the item-clum, remove them
dplyr::mutate(item = trimws(item)) %>%
# Get value from numeric and character-column
dplyr::mutate(value = ifelse(item == "Status", character, numeric)) %>%
# Drop unneeded data
dplyr::select(country, year, item, value) %>%
# Fill down missing year info
tidyr::fill(year, .direction = "down") %>%
# Cast to wide format
tidyr::pivot_wider(names_from = item, values_from = value)
输出
# country year PR CL Status
# <chr> <chr> <chr> <chr> <chr>
# 1 Afghanistan 1972 4 5 PF
# 2 Afghanistan 1973 7 6 NF
# 3 Afghanistan 1974 7 6 NF
# 4 Afghanistan 1975 7 6 NF
# 5 Afghanistan 1976 7 6 NF
# 6 Afghanistan 1977 6 6 NF
# 7 Afghanistan 1978 7 7 NF
# 8 Afghanistan 1979 7 7 NF
# 9 Afghanistan 1980 7 7 NF
#10 Afghanistan Jan.1981-Aug. 1982 7 7 NF
#11 Afghanistan Aug.1982-Nov.1983 7 7 NF
#12 Afghanistan Nov.1983-Nov.1984 7 7 NF
#13 Afghanistan Nov.1984-Nov.1985 7 7 NF
#14 Afghanistan Nov.1985-Nov.1986 7 7 NF
#15 Afghanistan Nov.1986-Nov.1987 7 7 NF
#16 Afghanistan Nov.1987-Nov.1988 6 6 NF
#17 Afghanistan Nov.1988-Dec.1989 7 7 NF
#18 Afghanistan 1990 7 7 NF
#19 Afghanistan 1991 7 7 NF
#20 Afghanistan 1992 6 6 NF
# ...
关于r - 在 R 中将自由之家索引转换为整洁的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67683442/
我正在使用这种代码: document.write("foo 使用 HTML tidy 后,表格外的 script 标签被移除,因此破坏了页面布局。我
我正在为我的网格系统使用 Bourbon 的 Neat 库。 我有这样的代码: section { @include outer-container; aside { @include spa
我有三个文件。 header.php index.php footer.php 头文件包含来自至 索引文件包含页面内容页脚文件包含 至 它们一起包含一个带有 PHP 的普通 HTML 文件 当我使用
我有一个格式有点乱的 Objective-C 代码库。有没有办法让 Xcode 重新格式化整个项目以符合编码标准(即正确缩进、空格与制表符等)?是否有其他工具可以完成此任务? 最佳答案 去壳化:htt
我试图自己实现整洁,使用原始论文但被卡住了。 假设在上一代我有以下物种: Specie 1: members: 100 avg_score: 100 Specie 2: memb
我正在尝试整理我的一些 SKScene 代码。目前我有大约 11 个对 SKNode 的引用(有些是包含子节点的层)。这些节点及其子节点被类频繁访问。我考虑这样做的方式是: 将所有 SKNode 子类
Notepad++ 的 HTML Tidy 坏了吗?除了 Tidy(第一个)之外,所有命令都不起作用。他们不显示任何消息,即使选择了所有文本。我真的需要 Tidy 才能工作,还是它只是最新版本 N++
有没有一种方法可以不使用 rowwise() 来创建 key? 非常感谢任何指针。 df % rowwise %>% mutate(key=paste(sort(c(grp1, grp2)), col
我正在尝试使用作为 PHP (http://www.php.net/manual/en/book.tidy.php) 一部分的 HTML Tidy 实现来重新格式化大量 HTML。我遇到了一个问题,其
我为 Sublime Text 2 安装了 phptidy 插件,并尝试用它来清理一些丑陋的代码,比如 $tdt="
我在 Windows 的命令行环境中使用 HTML Tidy。我需要强制将一些 html 文件转换为 xml,即使有错误也是如此。 我执行以下步骤: 创建文件“conf.txt”,其内容为: 强制输出
我正在重写一个使用 Bourbon 的“旧”React 原型(prototype),它还在 gulpfile 中使用 gulp-sass 来注入(inject)节点整洁的依赖项: var sassOp
我正在创建一个供个人使用的 jQuery Accordion 插件。 我的主要目标是拥有 super 简洁的 JS 代码和 HTML 结构。 这就是我已经走了多远 http://jsfiddle.ne
我正在测试 Bourbon Neat,我在一个外容器中有两列,我希望这些列的高度相等(与最高的列一样高)。在短列上使用 @include fill-parent 不起作用,它只会使它与外部容器一样宽。
大多数时候在 repos 中,我们看到一个 PR,然后是那个 PR 的 merge 提交,它只是说“Merged pull request #XXX from ...”。 但最近,我看到了一个紧凑的版
我正在使用 Neat 的 12 列网格。该页面由延伸整个网格宽度的部分组成。部分背景与页面背景不同: 如您所见,粉红色部分的左侧与网格边缘齐平。我想要的是该部分的左侧超出网格几个雷姆。 但是,如果我添
只是出于好奇而提出的简单问题。 类上的多个方法需要使用字符串流,或者特别是 ostringstream。 1) 有一个 stringstream 变量作为类成员,然后在使用它之前清除它,即 msg.s
我是波旁/整洁的新手。我有一个关于嵌套的问题:我希望红色框填充整个宽度,而彼此之间不要有排水沟。当在其上使用“@include omega”时,第一个框将删除其右边距,但是右边的框仍具有边距,并且不会
GWT(Google Web Toolkit)是否有一个功能可以漂亮地打印小部件的 html 输出? (如果问题措辞不当,我深表歉意——我不是 GWT 开发人员,但我们的开发人员声称没有办法做到这一点
我是一名优秀的程序员,十分优秀!