确定tibble中的列表是否为空

时间:2017-07-11 16:09:16

标签: r list null

我试图得到一个布尔响应,以确定包含列表的向量的每一行是否为空。

我按照这里的各种问题尝试了以下内容

g <- combined_plan %>%
  mutate(d = is.null(bck),
         e = length(bck),
         f = nchar(bck),
         h = is.list(bck)
         )

Is.null总是假,即使我在tibble中看到<NULL>length总是27561is.list总是如此。这似乎使用了可接受的选项here

我可以使用nchar,当list为null时,它给出的值为4,但这似乎不太好。

如何清楚地找到列表中相关位置是否存在列表?

数据:

structure(list(fwd = list(structure(c("44993002", "44993003", 
"44993004", "44993005", "44993006", "44993007", "44993008", "44993009", 
"4499301", "4499302", "4499303", "4499304", "4499305", "4499306", 
"4499307", "4499308", "4499309"), .Dim = c(17L, 1L)), structure(c("78110023", 
"78110024", "78110025", "78110026", "78110027", "78110028", "78110029", 
"7811003", "7811004", "7811005", "7811006", "7811007"), .Dim = c(12L, 
1L)), structure(c("27610013", "27610014", "27610015", "27610016", 
"27610017", "27610018", "27610019", "2761002", "2761003", "2761004", 
"2761005", "2761006"), .Dim = c(12L, 1L)), structure(c("44223002", 
"44223003", "44223004", "44223005", "44223006", "44223007", "44223008", 
"44223009", "4422301", "4422302", "4422303", "4422304", "4422305", 
"4422306", "4422307", "4422308", "4422309"), .Dim = c(17L, 1L
)), structure(c("27210034", "27210035", "27210036", "27210037", 
"27210038", "27210039", "2721004", "2721005", "2721006", "2721007", 
"2721008", "2721009"), .Dim = c(12L, 1L))), bck = list(NULL, 
    NULL, NULL, NULL, NULL)), .Names = c("fwd", "bck"), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

df %>% rowwise() %>% mutate_each(funs(NULL_check = is.null, CLASS_check = class))
#Source: local data frame [5 x 6]
#Groups: <by row>

## A tibble: 5 x 6
#             fwd    bck fwd_NULL_check bck_NULL_check fwd_CLASS_check bck_CLASS_check
#          <list> <list>          <lgl>          <lgl>           <chr>           <chr>
#1 <chr [17 x 1]> <NULL>          FALSE           TRUE          matrix            NULL
#2 <chr [12 x 1]> <NULL>          FALSE           TRUE          matrix            NULL
#3 <chr [12 x 1]> <NULL>          FALSE           TRUE          matrix            NULL
#4 <chr [17 x 1]> <NULL>          FALSE           TRUE          matrix            NULL
#5 <chr [12 x 1]> <NULL>          FALSE           TRUE          matrix            NULL