我在R工作,如果可能的话,我更喜欢dplyr解决方案.

sample data:

data.frame(
  col1 = c("a", "b", "c", "d"),
  col2 = c("a", "b", "d", "a"),
  col3 = rep("a", 4L),
  col4 = c("a", "b", "d", "a"),
  col5 = c("a", "a", "c", "d"),
  col6 = rep(c("b", "a"), each = 2L)
)
col1 col2 col3 col4 col5 col6
a a a a a b
b b a b a b
c d a d c a
d a a a d a

Question

我想知道每一行,col1,col2col3是否与col4, col5col6,相同,但COL1-COL3和COL4-COL6的顺序应忽略不计.

因此,对于第1行,如果col 1-col 3分别包含a、a、b,col 4-col 6分别包含b、a、a,那么这将被视为匹配.

Desired result

我在"判断"栏目中注明,以帮助理解

col1 col2 col3 col4 col5 col6 assessment
a a a a a b FALSE (because 1-3 are not same as 4-6)
b b a b a b TRUE (because 1-3 are the same as 4-6, if ignore order)
c d a d c a TRUE (because 1-3 are the same as 4-6, if ignore order)
d a a a d a TRUE (because 1-3 are the same as 4-6, if ignore order)

推荐答案

使用dplyr,您可以执行以下操作:

df %>%
  rowwise() %>%
  mutate(result = all(sort(c_across(col1:col3)) == sort(c_across(col4:col6))))

# A tibble: 4 × 7
# Rowwise: 
  col1  col2  col3  col4  col5  col6  result
  <chr> <chr> <chr> <chr> <chr> <chr> <lgl> 
1 a     a     a     a     a     b     FALSE 
2 b     b     a     b     a     b     TRUE  
3 c     d     a     d     c     a     TRUE  
4 d     a     a     a     d     a     TRUE  

R相关问答推荐

从R中的另一个包扩展S3类的正确方法是什么

如何在ggplot 2线性图的每个方面显示每个组的误差条?

行式dppr中的变量列名

如何对数据集进行逆向工程?

编辑文件后编辑RhandsonTable

找出疾病消失的受试者

使用tidyverse方法绑定行并从一组管道列表执行左连接

将年度数据插入月度数据

如何在ggplot中标记qqplot上的点?

矩阵的堆叠条形图,条形图上有数字作为标签

R Read.table函数无法对制表符分隔的数据正常工作

从多个线性回归模型中提取系数

派生程序包| ;无法检索';return()';的正文

基于Key->Value数据帧的基因子集相关性提取

将具有坐标列表列的三角形转换为多个多边形

R中时间间隔的大向量与参考时间间隔的相交

如何在GALT包的函数&geom_x样条线中调整线宽

R-找出存在其他变量的各种大小的所有组合

分隔日期格式为2020年7月1日

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用