假设这是我的数据框 df
ID COL1
a1 12
a2 12
a3 1
a4 5
a5 10
a6 5
a7 5
我期望的是构成数字的值的子集,例如,假设我有一个数字25,并且我想检查来自 COL1 of df 的所有值,以便它总计为25.所以加上(a1 + a2 + a3)= 25的值;; (a4 + a5 + a6 + a7)= 25,所有这样的可能性总和为25. 但是条件,ID不应该添加到自身产生结果使得(a1 + a1 + a3);;或(a5 + a5 + a6)。
这就是我试过的
df$ID[seq(which(cumsum(df$COL1) == 25))]
但这只是给了我a1,a2,a3。
答案 0 :(得分:1)
import { Layout, Menu, Breadcrumb, Icon, Form, Input, Button} from 'antd';
const { SubMenu } = Menu;
const { Header, Content, Sider } = Layout;
import React from "react";
import { Link, Router, Route, IndexRoute, browserHistory } from "react-router";
import { createForm } from 'rc-form';
import { Category } from "../../../imports/collections/category-db";
const FormItem = Form.Item;
function hasErrors(fieldsError) {
return Object.keys(fieldsError).some(field => fieldsError[field]);
}
class AddCategory extends React.Component {
componentDidMount() {
// To disabled submit button at the beginning.
this.props.form.validateFields();
}
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
const category_name =values.categoryName;
Meteor.subscribe('category');
Meteor.call('categoryInsert',category_name);
alert('category added cheers !');
}
});
}
state = {
collapsed: false,
};
toggle = () => {
this.setState({
collapsed: !this.state.collapsed,
});
}
mydata(){
Meteor.subscribe('category');
var categories = Category.find({}).fetch();
categories.forEach(function(cat){
console.log(cat.category_title);
});
}
render() {
const { getFieldDecorator, getFieldsError, getFieldError, isFieldTouched } = this.props.form;
// Only show error after a field is touched.
const userNameError = isFieldTouched('userName') && getFieldError('userName');
return (
<Layout>
<Sider
trigger={null}
collapsible
collapsed={this.state.collapsed}
>
<div className="logo" >
</div>
<Menu theme="dark" mode="inline" defaultSelectedKeys={['1']}>
<Menu.Item key="1">
<Link to="/users-admin"> <Icon type="user" />
<span> USERS </span></Link>
</Menu.Item>
<Menu.Item key="2">
<Link to="/category-admin">
<Icon type="video-camera" />
<span>Category</span>
</Link>
</Menu.Item>
<Menu.Item key="3">
<Icon type="mail" />
<span>Blogs 3</span>
</Menu.Item>
<Menu.Item key="4">
<Icon type="mail" />
<span>Jobs 4</span>
</Menu.Item>
</Menu>
</Sider>
<Layout>
<Header style={{ background: '#fff', padding: 0 }}>
<Icon
className="trigger"
type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'}
onClick={this.toggle}
/>
</Header>
<Content style={{ margin: '24px 16px', padding: 24, background: '#fff', minHeight: 720 }}>
<Form layout="inline" onSubmit={this.handleSubmit}>
<FormItem
validateStatus={userNameError ? 'error' : ''}
help={userNameError || ''}
>
{getFieldDecorator('categoryName', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input prefix={<Icon type="user" style={{ fontSize: 13 }} />} placeholder="Category tile" />
)}
</FormItem>
<FormItem>
<Button
type="primary"
htmlType="submit"
disabled={hasErrors(getFieldsError())}
>
Log in
</Button>
</FormItem>
</Form>
{this.mydata()}
</Content>
</Layout>
</Layout>
);
}
}
希望这能解决你的问题 编辑:
library(gtools)
#Replace 7 with the number of rows in your column
a <- c(12,12,1,5,10,5,5) #df$COL1
binary <- permutations(n=2,r=7,v=c(0,1),repeats.allowed = T)
mult <- binary %*% a
indices <- which(mult==25)
这有用吗?
答案 1 :(得分:1)
你可以试试这个
ID <- paste0(rep("a", 7), 1:7)
COL1 <- c(12, 12, 1, 5, 10, 5, 5)
df <- data.frame(ID, COL1)
for(i in 1:7){
comb <- combn(1:7, i, FUN = NULL, simplify = TRUE)
for (j in 1:ncol(comb)){
subvec <- comb[,j]
a <- sum(df[subvec,2])
if(a == 25){
print(df[subvec,1])
}
}
}
它给出了这个输出:
[1] a1 a2 a3
Levels: a1 a2 a3 a4 a5 a6 a7
[1] a4 a5 a6 a7
Levels: a1 a2 a3 a4 a5 a6 a7