Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of logical Operation in R language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of logical operation in R language, which is very detailed and has certain reference value. Friends who are interested must finish it!

I. logical type vector and comparison operation

Logical type is one of the basic data types of R, with only two values TRUE and FALSE, and NA when missing.

Logical values are generally generated from self-comparison, such as

Sele 2

Return:

(1:4) > = (4:1)

Return:

Compare with NA to produce NA, such as

C (1, NA, 3) > 2

Return:

NA = = NA

Return:

To determine whether each element of the vector is NA, use the is.na () function, such as

Is.na (c (1, NA, 3) > 2)

Return:

Use is.finite () to determine whether each element of the vector is an Inf value.

Is.finite (c (1mai Info 8))

Return:

Comparison operators include:

< >

=!% in%

Respectively means less than, less than or equal to, greater than or equal to, equal to, not equal to, belonging to. Note that two equal signs are used for comparison.

% in% is a special comparison. The operation of x% in% y regards vector y as a set, and the result is a logical vector. The value of the I element is whether the I element of x belongs to the logical value of y. Such as

C (1Jue 3)% in% c (2Jing 3Jing 4)

Return:

C (NA,3)% in% c # # [1] FALSE TRUEc (1pm 3)% in% c (NA,3, 4) # # [1] FALSE TRUEc (NA,3)% in% c (NA,3, 4) # [1] TRUE TRUE

The function match (x, y) is similar to the x% in% y operation, but its return result is not found or not, but for each element of x, find the subscript that appears for the first time in y, and take the missing value if it is not found, such as

Match (c (1, 3), c (2, 3, 4, 3))

Return:

Second, logical operation

In order to express things like "x > 0 and x"

< 1", " x ≤ 0 或者 x ≥ 1" 之类的复合比较,需要使用逻辑运算把两个比较连接起来。逻辑运算符为 & , | 和 ! , 分别表示 " 同时成 立" 、 " 两者至少其一成立 " 、 " 条件的反面 " 。比如,设 age 2)all(NA)any(NA) 返回: 函数 which() 返回真值对应的所有下标,如 which(c(FALSE, TRUE, TRUE, FALSE, NA))which((11:15) >

12)

Return:

The function identical (XPY) compares whether the contents of two R objects x and y are exactly the same, and the result is that only scalar TRUE and FALSE will be taken. Such as

Identical (c), c (1min2)) identical (c (1L, 2L, 3L), c (1me2, 3L))

Because the first vector is of integer type and the latter is of real type, the result returns:

The function all.equal () is similar to identical (), but does not distinguish between integer and real types when comparing numeric types, and returns scalar TRUE at the same time, but returns a string indicating how different it is. Such as

All.equal (c), c (1min2)) all.equal (c (1L, 2L, 3L), c (1me2, 3L))

Return:

The function duplicated () returns the result of whether each element is a duplicate value, such as:

Duplicated (c (1, 2, 1, 3, 4, and na))

Return:

With the function unique (), you can return the result of removing duplicate values, such as

Unique (c (1, 2, 1, 3, 4, and na))

Return:

The above is all the contents of the article "example Analysis of logical Operation in R language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report