查找仅在R中的一行中出现的变量 [英] Find variables that occur only in ONE row in R

查看:49
本文介绍了查找仅在R中的一行中出现的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用BASE R,我想知道如何回答以下问题:

Using BASE R, I wonder how to answer the following question:

XY上是否存在仅在一行中出现而在其他行中没有出现的任何值?如果是,请在下面生成我的所需输出.

Are there any value on X or Y that occurs only in one row but not others? If yes, produce my desired output below.

f <- data.frame(id = c(rep("AA",4), rep("BB",2), rep("CC",2)), X = c(1,2,2,3,1,4,3,3), 
                                                               Y = c(99,7,8,7,6,7,7,7))

所需的输出:

list(BB = c(X = 4, Y = 6), AA = c(Y = c(99, 8)))

# $BB
# X Y 
# 4 6 

# $AA
# Y1 Y2  # Would be a plus if shows `Y Y` instead of `Y1 Y2` 
# 99  8

推荐答案

使用这种基本方法有两个大思路:

There are two big ideas with this base approach:

  1. 因为我们需要比较所有值,所以我们应该将所有内容重新组合为一个data.frame.
  2. 长时间保留未拆分的data.frame可以节省一些额外的步骤.

#https://stackoverflow.com/questions/58786052/find-variables-that-occur-only-once-across-a-split-data-frame-in-r/58788854#58788854
f <- data.frame(id = c(rep("AA",4), rep("BB",2), rep("CC",2)), X = c(1,2,2,3,1,4,3,3), 
                Y = c(99,7,8,7,6,7,7,7))
m <- split(f, f$id) # Here is `m`

unsplit <- do.call(rbind, c(m, make.row.names = F))
molten <- data.frame(unsplit[, 1, drop = F], stack(unsplit[, -1]))

# res <- subset(molten, !duplicated(values) & !duplicated(values, fromLast = T))
res <- molten[as.logical(ave(molten[['values']], molten[['ind']], FUN = function(x) !duplicated(x) & !duplicated(x, fromLast = T))), ]
#I would stop here
res
#>    id values ind
#> 6  BB      4   X
#> 9  AA     99   Y
#> 11 AA      8   Y
#> 13 BB      6   Y

#to get exact output
res_vector <- res$values
names(res_vector) <- res$ind

split(res_vector, as.character(res$id))
#> $AA
#>  Y  Y 
#> 99  8 
#> 
#> $BB
#> X Y 
#> 4 6

reprex软件包(v0.3.0)于2019-11-10创建 sup>

Created on 2019-11-10 by the reprex package (v0.3.0)

这是另一种可能不太复杂的基本方法:

Here's another base approach that may be less complicated:

####Way 1 with rapply
vec <- rapply(lapply(m, '[', mods), I)
unique_vec <- vec[!duplicated(vec) & !duplicated(vec, fromLast = T)]

vec_names <- do.call(rbind, strsplit(names(unique_vec), '.', fixed = T))

names(unique_vec) <- substr(vec_names[, 2], 1, 1) #turns Y1 into Y
split(unique_vec, vec_names[, 1])

###Way 2 with data.frame already do.call(rbind, m)
vec <-   unlist(
  lapply(f[, -1],
         function(x){
           ind <- !duplicated(x) & !duplicated(x, fromLast = T)
           ret <- x[ind]
           names(ret) <- f[ind, 1]
           ret
         } 
  )
)

#this is likely overly simplified:
split(vec, sub('.*\\.', '', names(vec)))

#this leads to exact result
vec_names <- do.call(rbind, strsplit(names(vec), '.', fixed = T))
names(vec) <- vec_names[, 1]

split(vec, vec_names[, 2])

$AA
 Y  Y 
99  8 

$BB
X Y 
4 6 

OP在提示中使用table()调出. duplicated()表现出色:

OP brings up using table() in a hint. duplicated() is very performant:

unlist(lapply(f[mods], function(y) names(which(table(y) == 1))))
#   X   Y1   Y2   Y3 
# "4"  "6"  "8" "99"

vec
#X.BB Y.AA Y.AA Y.BB 
#   4   99    8    6 

# A tibble: 2 x 13
  expression   min median `itr/sec` mem_alloc
  <bch:expr> <bch> <bch:>     <dbl> <bch:byt>
1 table_meth 321us  336us     2794.    10.3KB
2 dup_meth   132us  136us     7105.    31.7KB

bench::mark(
  table_meth = {unlist(lapply(f[mods], function(y) names(which(table(y) == 1))))},
  dup_meth = {
  #could get slight performance boost with
    #f_id <- f[['id']]
  unlist(
    lapply(f[, -1],
           function(x){
             ind <- !duplicated(x) & !duplicated(x, fromLast = T)
             ret <- x[ind]
             names(ret) <- f[ind, 1]
             #names(ret) <- f_id[ind] 
             ret
           } 
    )
  )}
  , check = F
)

:

library(data.table)

molten_dt <- melt(rbindlist(m), id.vars = 'id')
molten_dt[!duplicated(value, by = variable) &
             !duplicated(value, by = variable, fromLast = T)]

中的类似想法:

And similar idea in dplyr:

library(dplyr)
library(tidyr)

m%>%
  bind_rows()%>%
  pivot_longer(cols = -id)%>%
  group_by(name)%>%
  filter(!duplicated(value) & !duplicated(value, fromLast = T))%>%
  group_by(id)%>%
  group_split()

这篇关于查找仅在R中的一行中出现的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆