我开始学习R并得到一段代码,声明是:
if(sum(C == C[i]) == 1)# C is simply a vector and i is index of a value in this vector which the user specifies in an arguement.
如何将条件语句作为函数的参数传递,也解释了该语句的含义。 谢谢。
答案 0 :(得分:1)
让我们举个例子来理解
将C
视为1到10之间的数字向量,让我们将i
视为3
C <- 1:10
i <- 3
所以当我们做的时候
C == C[i]
#[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
它将C
的每个元素与C[i]
进行比较,该值为3,并返回相应的逻辑向量,该向量在第3个索引处仅为TRUE
。
当我们sum
此逻辑向量时,它返回所有TRUE
的计数(因为它将FALSE
视为0而TRUE
视为1)在这种情况下为1的值
sum(C == C[i])
#[1] 1
然后再次与1进行比较,以确保C[i]
C
sum(C == C[i]) == 1
#[1] TRUE
如果我们在C
中重复了数字,则会失败。例如,
C <- c(1:10, 3) #Adding an extra 3 in the end
C
#[1] 1 2 3 4 5 6 7 8 9 10 3
i <- 3
sum(C == C[i]) == 1
#[1] FALSE
如果TRUE
仅在C[i]
中出现C
,那么条件就是'use strict';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
var Base = require('../Statics.Common');
export default class User extends Component {
static get propTypes() {
return {
user: PropTypes.object,
getAllUsers: PropTypes.func
}
}
constructor(props) {
super(props);
this.user = this.props.user;
this.getAllUsers = this.props.getAllUsers;
}
update(id, name) {
var updatedName = prompt("Please enter updated name:", name);
axios.put(Base.API + '/' + id, {name: updatedName}).then(results => {
if(results.status == 200) {
this.getAllUsers();
}
})
}
delete(id) {
axios.delete(Base.API + '/' + id).then(results => {
if(results.status == 200) {
this.getAllUsers();
}
})
}
render() {
return <tr>
<td>{this.user._id || this.user.id}</td>
<td>{this.user.name}</td>
<button onClick={(e) => this.update(this.user._id || this.user.id, this.user.name)}>Update</button>
<button onClick={(e) => this.delete(this.user._id || this.user.id)}>Delete</button>
</tr>
}
}
Users.jsx - users module renders list of users into a ui
the
'use strict';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import User from './User.jsx';
export default class Users extends Component {
static get propTypes() {
return {
users: PropTypes.array
}
}
constructor(props) {
super(props);
}
componentWillReceiveProps(props) {
this.setState(props)
}
render() {
this.users = this.props.users;
return <div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{
this.users.map(user => {
return <User key={user._id || user.id} user={user} getAllUsers={() => this.props.getAllUsers()}/>
})
}
</tbody>
</table>
</div>;
}
}
AddUser.jsx - adding a new user
'use strict';
import React, {Component} from 'react';
import PropTypes from "prop-types";
export default class AddUser extends Component {
static get propTypes() {
return {
addUser: PropTypes.func,
name: PropTypes.string
}
}
constructor(props) {
super(props);
}
onNameChange(event) {
event.preventDefault();
event.stopPropagation();
this.name = event.target.value;
}
onSubmit(event) {
event.preventDefault();
event.stopPropagation();
if (this.name) {
this.props.addUser({name: this.name});
this.name = '';
}
}
render() {
return <div>
<form onSubmit={event => this.onSubmit(event)}>
<label>Name:</label>
<input type="text" onChange={event => this.onNameChange(event)}/>
<button type="submit">Add</button>
</form>
</div>;
}
}
Appcontainer.jsx - the app container for displaying the class
'use strict';
import React, {Component} from 'react';
import Users from './Modules/Users';
import AddUser from './Controllers/AddUser';
import axios from 'axios';
var Base = require('./Statics.Common');
export default class AppContainer extends Component {
constructor(props) {
super(props);
this.state = {
users: []
}
this.getAllUsers();
}
getAllUsers() {
axios.get(Base.API + '/').then(res => {
this.setState({
users: res.data.data || res.data
});
})
}
addUser(user) {
axios.post(Base.API + '/', {name: user.name}).then(result => {
if(result.status == 200) {
this.getAllUsers();
}
}).catch(err => {
alert(err);
})
}
render() {
return <div>
<h2>Users App</h2>
<AddUser addUser={user => this.addUser(user)}/>
<Users users={this.state.users} getAllUsers = {() => this.getAllUsers()}/>
</div>;
}
}
statics.common.jsx - this has some api urls for node and spring boot
var CommonDetails = function() {
//this.API = 'http//localhost:8081'; // node api
this.API = 'http://localhost:8084'; // springboot api
}
module.exports = new CommonDetails();
。