I need to do three actions:
1. Count the rowwise non NA's values in a table and sum them (in a single column "check_na")
[I put my solution to this below, interested if someone can figure out how to do this with map though. I have already checked https://stackoverflow.com/questions/50680413/count-na-in-given-columns-by-rows for answers for this]
2. For those values that are not NA, create a column that concatenates these the unique values in a new column "block detail".
[I don't know how to do this]
3. If "check_na" has a value then pull in the column name(s) and concatenate them in a new column ("block type")
[I don't know how to do this]
This is what the final product should look like. note that in line 2 even though "b" appears twice, it only shows up once in "block detail" yet the columns that have it are individually listed "y|z"
w x y z na_check block_detail block_type
<dbl> <chr> <chr> <chr> <int> <chr> <chr>
1 NA a NA NA 1 a x
2 NA NA b b 2 b y|z
3 NA NA b c 2 b|c y|z
4 NA NA NA NA 0 NA NA
5 NA NA NA b 1 b z
below is sample data and my solution to part 1:
#sample data
df <- tibble(w=rep(NA_real_,5),
x=c(1,rep(NA_real_,4)),
y=c(NA_real_,1,rep(NA_real_,3)),
z=c(NA_real_,1,rep(NA_real_,2),1)
)
#my solution to the first part, interested if someone can do this more efficiently or can do this with map as I have 100s columns that I need to do this with
df_na_check <- df %>%
mutate(across(everything(),
list(na_check=~!is.na(.)),
.names="{.col}_{.fn}")) %>%
rowwise() %>%
mutate(na_check=sum(c_across(contains("na_check")))) %>%
select(w:z,na_check)
I appreciate any help. Ideally if the solution can use tidyverse but open to other methods (data.table or base r)