2017-07-11 3 views
1

Ich versuche eine Boolesche Antwort zu erhalten, ob jede Zeile eines Vektors, der Listen enthält, leer ist.Ermitteln, ob eine Liste in einem tibble leer ist

Ich habe folgendes versucht, nach verschiedenen Fragen hier

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

Is.null immer falsch gibt, auch wenn ich <NULL> im tibble sehen, length ist immer 27561, is.list immer wahr. Dies scheint die akzeptierten Optionen here

zu verwenden Ich könnte nchar verwenden, die einen Wert von 4 gibt, wenn die Liste null ist, aber das scheint keine gute Möglichkeit.

Wie kann ich sauber finden, wenn eine Liste an der relevanten Stelle in einem tibble existiert?

Daten:

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")) 
+2

'sapply (df, Funktion (x) sapply (x, is.null))' –

Antwort

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 
Verwandte Themen