【问题标题】:How to remove double quotes after using colnames() for looping filter() function [duplicate]使用colnames()循环filter()函数后如何删除双引号[重复]
【发布时间】:2020-03-27 17:17:24
【问题描述】:

我正在尝试使用 dplyr 包中的 filter 函数从符合条件的数据框中提取行。

我的数据框如下所示:

head(EIA)
  length location  stype  rock  dist  ftype  depth aspect  degree  drain  TWI
1   len1     loc3 stype2 rock3 dist3 ftype1 depth2   asp2 degree2 drain4 TWI1
2   len4     loc2 stype2 rock3 dist1 ftype4 depth3   asp4 degree3 drain4 TWI3
3   len2     loc2 stype2 rock2 dist1 ftype4 depth3   asp1 degree2 drain4 TWI2
4   len4     loc2 stype2 rock3 dist2 ftype4 depth3   asp4 degree2 drain4 TWI2
5   len4     loc2 stype1 rock3 dist1 ftype2 depth3   asp1 degree3 drain4 TWI2
6   len4     loc3 stype2 rock2 dist1 ftype2 depth3   asp4 degree3 drain1 TWI2

等等。总行数:10560

但由于我试图在 for 循环中完成这项工作,过滤函数中的每个元素都必须通过变量(colcol,aa,bb,cc)传递:

 colcol
    [1] "length"   "location" "stype"    
 aa;bb;cc
    [1] "len1"
    [1] "loc2"
    [1] "stype1"

我尝试过这样的过滤功能,但没有奏效:

filter(EIA, colcol[1] == aa & colcol[2] == bb & colcol[3] == cc)

结果必须提取两行。我认为这是因为 colcol[1] 元素周围的双引号。我尝试使用as.symbolas.namenoquote 函数删除这些引号,但它们不起作用。

有没有办法解决这个问题?

提前谢谢你。

【问题讨论】:

    标签: r dataframe filter dplyr


    【解决方案1】:

    子集是此类问题的最佳解决方案

     aa <- subset(EIA,EIA$lenght=='len1')
    

    您可能会喜欢的上述示例以及简单而智能的解决方案。 您也可以使用 and (&) 条件来提取行。

     aa <- subset(EIA,EIA$lenght=='len1' & EIA$location=='loc2')
    

    【讨论】:

      【解决方案2】:

      将其转换为符号然后求值

      library(dplyr)
      filter(EIA,!!sym(colcol[1]) == aa & !!sym(colcol[2]) == bb & !!sym(colcol[3]) == cc)
      

      例如,当我们评估第一个条件时,我们得到

      filter(EIA, !!sym(colcol[1]) == aa)
      #  length location  stype  rock  dist  ftype  depth aspect  degree  drain  TWI
      #1   len1     loc3 stype2 rock3 dist3 ftype1 depth2   asp2 degree2 drain4 TWI1
      

      数据

      EIA <- structure(list(length = structure(c(1L, 3L, 2L, 3L, 3L, 3L), 
      .Label = c("len1", "len2", "len4"), class = "factor"), location = structure(c(2L, 
      1L, 1L, 1L, 1L, 2L), .Label = c("loc2", "loc3"), class = "factor"), 
      stype = structure(c(2L, 2L, 2L, 2L, 1L, 2L), .Label = c("stype1", 
      "stype2"), class = "factor"), rock = structure(c(2L, 2L, 
      1L, 2L, 2L, 1L), .Label = c("rock2", "rock3"), class = "factor"), 
      dist = structure(c(3L, 1L, 1L, 2L, 1L, 1L), .Label = c("dist1", 
      "dist2", "dist3"), class = "factor"), ftype = structure(c(1L, 
      3L, 3L, 3L, 2L, 2L), .Label = c("ftype1", "ftype2", "ftype4"
      ), class = "factor"), depth = structure(c(1L, 2L, 2L, 2L, 
      2L, 2L), .Label = c("depth2", "depth3"), class = "factor"), 
      aspect = structure(c(2L, 3L, 1L, 3L, 1L, 3L), .Label = c("asp1", 
      "asp2", "asp4"), class = "factor"), degree = structure(c(1L, 
      2L, 1L, 1L, 2L, 2L), .Label = c("degree2", "degree3"), class = "factor"), 
      drain = structure(c(2L, 2L, 2L, 2L, 2L, 1L), .Label = c("drain1", 
      "drain4"), class = "factor"), TWI = structure(c(1L, 3L, 2L, 
      2L, 2L, 2L), .Label = c("TWI1", "TWI2", "TWI3"), class = "factor")), 
      class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))
      
      colcol <- c("length",   "location", "dist")
      aa <- "len1"
      bb <- "loc2"
      cc <- "stype1"
      

      【讨论】:

      • 效果很好!非常感谢!
      猜你喜欢
      • 2017-07-09
      • 2021-04-24
      • 1970-01-01
      • 2021-12-09
      • 2019-03-17
      • 2021-06-12
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      相关资源
      最近更新 更多