2

I'd like to use a vector featuring blank ("") and non-blank character strings to subset rows so that I end up with a result like in dfgoal.

I've tried using dplyr::select(), but I get an error message (Error: Strings must match column names. Unknown columns: tooth, , head, foot).

I realise I've got a problem in that I want to keep some "" and get rid of others, but I don't know how to resolve it.

Thanks for any help!

# Data
df <- data.frame(avar=c("tooth","","","head","","foot","",""),bvar=c(1:8))

# Vector 
veca <- c("tooth","foot") 
vecb <- c("") 
vecc <- as.vector(rbind(veca,vecb)) 
vecc <- unique(vecc) 

# Attempt 
library(dplyr)
df <- df %>% dplyr::select(vecc)

# Goal 
dfgoal <- data.frame(avar=c("tooth","","","foot","",""),bvar=c(1,2,3,6,7,8)) 
LLL
  • 723
  • 1
  • 9
  • 27

1 Answers1

1

I'm not entirely clear on what you're trying to do. I assume you're asking how to select rows where avar %in% veca including subsequent blank ("") rows.

Perhaps something like this using tidyr::fill?

library(tidyverse)
veca <- c("tooth","foot")
df %>%
    mutate(tmp = ifelse(avar == "", NA, as.character(avar))) %>%
    fill(tmp) %>%
    filter(tmp %in% veca) %>%
    select(-tmp)
#  avar bvar
#1 tooth    1
#2          2
#3          3
#4  foot    6
#5          7
#6          8
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68