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

使用另一个具有var名称和其他条件的数据帧对数据帧进行子集设置

r
  •  1
  • LLL  · 技术社区  · 7 年前

    df_main df_keep df_goal

    df_keep$keep_var NA "r" df_keep$othvar

    # Starting point 
    df_main <- data.frame(coat=c(1:5),hanger=c(1:5),book=c(1:5),dvd=c(1:5),bookcase=c(1:5),
                                     clock=c(1:5),bottle=c(1:5),curtains=c(1:5),wall=c(1:5))
    df_keep <- data.frame(keep_var=c("coat","hanger","book","wall","bottle"),othvar=c("r","w","r","w",NA))
    
    
    # Goal
    df_goal <- data.frame(coat=c(1:5),book=c(1:5),bottle=c(1:5))
    
    
    # Attempt
    df_keep$othvar[is.na(df_keep$othvar)] <- "r"   # everything in othvar that's NA I want to keep so I recode it to "r"
    df_keep <- df_keep %>% filter(othvar == "r")  # keep everything that's "r"
    df_main <- df_main[df_keep$keep_var]  # subset my df_main using updated df_keep  
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Luke C    7 年前

    df_keep

    conditions_met <- df_keep$othvar == "r" | is.na(df_keep$othvar)
    
    > conditions_met
    [1]  TRUE FALSE  TRUE FALSE  TRUE
    

    df_keep$keepvar

    kept_rows <- df_keep$keep_var[conditions_met]
    
    > kept_rows
    [1] coat   book   bottle
    

    df_main 他们的名字和 kept_rows :

    df_main[, as.character(kept_rows)]
      coat book bottle
    1    1    1      1
    2    2    2      2
    3    3    3      3
    4    4    4      4
    5    5    5      5
    

    或者在一行中:

    > df_main[, as.character(df_keep$keep_var[df_keep$othvar == "r" |
    +                                           is.na(df_keep$othvar)])]
      coat book bottle
    1    1    1      1
    2    2    2      2
    3    3    3      3
    4    4    4      4
    5    5    5      5
    

    注意, as.character 因为示例数据集不使用 stringsAsFactors = FALSE . 如果有,你可以省略 as.字符 参数,所以如果您的实际数据是字符而不是因子,那么您应该能够删除 as.字符 . 如:

    df_main <-
      data.frame(
        coat = c(1:5),
        hanger = c(1:5),
        book = c(1:5),
        dvd = c(1:5),
        bookcase = c(1:5),
        clock = c(1:5),
        bottle = c(1:5),
        curtains = c(1:5),
        wall = c(1:5),
        stringsAsFactors = FALSE
      )
    
    df_keep <-
      data.frame(
        keep_var = c("coat", "hanger", "book", "wall", "bottle"),
        othvar = c("r", "w", "r", "w", NA),
        stringsAsFactors = FALSE
      )
    
    df_goal <- data.frame(coat = c(1:5),
                          book = c(1:5),
                          bottle = c(1:5))
    
    
    df_main[, df_keep$keep_var[df_keep$othvar == "r" |
                                              is.na(df_keep$othvar)]]
    
        2
  •  0
  •   steveb    7 年前

    这是一个 dplyr 解决方案

    library(dplyr)
    # Filter based on 'othvar' and convert factor to string.
    keep.vec <- as.character(
        (df_keep %>% dplyr::filter(is.na(othvar) | othvar == 'r'))$keep_var
    )
    df_main %>% dplyr::select(keep.vec)
    
    ##   coat book bottle
    ## 1    1    1      1
    ## 2    2    2      2
    ## 3    3    3      3
    ## 4    4    4      4
    ## 5    5    5      5