【问题标题】:subset data frame by complex pattern of column names按列名的复杂模式子集数据框
【发布时间】:2020-08-13 22:45:20
【问题描述】:

我有一个如下所示的数据集:

  • 两轮数据(.t0.t1
  • 多尺度(thisthat
  • 每个量表有几个项目(12222a
  • 要忽略的几个变量(v2v3ignore.t0ignore.t1this.t0this.t1that.t0that.t1

.

dat <- data.frame(id = seq(from=1, to=10, by=1),
                  v2 = rnorm(10),
                  v3 = rnorm(10),
                  ignore.t0 = rnorm(10),
                  this.t0 = rnorm(10),
                  this1.t0 = rnorm(10),
                  this22.t0 = rnorm(10),
                  this22a.t0 = rnorm(10),
                  that.t0 = rnorm(10),
                  that1.t0 = rnorm(10),
                  that22.t0 = rnorm(10),
                  that22a.t0 = rnorm(10),
                  ignore.t1 = rnorm(10),
                  this.t1 = rnorm(10),
                  this1.t1 = rnorm(10),
                  this22.t1 = rnorm(10),
                  this22a.t1 = rnorm(10),
                  that.t1 = rnorm(10),
                  that1.t1 = rnorm(10),
                  that22.t1 = rnorm(10),
                  that22a.t1 = rnorm(10))

我想对数据框进行子集化以包含 id 并且仅包含以下列:

  • 比例名称(thisthat)和
  • 句点前的数字 (1.) 或数字和字母 (22a.)

所以最后,数据框应该是这样的:

dat2 <- data.frame(
                   id = seq(from=1, to=10, by=1),
                   #v2 = rnorm(10),
                   #v3 = rnorm(10),
                   #ignore.t0 = rnorm(10),
                   #this.t0 = rnorm(10),
                   this1.t0 = rnorm(10),
                   this22.t0 = rnorm(10),
                   this22a.t0 = rnorm(10),
                   #that.t0 = rnorm(10),
                   that1.t0 = rnorm(10),
                   that22.t0 = rnorm(10),
                   that22a.t0 = rnorm(10),
                   #ignore.t1 = rnorm(10),
                   #this.t1 = rnorm(10),
                   this1.t1 = rnorm(10),
                   this22.t1 = rnorm(10),
                   this22a.t1 = rnorm(10),
                   #that.t1 = rnorm(10),
                   that1.t1 = rnorm(10),
                   that22.t1 = rnorm(10),
                   that22a.t1 = rnorm(10))

数据框比此处显示的要大得多,因此无法输入列索引。也不可能只查找比例名称,因为this.t0this.t1that.t0that.t1 会被捕获。

# not quite right
dat2$id <- dat$id
scales <- c("this", "that")
keep.index <- grep(paste(scales,collapse="|"), names(dat))
temp <- dat[keep.index]
dat2 <- cbind(dat2, temp)

如何修改 grep 模式以在句点之前查找数字 OR(数字和字符)?还是有更好的方法?

【问题讨论】:

    标签: r regex


    【解决方案1】:

    这适用于您的示例:

    dat[c("id", grep("(this|that)\\d+[a-z]?\\.", names(dat), value = TRUE))]
    

    地点:

    1. \\d+ 代表一位或多位数字
    2. [a-z]? 代表零个或一个小写字母
    3. \\. 代表点

    如果你想为各种scales动态构建一个模式,你可以这样做:

    scales <- c("this", "that")
    pattern <- sprintf("(%s)\\d+[a-z]?\\.", paste(scales, collapse = "|"))
    dat[c("id", grep(pattern, names(dat), value = TRUE))]
    

    【讨论】:

    • 太棒了。动态模式是我需要的。真的很有帮助。我应该能够从这个例子推广到其他 grep 模式。谢谢!
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2017-03-18
    • 2021-09-15
    • 2016-07-20
    • 2016-11-04
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多