【问题标题】:Compare the colnames within two lists of data.frames?比较两个data.frames列表中的colnames?
【发布时间】:2023-03-23 06:14:01
【问题描述】:

当这些变量不符合某些条件时,我正在使用一个函数从列表中的每个 data.frame 中删除各个列,并且我想要一种方便的方法来查看哪些列已被删除。现实世界的 data.frames 将有 1000 多个不同的colnames,这些名称有些重叠。

在这个简化的例子中,我想得到一个列表,显示每个 data.frame 的变量,这些变量存在于 list1 中,但在 list2 中不存在。

输入列表

> list1
$A
  a b
1 2 3
$B
  c d e
1 9 8 1
$C
  f g
1 6 7

> list2
$A
  a
1 2
$B
  c d
1 9 8
$C
  g
1 7  

期望的输出

我想保留列表结构,以便查看从每个 data.frame 中删除了哪些列。

$A
  b
1 3
$B
  e
1 1
$C
  f
1 6

我的尝试

我看过 SO,但只找到了与比较 data.frames 相关的解决方案。请记住,列表元素的名称(此处为 A、B 和 C)在列表中将始终相同。我的想法是使用setdiffsetdiffmapply,但我的修修补补并没有成果。可以做什么?

## sample data
list1 <- list(A=data.frame(a=2, b=3), B=data.frame(c=9,d=8,e=1), C=data.frame(f= 6,g=7))
list2 <- list(A=data.frame(a=2), B=data.frame(c=9,d=8), C=data.frame(g=7))
desired_output <- list(A=data.frame(b=3), B=data.frame(e=1), C=data.frame(f= 6))

## attempts

# gives List 1
setdiff(list1, list2)

# gives 'Error: not compatible: Cols in x but not y: `b`.'
mapply(setdiff, x = list1, y = list2)

# gives 'Error in list1[[i]] : recursive indexing failed at level 3'
mapply(setdiff, x = colnames(list1[[i]]), y = colnames(list2[[i]]))

# gives 'list()'
mapply(setdiff, x = colnames(list1[i]), y = colnames(list2[i]))

# Gives 'Error in list1[colnams] : invalid subscript type 'list''
colnams <- list()
for(i in seq_along(list1)){
   colnams[i] <- !colnames(list1[[i]]) %in% colnames(list2[[i]]) 
}
list1[colnams]

【问题讨论】:

    标签: r list names


    【解决方案1】:

    您可以根据另一个函数中的列应用一个函数来对data.frame 进行子集化,并确保它始终使用drop = F 返回一个data.frame。并确保在mapply 中使用SIMPLIFY = F,以便它始终返回列表结构。

    mapply(function(x,y) x[,-which(names(x) %in% names(y)), drop = F], list1, list2, SIMPLIFY = F)
    #> $A
    #>   b
    #> 1 3
    #> 
    #> $B
    #>   e
    #> 1 1
    #> 
    #> $C
    #>   f
    #> 1 6
    

    【讨论】:

      【解决方案2】:

      您可以使用lapply 提取名称并使用setdiff 获取不在其他列表中的名称。 不需要列表被排序

      x <- lapply(list1, names)
      y <- lapply(list2, names)
      lapply(setNames(names(x), names(x)), function(i) list1[[i]][setdiff(x[[i]], y[[i]])])
      #$A
      #  b
      #1 3
      #
      #$B
      #  e
      #1 1
      #
      #$C
      #  f
      #1 6
      

      【讨论】:

        【解决方案3】:

        purrr:

        map2(.x = list1,
             .y = list2,
             ~ .x[setdiff(names(.x), names(.y))])
        
        $A
          b
        1 3
        
        $B
          e
        1 1
        
        $C
          f
        1 6
        

        【讨论】:

          猜你喜欢
          • 2013-02-14
          • 2015-12-18
          • 2011-06-19
          • 2013-03-03
          • 2021-03-21
          • 2018-03-19
          • 2016-08-24
          • 2019-04-18
          相关资源
          最近更新 更多