代码之家  ›  专栏  ›  技术社区  ›  xeyetopewu

将列传递到lappy中的“with”

  •  1
  • xeyetopewu  · 技术社区  · 6 年前

    如何将数据帧的列传递给 with 中的函数 lapply 呼叫

    我已经试过了,但都不管用!

    lapply(data[ , grepl( "Measured." , names( data ) ) ], with, (. <= 5 & . >= 1) | . == 4244)
    
    lapply(data[ , grepl( "Measured." , names( data ) ) ], function(x) with((x <= 5 & x >= 1) | x == 4244))
    

    我想看看 Measured. 列介于 1 5 而且 4244 也可接受。

    示例数据集:

    data <- structure(list(ID = 1:10, Date = c(2018L, 2018L, 2018L, 2015L, 
    2018L, 2015L, 2015L, 2014L, 2014L, 2014L), Gender = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), 
        Measured.1 = c(1L, 7L, 1L, 6L, 6L, 2L, 5L, 4L, 2L, 6L), Measured.2 = c(9L, 
        2L, 4L, 5L, 2L, 3L, 6L, 3L, 7L, 7L), Measured.3 = c(9L, 4L, 
        35L, 3L, 4L, 2L, 2L, 1L, 3L, 4L), Measured.4 = c(12L, 8L, 
        50L, 7L, 2L, 6L, 2L, 2L, 1L, 2L), Text = structure(c(1L, 
        1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("N", "Y"), class = "factor"), 
        Test = c(5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L)), .Names = c("ID", 
    "Date", "Gender", "Measured.1", "Measured.2", "Measured.3", "Measured.4", 
    "Text", "Test"), class = "data.frame", row.names = c(NA, -10L
    ))
    

    及其输出:

       ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
    1   1 2018      M          1          9          9         12    N    5
    2   2 2018      M          7          2          4          8    N    5
    3   3 2018      M          1          4         35         50    N    5
    4   4 2015      M          6          5          3          7    N    5
    5   5 2018      M          6          2          4          2    N    5
    6   6 2015      M          2          3          2          6    Y    6
    7   7 2015      F          5          6          2          2    Y    6
    8   8 2014      F          4          3          1          2    Y    6
    9   9 2014      F          2          7          3          1    N    6
    10 10 2014      F          6          7          4          2    N    6
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Jan    6 年前

    除底座外 R 你可以使用 dplyr 解决方案:

    library(dplyr)
    data %>%
      filter_at(vars(starts_with("Measured")), 
                any_vars((. >= 1 & . <= 5) | . == 4244))
    

    这将查找至少 Measured 列的值介于1和5或4244之间。
    如果你想限制自己 全部的 值必须在此范围内,您可以将其更改为:

    data %>%
      filter_at(vars(starts_with("Measured")), 
                all_vars((. >= 1 & . <= 5) | . == 4244))
    


    前者产生
       ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
    1   1 2018      M          1          9          9         12    N    5
    2   2 2018      M          7          2          4          8    N    5
    3   3 2018      M          1          4         35         50    N    5
    4   4 2015      M          6          5          3          7    N    5
    5   5 2018      M          6          2          4          2    N    5
    6   6 2015      M          2          3          2          6    Y    6
    7   7 2015      F          5          6          2          2    Y    6
    8   8 2014      F          4          3          1          2    Y    6
    9   9 2014      F          2          7          3          1    N    6
    10 10 2014      F          6          7          4          2    N    6
    

    而后者会屈服

      ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
    1  8 2014      F          4          3          1          2    Y    6
    


    使用base可以以更快的方式(但在我看来可读性较差)完成同样的操作 R : 你可以用一个底座 R 带面罩进场 apply :
    # set up the cols of interest
    colmask <- grepl("^Measured", names(data))
    
    # apply the function rowwise (=1)
    rowmask <- apply(data[colmask], 1, function(col) {
      any(((col >= 1 & col <= 5) | col == 4244))
    })
    data[rowmask,]
    

    colmask <- grepl("^Measured", names(data))
    rowmask <- apply(data[colmask], 1, function(col) {
      all(((col >= 1 & col <= 5) | col == 4244))
    })
    data[rowmask,]
    

    显然,这会产生相同的结果。

        2
  •  1
  •   rg255    6 年前

    使用base R,可以提取符合以下条件的行:

    data[data[,1][data[,4] >= 1 & data[,4] <= 5 & data[,5] >= 1 & data[,5] <= 5 & data[,6] >= 1 & data[,6] <= 5 & data[,7] >= 1 & data[,7] <= 5 | data[,4] == 4244 | data[,5] == 4244 | data[,6] == 4244 | data[,7] == 4244],]
    

    我正在使用 & 创建附加标准(您正在查找测量的行。1、测量的行。2、测量的行。3和测量的行。4都是 >= 1 <= 5 )以及 | 创建替代标准(任何测量值 4424 ):

    给予:

      ID Date Gender Measured.1 Measured.2 Measured.3 Measured.4 Text Test
    8  8 2014      F          4          3          1          2    Y    6
    

    这不是最漂亮的代码,但(根据microbenchmark)从1月1日起,它的运行速度是dplyr方法的43倍。