使用dplyr汇总数据集,我想调用n_distinct来计算列中唯一出现次数.但是,我还想对列中满足另一列条件的所有唯一事件进行另一次汇总().
名为"a"的示例数据帧:
A B
1 Y
2 N
3 Y
1 Y
Run Code Online (Sandbox Code Playgroud)
a %>% summarise(count = n_distinct(A))
不过,我也想添加的数量n_distinct(A),其中B == "Y"
结果应该是:
count
3
Run Code Online (Sandbox Code Playgroud)
添加条件时,结果应为:
count
2
Run Code Online (Sandbox Code Playgroud)
我试图实现的最终结果是两个语句合并为一个调用,给我一个结果
count_all count_BisY
3 2
Run Code Online (Sandbox Code Playgroud)
使用dplyr进行此操作的适当方法是什么?
Jest提供了一种模拟函数的方法,如文档中所述
apiGetMethod = jest.fn().mockImplementation(
new Promise((resolve, reject) => {
const userID = parseInt(url.substr('/users/'.length), 10);
process.nextTick(
() => users[userID] ? resolve(users[userID]) : reject({
error: 'User with ' + userID + ' not found.',
});
);
});
);
Run Code Online (Sandbox Code Playgroud)
但是,当在测试中直接调用函数时,这些函数似乎只能工作.
describe('example test', () => {
it('uses the mocked function', () => {
apiGetMethod().then(...);
});
});
Run Code Online (Sandbox Code Playgroud)
如果我有一个如此定义的React组件,我该如何模拟它?
import { apiGetMethod } from './api';
class Foo extends React.Component {
state = {
data: []
}
makeRequest = () => {
apiGetMethod().then(result => {
this.setState({data: result}); …Run Code Online (Sandbox Code Playgroud) 升级到Jest v20之后我收到以下错误,babel-polyfill由于内存泄漏,他们删除了自动:
TypeError: Object.values is not a function
我知道我现在需要自己填充这个,我正在使用babel-preset-env并拥有以下.babelrc文件:
"env": {
"development": {
"plugins": ["transform-react-jsx-source"]
},
"test": {
"presets": [
"react",
"stage-3",
["env", {
"targets": {
"browsers": [
"firefox >= 36",
"chrome >= 38",
"opera >= 25",
"safari >= 9",
"ios >= 9"
],
"node": "6.11.4"
},
"useBuiltIns": "usage",
"include": ["es7.object.values"],
"debug": true
}],
"jest"
],
"plugins": [
"transform-class-properties"
],
}
}
Run Code Online (Sandbox Code Playgroud)
我可以看到es7.object.values在调试输出中正在填充:
Using polyfills:
...
es7.object.values {"chrome":"38","firefox":"36","ios":"9","safari":"9","node":"6.11.4"}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到错误消息,帮助!
我正在尝试使用 dplyr 查找数据帧上变量的平均长度:
x <- data %>% group_by(Date, `% Bucket`) %>% summarise(count = n())
Date % Bucket count
(date) (fctr) (int)
1 2015-01-05 <=1 1566
2 2015-01-05 (1-25] 421
3 2015-01-05 (25-50] 461
4 2015-01-05 (50-75] 485
5 2015-01-05 (75-100] 662
6 2015-01-05 (100-150] 1693
7 2015-01-05 >150 12359
8 2015-01-13 <=1 1608
9 2015-01-13 (1-25] 441
10 2015-01-13 (25-50] 425
Run Code Online (Sandbox Code Playgroud)
如何汇总以找出% Bucket一年中每一年的平均值dplyr?
in base:
x <- as.data.frame(x)
aggregate(count ~ `% Bucket`, data = x, FUN=mean) …Run Code Online (Sandbox Code Playgroud)