楼主: 树袋熊2
14389 5

[问答] R中如何求并集? [推广有奖]

  • 4关注
  • 4粉丝

已卖:464份资源

博士生

3%

还不是VIP/贵宾

-

威望
0
论坛币
336 个
通用积分
3.8492
学术水平
11 点
热心指数
19 点
信用等级
8 点
经验
4585 点
帖子
135
精华
0
在线时间
243 小时
注册时间
2011-3-9
最后登录
2023-9-20

楼主
树袋熊2 发表于 2012-2-28 13:09:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有a<-c(1,2,3) b<-c(2,3,4)怎么得到a并b :c(1,2,3,4)?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:如何

沙发
树袋熊2 发表于 2012-2-28 13:21:17
知道了~

藤椅
树袋熊2 发表于 2012-2-28 13:21:52
unique(c(a,b))

板凳
吉林小王子 在职认证  发表于 2012-2-28 13:23:19
a<-c(1,2,3)
b<-c(2,3,4)
union(a,b)#并集

报纸
qoiqpwqr 发表于 2012-2-29 00:21:25
Set Operations in R

R can perform different operations in sets, such as union, intersection, asymmetric difference of two sets, etc. Specifically, the following operations are available in R for set operations.

Operator

Usage

Definition

union

union(x, y)

Union of sets x and y

intersect

intersect(x, y)

Intersection of sets x and y

setdiff

setdiff(x, y)

Asymmetric difference between sets x andy (Elements in x but not in y)

setequal

setequal(x, y)

If sets x and y have the same elements

is.element

is.element(el, set)

If el is an element of set


Examples:

> x <- c(sort(sample(1:20, 9)),NA)
> y <- c(sort(sample(3:23, 7)),NA)
> x
[1]  1  3  5  8 11 17 18 19 20 NA
> y
[1]  7 11 15 16 17 19 22 NA
> union(x, y)
[1]  1  3  5  8 11 17 18 19 20 NA  7 15 16 22
> intersect(x, y)
[1] 11 17 19 NA
> setdiff(x, y)

[1]  1  3  5  8 18 20
> setdiff(y, x)
[1]  7 15 16 22
> setequal(x, y)
[1] FALSE

Note that each of union, intersect, setdiff and setequal will discard any duplicated values in the arguments. Look at the following example:

> x
[1]  1  3  5  8 11 17 18 19 20 NA
> x2 <- c(x, 1, 3, 5, 8)
> x2
[1]  1  3  5  8 11 17 18 19 20 NA  1  3  5  8
> setdiff(x, y)
[1]  1  3  5  8 18 20
> setdiff(x2, y)
[1]  1  3  5  8 18 20
> setequal(x, x2)
[1] TRUE

Although x and x2 have different length, they have the same UNIQUE elements sosetequal(x, x2) returns a TRUE value.

is.element(x, y) is identical to x %in% y.
The return value of is.element is a vector of TRUE and FALSE with the same length as x, which indicates whether each element of x is an element of y or not.
> is.element(x, y)  # vector of length 10
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
> is.element(y, x)  # vector of length 8
[1] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE


地板
AthenaUchiha 发表于 2015-3-17 15:36:24
谢谢楼主分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 08:30