- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个很长的数据框
mydf <- data.frame(
+ date=c("2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01"),
+ value=c(1,2,3,4,5,1,2,3,4,5),
+ country=c("US", "US", "US", "US", "US", "US", "US", "US", "US", "US"),
+ indicator=c("gdp", "gdp", "gdp", "gdp", "gdp", "population", "population", "population", "population", "population"))
date value country indicator
1 2016-01-01 1 US gdp
2 2016-02-01 2 US gdp
3 2016-03-01 3 US gdp
4 2016-04-01 4 US gdp
5 2016-05-01 5 US gdp
6 2016-02-01 1 US population
7 2016-03-01 2 US population
8 2016-04-01 3 US population
9 2016-05-01 4 US population
10 2016-06-01 5 US population
我想创建来自比率的具体新指标,例如GDP/人口*1000
它看起来像这样,它必须与每个相应指标的正确日期相匹配
mydf <- data.frame(
+ date=c("2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01"),
+ value=c(1,2,3,4,5,1,2,3,4,5,2,1.5,1.33,1.2),
+ country=c("US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US"),
+ indicator=c("gdp", "gdp", "gdp", "gdp", "gdp", "population", "population", "population", "population", "population", "gdp per capita", "gdp per capita", "gdp per capita", "gdp per capita"))
date value country indicator
1 2016-01-01 1.00 US gdp
2 2016-02-01 2.00 US gdp
3 2016-03-01 3.00 US gdp
4 2016-04-01 4.00 US gdp
5 2016-05-01 5.00 US gdp
6 2016-02-01 1.00 US population
7 2016-03-01 2.00 US population
8 2016-04-01 3.00 US population
9 2016-05-01 4.00 US population
10 2016-06-01 5.00 US population
11 2016-02-01 2.00 US gdp per capita
12 2016-03-01 1.50 US gdp per capita
13 2016-04-01 1.33 US gdp per capita
14 2016-05-01 1.20 US gdp per capita
在 R 中是否有一种简单的方法可以做到这一点?
最佳答案
我个人发现 reshape 包更容易使用,并且它会自动处理多个国家/地区/无论您拥有多少类型的标签/数据类型。
library(reshape)
mydf <- data.frame(
date=c("2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01",
"2016-06-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01","2016-05-01"),
value=c(1,2,3,4,5,1,2,3,4,5,2,1.5,1.33,1.2, 2),
country=c("US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", "US", 'AU'),
indicator=c("gdp", "gdp", "gdp", "gdp", "gdp", "population", "population", "population",
"population", "population", "gdp per capita", "gdp per capita", "gdp per capita", "gdp per capita", 'gdp'))
要获取新指标,首先将数据设置为宽格式,以便相关列彼此相邻。这样您就可以进行简单的按列操作
df_wide = cast(mydf, date+country~indicator, sum)
您希望将国家/地区和日期作为唯一定义行的列(公式左侧),并将不同的指示符作为列(公式右侧)
date country gdp gdp per capita population
1 2016-01-01 US 1 0.00 0
2 2016-02-01 US 2 2.00 1
3 2016-03-01 US 3 1.50 2
4 2016-04-01 US 4 1.33 3
5 2016-05-01 AU 2 0.00 0
6 2016-05-01 US 5 1.20 4
7 2016-06-01 US 0 0.00 5
现在创建一个新列并将其设置为您想要的任何内容
df_wide['g_p_ratio'] = df_wide['gdp'] / df_wide['population']
然后使用 Melt 将其恢复为长格式
df_new = melt(df_wide, id=c('date'))
瞧!
date country value indicator
gdp 2016-01-01 US 1.00 gdp
gdp.1 2016-02-01 US 2.00 gdp
gdp.2 2016-03-01 US 3.00 gdp
gdp.3 2016-04-01 US 4.00 gdp
gdp.4 2016-05-01 AU 2.00 gdp
gdp.5 2016-05-01 US 5.00 gdp
gdp.6 2016-06-01 US 0.00 gdp
gdp.per.capita 2016-01-01 US 0.00 gdp per capita
gdp.per.capita.1 2016-02-01 US 2.00 gdp per capita
gdp.per.capita.2 2016-03-01 US 1.50 gdp per capita
gdp.per.capita.3 2016-04-01 US 1.33 gdp per capita
gdp.per.capita.4 2016-05-01 AU 0.00 gdp per capita
gdp.per.capita.5 2016-05-01 US 1.20 gdp per capita
gdp.per.capita.6 2016-06-01 US 0.00 gdp per capita
population 2016-01-01 US 0.00 population
population.1 2016-02-01 US 1.00 population
population.2 2016-03-01 US 2.00 population
population.3 2016-04-01 US 3.00 population
population.4 2016-05-01 AU 0.00 population
population.5 2016-05-01 US 4.00 population
population.6 2016-06-01 US 5.00 population
您可能需要也可能不需要新的行标签,但您可以解决这个问题
rownames(df_new) <- 1:nrow(df_new)
关于r - 在长数据中创建新的比率指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47667022/
我不太确定如何制作它,因此当在屏幕上绘制矩形时,它有可能是金色的。这是我的游戏随机生成随机矩形的当前代码: public void drawRectangle() { rects.clear(
我知道这个问题可能是简单的几何问题,但我正在为一个地理区域使用特定的 shapefile 并寻找特定的 gis 解决方案(希望是代码)。 本质上,我想找到覆盖每个独立地理边界的黑色区域。覆盖红色区域、
我有一个 wordpress 网站,我有 5 个低文本 HTML 比率的页面,这是用于类别页面、作者页面和主页。我删除了所有不必要的插件以减少代码,它去掉了一个(我之前有 6 个)。我知道删除代码行之
这个问题已经有答案了: Division of integers in Java [duplicate] (7 个回答) 已关闭 9 年前。 我的代码: public class Test { pub
我希望在缩小时保持视频嵌入 (iframe) 的尺寸不变。 此时会出现两条黑线,并且随着您将其缩小而变大。 示例:http://jsfiddle.net/k5nbmqau/ 最简单的方法是什么?我想在
我正在尝试创建一个“幻灯片”div,它以恒定的纵横比在屏幕中间居中。 结合这个trick用于居中和this one对于比率,我想出了这个: HTML Percentage
我正在寻找一个提供统计数据/图表的网站,该网站提供 Android 智能手机和平板电脑上最流行的分辨率/屏幕比例。需要它来设计 UI 并计划如何扩展。如果有人共享数据会很酷。 最佳答案 这是您要找的吗
在我的应用中,我允许用户以纵向模式录制视频。我在应用程序中使用它,这很好。但是,我正在实现一项功能以在 Instagram 上分享视频,其中视频必须是方形的。 将视频裁剪成正方形,比方说中间部分,是一
我需要从两台服务器创建“可靠性”报告 - 服务器 A 和服务器 B。 两者都跟踪其正常运行时间 - 或者更确切地说,在它们不可用的任何情况下。这些记录存储在 SQL Server 数据库表中 Even
您好,我想更改我的/etc/my.cnf 文件(mysql 的配置文件)。 以下值应该是多少才能使我的查询获得更好的性能。 query_cache_type = 1 query_cache_limit
目前我有一个布局,可以将一些缩略图拉入一个 grid - 每个缩略图都由一种样式定义,该样式使它们保持固定比例(大约 16:9),该比例由以下定义像素尺寸 (389px x 230px),但它们在高分
我有一个 wxPython 应用程序,带有一个框架和一个面板。该面板上有许多静态框,每个静态框都有按钮和文本框。 我刚刚开始阅读有关 sizer 的内容,但它们似乎可能超出了我的需要,或者它们可能正是
假设我正在构建一个基本的搜索引擎。我有一个字符串列表作为搜索结果,我想对搜索结果列表进行排序,最匹配的结果排在最前面。 我当前的代码如下所示(以命名参数为例) import difflib def o
我正在为一组物种生成生态位模型,我想使用 AUC 作为生态位质量的指标。开发 Maxent 的 Steven Phillips 在他的 Maxent 手册中提供了用于计算 R 中 AUC 的代码。但是
我在 LaunchScreen 中有一个 UIView。 我已将以下约束添加到此 UIView。 我希望我的红色 View 始终占屏幕的 35%。 我计算了 RedView 的高度: 我的 ViewC
我有一个包含列的数据框:Year 和 Min Delay。示例行如下: 2014 0 2014 2 2014 0 2014 4 2015 4 2015 4 2015
我需要 CSS/响应式代码方面的帮助。随着我的窗口大小减小,我需要所有元素以相同的比例减小。对此有疑问。箭头和公鸡不会随着视口(viewport)减小而减小。 公鸡脚的影子应该稍微被箭遮住。箭头主体(
我想创建 roofline 模型,但我遇到了每字节比率算法失败的问题。你能解释一下如何计算吗?该算法使用 5 点模板进行计算。 这是算法 for(int i=1; i
我有一个数据框,其中单元格用 float 填充,列名采用日期时间格式,格式为年+季度 - 例如“1995Q2”。我想划分其中两个列的值并将结果存储在一个新列中。我这样做了: df['ratio'] =
我有一个 8GB RAM 的生产服务器。我希望在服务器上托管 elastic、logstash 和 kibana。使用 docker compose。 每个容器的推荐 java 大小内存大小是多少。我
我是一名优秀的程序员,十分优秀!