Checking for blank columns beyond a certain range

Hi Guys,

I am looking for something in R, where assume we have a 24 columns with some random values, also assume post 12th column, every columns are blank.

Now, I punch in a number, like to test, assume, if it is 10, it will check, whether do you have any values beyond 10th column, if it is 20, it will check whether do you have any values beyond 20th column. Can you guys help me out in excel or R. I think a loop needs to be created.

Thanks,
Sachith

What an interesting puzzle. I suppose you could do something like this. This function returns TRUE when all columns from start_col onwards are empty and FALSE if any values are found.

library(dplyr, warn.conflicts = FALSE)
library(purrr)

df <- tribble(~ col1, ~ col2, ~ col3, ~ col4, ~ col5,
              1, 2, NA, NA, NA,
              3, 4, 3, NA, NA)

check_for_empty_cols <- function(data, start_col) {
  data %>% 
    select(all_of(start_col):last_col()) %>% 
    map(~ all(is.na(.x))) %>% 
    reduce(all)
}

check_for_empty_cols(df, start_col = 3)
#> [1] FALSE
check_for_empty_cols(df, start_col = 4)
#> [1] TRUE

Created on 2020-07-31 by the reprex package (v0.3.0)

1 Like

There are existing tools in packages that can give you insight on the data.frame contents. for example skimr package has skim() function, which will give you many facts about your data.frame. If it was the case that there were many unpopulated columns, I would notice it easily if I skimr::skim(mytable)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.