R语言:if-else条件判断及any、all、na.omit使用方法

基本结构展示:

if (7<10) {
  print("Seven is less than ten")
} else{
  print("seven is more than ten")
}


 

实例演示:

Titanic=read.csv("https://goo.gl/4Gqsnz")  #从网络读取数据

1. any()     #any代表只要有任一值符合,即为TRUE

if (any(titanicC$Age>70)) {                                              
  print("there are passengers older than 70")
} else{
  print("no one is older than 70")
}


2. all()   #所有都满足才true

if (all(titanicC$Age>10)) {
  print("all passengers older than 10")
} else{
  print("there are passengers younger than 10")
}


3. na.omit()        #放的位置决定是删除单一变量缺失值,还是删除任何变量缺失值

if (any(na.omit(titanic$Age==100))) {
  print("there are passengers aged 100")
} else{
  print("there are no passengers aged 100")
}                                                                                 #数据库中只要有missing的记录都删掉


if (any(titanic$Age==80, na.rm=TRUE)) {
  print("there are passengers aged 80")
} else{
  print("there are no passengers aged 80")
}                                                                               #Age这个变量有missing的记录删掉,其他变量有missing可以保留



4.  else if 写更重复的语句

x=100
y=10
if(x<y){
  print("AA")
} else if(x==y){
  print(BB)
} else{
  print(CC)
}




发布了30 篇原创文章 · 获赞 65 · 访问量 35万+
展开阅读全文

求问条件不等式的比较判断和输出,怎么使用的C语言的程序代码编写的思想怎么解决问题的

06-04

Problem Description When John studied the timed automaton, he met the problem about how to trigger the machine. With the problem deeply studied, he found that it can be ascribed to the clock constraints of the timed automaton. The timed automation in question is described below: The clock variables, or simply clocks, are variables whose values are integers. Of course, time passes at the same rate for all clocks, and any clock can be reset to zero. John uses C to denote the finite set of clocks, and defines the clock constraints for C as follows: (1) All inequalities of the form t#c or c#t are clock constraints, where t is a clock, # is either < or <= , and c is an integer. (2) If A1 and A2 are clock constraints, then A1 ^ A2 is a clock constraint. John notes that a clock constraint can define several regions in some multidimensional space. He wants to know such regions, so he defines the clock zones recursively as follows. For simplicity, he let C0 = C {x0} , where x0 is a reference clock whose value is always 0. The clock zone A can be described by a Difference Bound Matrix D (called a DBM) which is a matrix (Dij) of size | C0|×| C0| . Each Dij has the form (dij,#) , where dij Z {$} , # { < ,<=} . The value of Dij can be evaluated in the following form: For every inequality xi - xj#dij in clock zone A , let Dij = (dij,#) , where xi and xj are two clocks. If the bound of xi - xj for xi and xj is unknown, let Dij = ($, < ) . For example, DBM of the clock zone given by x1 - x2 < 2 ^ 0 < x2<=2 ^ 1<=x1 is shown below: The representation of a clock zone by a DBM is not unique. In this example, there are some implied constraints that are not reflected in the DBM. Since x1 - x2 < 2 and x2<=2 , it must be the case x1 < 4 . Since x0 = 0 , the original D10 = ($, < ) can be changed into D10 = (4, < ) . Such adjusting operation is called the tighten operation. Now John wants to do the similar adjusting operations of difference bounds for all clocks xi and xj until further application of this tighten operation does not change the matrix. John obtains the following new canonical difference bound matrix: Note that some clock zone may contain contrary conditions and has not canonical difference bound matrix. But John can not obtain a canonical difference bound matrix for a complex clock zone. He asks for your help. Input The first line of the input file is a single integer T (1<=T<=20) , which is the number of test cases you must process, followed by T test cases: Each test case consists of several lines. Four integers i , j , d and r are given on each line, representing a constraint xi - xj < d or xi - xj<=d (0<=i, j<=m, -10000 < d < 10000) . If r = 0 , then this line represents an inequality in the form of xi - xj < d , otherwise it represents an inequality in the form of xi - xj<=d . The maximal index m of clocks indicates that the indexes of the clocks are 0, 1,..., m,(1<=m<=100) . Note that you have to get the value of m by yourself. A symbol # given on a single line indicates the end of a test case. Output For each test case, first output ``Case #:" on a single line, where # is the case number starting from 1. Print a blank line after each test case. For each test case, output the description of the canonical difference bound matrix. If it doesn't have a canonical difference bound matrix, print ``Canonical DBM does not exist." (without quotes); If it has a such canonical difference bound matrix, print the matrix in the format as indicated in the sample output. Every element Dij of the matrix should be written in the form (dij,#) , where # is either < or <=. If the bound of xi - xj for xi and xj is unknown, print ($,<) at the position (i, j) . Two consecutive elements on each row should be separated by a single space. Sample Input 1 1 2 2 0 0 2 0 0 2 0 2 1 0 1 -1 1 # Sample Output Case 1: (0,<=) (-1,<=) (0,<) (4,<) (0,<=) (2,<) (2,<=) (1,<=) (0,<=) 问答

条件判断,数学归纳法算法,解决这里的C语言问题

02-08

Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight. Your task is to deal with M operations of 4 types: 1.Delete an edge (x, y) from the tree, and then add a new edge (a, b). We ensure that it still constitutes a tree after adding the new edge. 2.Given two nodes a and b in the tree, change the weights of all the nodes on the path connecting node a and b (including node a and b) to a particular value x. 3.Given two nodes a and b in the tree, increase the weights of all the nodes on the path connecting node a and b (including node a and b) by a particular value d. 4.Given two nodes a and b in the tree, compute the second largest weight on the path connecting node a and b (including node a and b), and the number of times this weight occurs on the path. Note that here we need the strict second largest weight. For instance, the strict second largest weight of {3, 5, 2, 5, 3} is 3. Input The first line contains an integer T (T<=3), which means there are T test cases in the input. For each test case, the first line contains two integers N and M (N, M<=10^5). The second line contains N integers, and the i-th integer is the weight of the i-th node in the tree (their absolute values are not larger than 10^4). In next N-1 lines, there are two integers a and b (1<=a, b<=N), which means there exists an edge connecting node a and b. The next M lines describe the operations you have to deal with. In each line the first integer is c (1<=c<=4), which indicates the type of operation. If c = 1, there are four integers x, y, a, b (1<= x, y, a, b <=N) after c. If c = 2, there are three integers a, b, x (1<= a, b<=N, |x|<=10^4) after c. If c = 3, there are three integers a, b, d (1<= a, b<=N, |d|<=10^4) after c. If c = 4 (it is a query operation), there are two integers a, b (1<= a, b<=N) after c. All these parameters have the same meaning as described in problem description. Output For each test case, first output "Case #x:"" (x means case ID) in a separate line. For each query operation, output two values: the second largest weight and the number of times it occurs. If the weights of nodes on that path are all the same, just output "ALL SAME" (without quotes). Sample Input 2 3 2 1 1 2 1 2 1 3 4 1 2 4 2 3 7 7 5 3 2 1 7 3 6 1 2 1 3 3 4 3 5 4 6 4 7 4 2 6 3 4 5 -1 4 5 7 1 3 4 2 4 4 3 6 2 3 6 5 4 3 6 Sample Output Case #1: ALL SAME 1 2 Case #2: 3 2 1 1 3 2 ALL SAME 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览