refer to range of columns by name in R

后端 未结 6 1732
暗喜
暗喜 2020-12-17 04:00

I need help with something that might be fairly simple in R. I want to refer to a range of columns in a data frame (e.g., extracting a few select variables). However, I don\

6条回答
  •  被撕碎了的回忆
    2020-12-17 04:33

    Use %in% in combination with names(). It's useful for grabbing a group of columns from a data frame. You can negate the expression when you want to keep just a subset and drop the rest. Type ?"%in%" at the R Console prompt for more details.

    set.seed(1234)
    mydf <- data.frame(A = runif(5, 1, 2),
                       B = runif(5, 3, 4),
                       C = runif(5, 5, 6),
                       D = runif(5, 7, 8),
                       E = runif(5, 9, 10))
    mydf
    
    keep.cols <- c('A','D','E')
    mydf[, names(mydf) %in% keep.cols]
    drop.cols <- c('A','B','C')
    mydf[, !names(mydf) %in% drop.cols]
    

    The data frame:

    > mydf
             A        B        C        D        E
    1 1.113703 3.640311 5.693591 7.837296 9.316612
    2 1.622299 3.009496 5.544975 7.286223 9.302693
    3 1.609275 3.232551 5.282734 7.266821 9.159046
    4 1.623379 3.666084 5.923433 7.186723 9.039996
    5 1.860915 3.514251 5.292316 7.232226 9.218800
    

    A subset of columns:

    > mydf[, names(mydf) %in% keep.cols]
             A        D        E
    1 1.113703 7.837296 9.316612
    2 1.622299 7.286223 9.302693
    3 1.609275 7.266821 9.159046
    4 1.623379 7.186723 9.039996
    5 1.860915 7.232226 9.218800
    

    Keeping a subset of columns and dropping the rest:

    > mydf[, !names(mydf) %in% drop.cols]
             D        E
    1 7.837296 9.316612
    2 7.286223 9.302693
    3 7.266821 9.159046
    4 7.186723 9.039996
    5 7.232226 9.218800
    

提交回复
热议问题