问:
How to merge different columns into a new one made with the merged columns' name
I'm trying to clean my dataset. The dataset comes from a survey that had a randomizer. It displayed different scenarios to each participant.
My dataset now has a column for each different scenario with the name I chose for it (e.g.: A_SM_GIL or D_DF_GIL etc.). The underscore "" separates the different scenarios variables that are combined together to form a new scenario. A, D_, B_, N_ are the timing for a disclosure message during the advertising, respectively AFTER, DURING, BEFORE,and NONEF. SM_, and DF_, stand for the type of terminology used. Respectively SYNTHETIC MEDIA, and DEEPFAKE. The last bit, LOR_, GIL_, describes the brand's ad. Respectively L'Oreal, Gillette.

What I am left now is a bunch of columns that show with a "1" the scenario each observation was exposed to before answering a 7point likert scale to measure their attitudes towards the ad shown. The scale used is called ADTEST (AD_) and has different items (BORING_1, IRRITATING_1, etc).

Only 1 scenario for observation is possible. So one observation will have a column with 1 and all the others with NAs.

My goal is to compare the different scenarios' attitudes. For example, studying if positioning the disclosure before the ad makes a difference than positioning it after the ad. I would like to plot the different answers (using a 7 point likert scale i.e.: AD_BORING_1 etc) people gave but divided into the different scenarios to compare them.

```<code>structure(list(D_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA,
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA), A_SM_GIL = c(NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA), A_DF_GIL = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), N_DF_GILL = c(1,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA), AD_BORING_1 = c("7",
"6", "5", "3", "4", "3", "5", "2", "4", "5", "3", "2", "5", "3",
"4", "4", "5", "5", "1", "1", "4", "7", "4", "4", "3", "6", "1",
"5", "2", "5", "3", "2", "2", "7", "6", "6", "6", "5", "4", "4",
"6", "7", "3", "2", "1", "5", "5", "5", "5", "5", "6", "4", "5",
"3", "5", "2", "7", "5", "2", "2", "3", "1", "5", "5", "4", "5",
"1", "3", "2", "6", "5", "1", "3", "3", "5", "6", "5", "6", "2",
"3", "5", "6", "5", "4", "6", "5", "4", "3", "5", "5", "4", "6",
"2", "4", "6", "3", "4", "5", "3", "2", "6", "6", "5", "6", "3",
"2", "6", "5", "4", "2", "3", "6", "4", "7", "3", "1", "4", "4",
"5", "4", "2", "5", "6", "3", "5", "6", "4", "2", "5", "5", "3",
"2", "3", "1", "6", "2", "1", "5", "5", "2", "6", "1", "6", "5",
"2", "5", "2", "2", "4", "2", "2", "4", "4", "6", "3", "5", "5",
"3", "2", "4", "7", "4", "4", "2", "5", "3", "2", "3", "3", "4",
"2", "5", "6", "4", "4", "7", "4", "6", "3", "4", "2", "4", "3",
"2", "6", "5", "5", "5", "4", "2", "3", "3", "5", "5", "6", "6",
"7", "4", "2", "6", "3", "6", "4", "2", "7", "6", "4", "3", "2",
"5", "5", "6", "5", "2", "7", "2", "3", "5", "2", "3", "5", "5",
"5", "2", "4", "3", "5", "2", "3", "3", "2", "5", "5", "3", "3",
"5", "3", "2", "5", "3", "7", "5", "5", "5", "4", "2", "4", "2",
"3", "4", "5", "2", "5", "7", "6", "4", "5", "6", "3", "5", "3",
"2", "4", "5", "5", "3", "1", "4", "7", "5", "6", "3", "3", "4",
"5", "6", "5", "4", "1", "5", "2", "3", "7", "3", "4", "5", "3",
"3", "5", "5", "2", "5", "4", "5", "2", "6", "2", "6", "4", "2",
"3", "4", "6", "3", "2", "5", "2", "6", "2", "1", "3", "4", "1",
"2", "5", "4", "3", "7", "5", "6", "6", "3", "4", "2", "4", "5",
"2", "4", "3", "3", "2", "2", "3", "5", "3", "1", "3", "2", "2",
"4", "2", "2", "3", "1", "4", "2", "5", "2", "5", "5", "5", "2",
"5", "5", "4", "2", "7", "5", "5", "3", "4", "5", "3", "4", "3",
"2", "5"), AD_IRRITATING_1 = c("4", "7", "3", "5", "4", "3",
"4", "4", "4", "3", "4", "2", "2", "4", "1", "2", "1", "4", "1",
"1", "4", "7", "5", "4", "2", "2", "6", "4", "2", "5", "1", "2",
"2", "4", "2", "5", "4", "6", "4", "4", "7", "6", "3", "2", "1",
"4", "2", "2", "4", "2", "7", "5", "2", "5", "2", "2", "6", "2",
"5", "2", "2", "4", "6", "5", "4", "3", "2", "5", "1", "4", "2",
"2", "2", "2", "2", "4", "5", "3", "5", "5", "1", "4", "4", "5",
"4", "4", "4", "4", "4", "2", "2", "4", "2", "5", "7", "4", "5",
"3", "2", "1", "4", "6", "3", "6", "3", "2", "6", "2", "3", "2",
"2", "4", "2", "7", "3", "1", "4", "5", "2", "4", "1", "5", "5",
"5", "5", "6", "2", "3", "5", "5", "2", "2", "3", "2", "6", "2",
"1", "4", "2", "2", "5", "2", "2", "3", "2", "4", "2", "2", "2",
"2", "2", "4", "5", "5", "1", "5", "2", "5", "1", "2", "2", "4",
"5", "2", "4", "3", "2", "5", "2", "4", "2", "2", "4", "3", "5",
"5", "3", "3", "1", "5", "2", "5", "6", "3", "4", "2", "6", "4",
"4", "2", "2", "2", "2", "2", "4", "1", "6", "3", "2", "5", "1",
"4", "5", "2", "2", "2", "6", "1", "1", "2", "3", "5", "7", "5",
"7", "2", "3", "2", "3", "2", "1", "2", "5", "2", "4", "4", "6",
"2", "3", "3", "1", "5", "4", "4", "3", "4", "4", "2", "4", "4",
"5", "4", "6", "4", "4", "2", "5", "2", "2", "2", "7", "2", "3",
"6", "5", "3", "3", "6", "1", "6", "2", "2", "5", "4", "2", "4",
"1", "2", "4", "6", "4", "1", "2", "4", "2", "2", "4", "3", "1",
"2", "2", "4", "4", "3", "5", "2", "4", "4", "5", "5", "3", "5",
"4", "2", "2", "4", "5", "4", "2", "5", "2", "4", "3", "2", "1",
"2", "2", "6", "5", "2", "1", "2", "1", "3", "6", "4", "4", "6",
"3", "4", "4", "2", "2", "1", "4", "1", "2", "3", "2", "2", "5",
"2", "3", "7", "7", "3", "2", "2", "3", "3", "1", "5", "2", "1",
"4", "2", "5", "2", "5", "4", "4", "2", "5", "6", "2", "1", "2",
"4", "4", "2", "5", "2", "4", "2", "5", "2", "7")), row.names = c(NA,
-367L), class = c("tbl_df", "tbl", "data.frame"))
</code>```

I was thinking I could create a new column made of the other columns' name if a condition is met (in this case the fact that a column has a 1 and not NA).

Something like this

Another problem is all around the dataset there are other columns, so I need to specify exactly the columns to analyze.

Thank you so much for your time.

答:
Explanation of Answer

Here you can get what I think you're after with two successive applications of tidyr::pivot_longer().

The first pivot_longer() consolidates all the different scenarios into a single column so there is still one row per participant and one column that indicates what scenario group that participant was in.

Next you use pivot_longer() again to get the different adjectives into a single column.

I run a dplyr::mutate() with stringr::str_extract() to parse the adjective column.

Since you have some important metadata embedded in the scenario name, you can parse that out with tidyr::separate()

Also it looks like you have two different spellings of GIL/GILL so I cleaned that up which will help them sort together, but omit if that was intentional.

Since these seem like discrete variables I think it will be easier downstream if you convert them all to factors with mutate(across(everything(), factor)).

Once cleaned in this way, the data should be much easier to work with and visualize.

Main Answer

library(tidyverse)

d <- structure(list(D_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA), A_SM_GIL = c(NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA), A_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), N_DF_GILL = c(1, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA), AD_BORING_1 = c("7", "6", "5", "3", "4", "3", "5", "2", "4", "5", "3", "2", "5", "3", "4", "4", "5", "5", "1", "1", "4", "7", "4", "4", "3", "6", "1", "5", "2", "5", "3", "2", "2", "7", "6", "6", "6", "5", "4", "4", "6", "7", "3", "2", "1", "5", "5", "5", "5", "5", "6", "4", "5", "3", "5", "2", "7", "5", "2", "2", "3", "1", "5", "5", "4", "5", "1", "3", "2", "6", "5", "1", "3", "3", "5", "6", "5", "6", "2", "3", "5", "6", "5", "4", "6", "5", "4", "3", "5", "5", "4", "6", "2", "4", "6", "3", "4", "5", "3", "2", "6", "6", "5", "6", "3", "2", "6", "5", "4", "2", "3", "6", "4", "7", "3", "1", "4", "4", "5", "4", "2", "5", "6", "3", "5", "6", "4", "2", "5", "5", "3", "2", "3", "1", "6", "2", "1", "5", "5", "2", "6", "1", "6", "5", "2", "5", "2", "2", "4", "2", "2", "4", "4", "6", "3", "5", "5", "3", "2", "4", "7", "4", "4", "2", "5", "3", "2", "3", "3", "4", "2", "5", "6", "4", "4", "7", "4", "6", "3", "4", "2", "4", "3", "2", "6", "5", "5", "5", "4", "2", "3", "3", "5", "5", "6", "6", "7", "4", "2", "6", "3", "6", "4", "2", "7", "6", "4", "3", "2", "5", "5", "6", "5", "2", "7", "2", "3", "5", "2", "3", "5", "5", "5", "2", "4", "3", "5", "2", "3", "3", "2", "5", "5", "3", "3", "5", "3", "2", "5", "3", "7", "5", "5", "5", "4", "2", "4", "2", "3", "4", "5", "2", "5", "7", "6", "4", "5", "6", "3", "5", "3", "2", "4", "5", "5", "3", "1", "4", "7", "5", "6", "3", "3", "4", "5", "6", "5", "4", "1", "5", "2", "3", "7", "3", "4", "5", "3", "3", "5", "5", "2", "5", "4", "5", "2", "6", "2", "6", "4", "2", "3", "4", "6", "3", "2", "5", "2", "6", "2", "1", "3", "4", "1", "2", "5", "4", "3", "7", "5", "6", "6", "3", "4", "2", "4", "5", "2", "4", "3", "3", "2", "2", "3", "5", "3", "1", "3", "2", "2", "4", "2", "2", "3", "1", "4", "2", "5", "2", "5", "5", "5", "2", "5", "5", "4", "2", "7", "5", "5", "3", "4", "5", "3", "4", "3", "2", "5"), AD_IRRITATING_1 = c("4", "7", "3", "5", "4", "3", "4", "4", "4", "3", "4", "2", "2", "4", "1", "2", "1", "4", "1", "1", "4", "7", "5", "4", "2", "2", "6", "4", "2", "5", "1", "2", "2", "4", "2", "5", "4", "6", "4", "4", "7", "6", "3", "2", "1", "4", "2", "2", "4", "2", "7", "5", "2", "5", "2", "2", "6", "2", "5", "2", "2", "4", "6", "5", "4", "3", "2", "5", "1", "4", "2", "2", "2", "2", "2", "4", "5", "3", "5", "5", "1", "4", "4", "5", "4", "4", "4", "4", "4", "2", "2", "4", "2", "5", "7", "4", "5", "3", "2", "1", "4", "6", "3", "6", "3", "2", "6", "2", "3", "2", "2", "4", "2", "7", "3", "1", "4", "5", "2", "4", "1", "5", "5", "5", "5", "6", "2", "3", "5", "5", "2", "2", "3", "2", "6", "2", "1", "4", "2", "2", "5", "2", "2", "3", "2", "4", "2", "2", "2", "2", "2", "4", "5", "5", "1", "5", "2", "5", "1", "2", "2", "4", "5", "2", "4", "3", "2", "5", "2", "4", "2", "2", "4", "3", "5", "5", "3", "3", "1", "5", "2", "5", "6", "3", "4", "2", "6", "4", "4", "2", "2", "2", "2", "2", "4", "1", "6", "3", "2", "5", "1", "4", "5", "2", "2", "2", "6", "1", "1", "2", "3", "5", "7", "5", "7", "2", "3", "2", "3", "2", "1", "2", "5", "2", "4", "4", "6", "2", "3", "3", "1", "5", "4", "4", "3", "4", "4", "2", "4", "4", "5",
"4", "6", "4", "4", "2", "5", "2", "2", "2", "7", "2", "3", "6", "5", "3", "3", "6", "1", "6", "2", "2", "5", "4", "2", "4", "1", "2", "4", "6", "4", "1", "2", "4", "2", "2", "4", "3", "1", "2", "2", "4", "4", "3", "5", "2", "4", "4", "5", "5", "3", "5", "4", "2", "2", "4", "5", "4", "2", "5", "2", "4", "3", "2", "1", "2", "2", "6", "5", "2", "1", "2", "1", "3", "6", "4", "4", "6", "3", "4", "4", "2", "2", "1", "4", "1", "2", "3", "2", "2", "5", "2", "3", "7", "7", "3", "2", "2", "3", "3", "1", "5", "2", "1", "4", "2", "5", "2", "5", "4", "4", "2", "5", "6", "2", "1", "2", "4", "4", "2", "5", "2", "4", "2", "5", "2", "7")), row.names = c(NA, -367L), class = c("tbl_df", "tbl", "data.frame"))

# make labels in case that helps
likert_breaks <- c("Strongly Disagree", "Somewhat Disagree", "Slightly Disagree", "Neutral", "Slightly Agree", "Somewhat Agree", "Strongly Agree")

# transform to long format and parse out metadata
d_long <- d %>%
  pivot_longer(cols = !starts_with("AD"),
               names_to = "group",
               values_to = "drop",
               values_drop_na = T) %>%
  select(-drop) %>%
  pivot_longer(cols = starts_with("AD"),
               names_to = "adjective",
               values_to = "likert") %>%
  mutate(adjective = str_extract(adjective, "(?<=_)(.*)(?=_)")) %>%
  separate(group, into = c("pretreat", "treat", "ad")) %>%
  mutate(ad = if_else(ad == "GILL", "GIL", ad)) %>%
  mutate(across(everything(), factor))

str(d_long)
#> tibble [150 x 5] (S3: tbl_df/tbl/data.frame)
#>  $ pretreat : Factor w/ 3 levels "A","D","N": 3 3 1 1 2 2 3 3 2 2 ...
#>  $ treat    : Factor w/ 2 levels "DF","SM": 1 1 2 2 1 1 1 1 1 1 ...
#>  $ ad       : Factor w/ 1 level "GIL": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ adjective: Factor w/ 2 levels "BORING","IRRITATING": 1 2 1 2 1 2 1 2 1 2 ...
#>  $ likert   : Factor w/ 7 levels "1","2","3","4",..: 7 4 4 4 2 4 4 4 3 4 ...
```
<hr />
<h2>Bonus: Plotting</h2>
<p>Since I got the impression you were hoping to look at the response profile by treatment group I wanted to provide some code to get you a plot of that fashion. Here there are a couple of important steps to make sure you get all of the bars in all plots in the right position.</p>
<ol>
<li>Above we turned all the variables into <code>factor</code>s to ensure they can keep even empty levels.</li>
<li>Then we use <code>dplyr::summarize(n = n())</code> to count the responses per group per likert score.</li>
<li>Because the variables are all <code>factor</code>s we can then use <code>tidyr::complete()</code> to expand out all of the groups and fill <code>0</code> when there was no response of that type. This way we will force the graph to keep space for every bar even if it wasn't observed in the data.</li>
</ol>
<pre class="lang-r prettyprint-override"><code># bonus: plot the data
d_long %>%
  group_by(across(everything())) %>%
  summarize(n = n(), .groups = "drop") %>%
  complete(pretreat, treat, ad, adjective, likert, fill = list(n = 0)) %>%
  ggplot(aes(x = adjective, y = n, fill = likert)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_grid(pretreat ~ treat) +
  scale_fill_viridis_d(option = "A", begin = 0, end = 0.85, labels = likert_breaks, drop = F) +
  theme_bw()
</code>```

Created on 2022-02-09 by the reprex package (v2.0.1)

Body:
I'm trying to clean my dataset. The dataset comes from a survey that had a randomizer. It displayed different scenarios to each participant.
My dataset now has a column for each different scenario with the name I chose for it (e.g.: A_SM_GIL or D_DF_GIL etc.). The underscore "" separates the different scenarios variables that are combined together to form a new scenario. A, D_, B_, N_ are the timing for a disclosure message during the advertising, respectively AFTER, DURING, BEFORE,and NONEF. SM_, and DF_, stand for the type of terminology used. Respectively SYNTHETIC MEDIA, and DEEPFAKE. The last bit, LOR_, GIL_, describes the brand's ad. Respectively L'Oreal, Gillette.

What I am left now is a bunch of columns that show with a "1" the scenario each observation was exposed to before answering a 7point likert scale to measure their attitudes towards the ad shown. The scale used is called ADTEST (AD_) and has different items (BORING_1, IRRITATING_1, etc).

Only 1 scenario for observation is possible. So one observation will have a column with 1 and all the others with NAs.

My goal is to compare the different scenarios' attitudes. For example, studying if positioning the disclosure before the ad makes a difference than positioning it after the ad. I would like to plot the different answers (using a 7 point likert scale i.e.: AD_BORING_1 etc) people gave but divided into the different scenarios to compare them.

structure(list(D_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA,
1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA), A_SM_GIL = c(NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA), A_DF_GIL = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), N_DF_GILL = c(1,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA,
NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 1, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA), AD_BORING_1 = c("7",
"6", "5", "3", "4", "3", "5", "2", "4", "5", "3", "2", "5", "3",
"4", "4", "5", "5", "1", "1", "4", "7", "4", "4", "3", "6", "1",
"5", "2", "5", "3", "2", "2", "7", "6", "6", "6", "5", "4", "4",
"6", "7", "3", "2", "1", "5", "5", "5", "5", "5", "6", "4", "5",
"3", "5", "2", "7", "5", "2", "2", "3", "1", "5", "5", "4", "5",
"1", "3", "2", "6", "5", "1", "3", "3", "5", "6", "5", "6", "2",
"3", "5", "6", "5", "4", "6", "5", "4", "3", "5", "5", "4", "6",
"2", "4", "6", "3", "4", "5", "3", "2", "6", "6", "5", "6", "3",
"2", "6", "5", "4", "2", "3", "6", "4", "7", "3", "1", "4", "4",
"5", "4", "2", "5", "6", "3", "5", "6", "4", "2", "5", "5", "3",
"2", "3", "1", "6", "2", "1", "5", "5", "2", "6", "1", "6", "5",
"2", "5", "2", "2", "4", "2", "2", "4", "4", "6", "3", "5", "5",
"3", "2", "4", "7", "4", "4", "2", "5", "3", "2", "3", "3", "4",
"2", "5", "6", "4", "4", "7", "4", "6", "3", "4", "2", "4", "3",
"2", "6", "5", "5", "5", "4", "2", "3", "3", "5", "5", "6", "6",
"7", "4", "2", "6", "3", "6", "4", "2", "7", "6", "4", "3", "2",
"5", "5", "6", "5", "2", "7", "2", "3", "5", "2", "3", "5", "5",
"5", "2", "4", "3", "5", "2", "3", "3", "2", "5", "5", "3", "3",
"5", "3", "2", "5", "3", "7", "5", "5", "5", "4", "2", "4", "2",
"3", "4", "5", "2", "5", "7", "6", "4", "5", "6", "3", "5", "3",
"2", "4", "5", "5", "3", "1", "4", "7", "5", "6", "3", "3", "4",
"5", "6", "5", "4", "1", "5", "2", "3", "7", "3", "4", "5", "3",
"3", "5", "5", "2", "5", "4", "5", "2", "6", "2", "6", "4", "2",
"3", "4", "6", "3", "2", "5", "2", "6", "2", "1", "3", "4", "1",
"2", "5", "4", "3", "7", "5", "6", "6", "3", "4", "2", "4", "5",
"2", "4", "3", "3", "2", "2", "3", "5", "3", "1", "3", "2", "2",
"4", "2", "2", "3", "1", "4", "2", "5", "2", "5", "5", "5", "2",
"5", "5", "4", "2", "7", "5", "5", "3", "4", "5", "3", "4", "3",
"2", "5"), AD_IRRITATING_1 = c("4", "7", "3", "5", "4", "3",
"4", "4", "4", "3", "4", "2", "2", "4", "1", "2", "1", "4", "1",
"1", "4", "7", "5", "4", "2", "2", "6", "4", "2", "5", "1", "2",
"2", "4", "2", "5", "4", "6", "4", "4", "7", "6", "3", "2", "1",
"4", "2", "2", "4", "2", "7", "5", "2", "5", "2", "2", "6", "2",
"5", "2", "2", "4", "6", "5", "4", "3", "2", "5", "1", "4", "2",
"2", "2", "2", "2", "4", "5", "3", "5", "5", "1", "4", "4", "5",
"4", "4", "4", "4", "4", "2", "2", "4", "2", "5", "7", "4", "5",
"3", "2", "1", "4", "6", "3", "6", "3", "2", "6", "2", "3", "2",
"2", "4", "2", "7", "3", "1", "4", "5", "2", "4", "1", "5", "5",
"5", "5", "6", "2", "3", "5", "5", "2", "2", "3", "2", "6", "2",
"1", "4", "2", "2", "5", "2", "2", "3", "2", "4", "2", "2", "2",
"2", "2", "4", "5", "5", "1", "5", "2", "5", "1", "2", "2", "4",
"5", "2", "4", "3", "2", "5", "2", "4", "2", "2", "4", "3", "5",
"5", "3", "3", "1", "5", "2", "5", "6", "3", "4", "2", "6", "4",
"4", "2", "2", "2", "2", "2", "4", "1", "6", "3", "2", "5", "1",
"4", "5", "2", "2", "2", "6", "1", "1", "2", "3", "5", "7", "5",
"7", "2", "3", "2", "3", "2", "1", "2", "5", "2", "4", "4", "6",
"2", "3", "3", "1", "5", "4", "4", "3", "4", "4", "2", "4", "4",
"5", "4", "6", "4", "4", "2", "5", "2", "2", "2", "7", "2", "3",
"6", "5", "3", "3", "6", "1", "6", "2", "2", "5", "4", "2", "4",
"1", "2", "4", "6", "4", "1", "2", "4", "2", "2", "4", "3", "1",
"2", "2", "4", "4", "3", "5", "2", "4", "4", "5", "5", "3", "5",
"4", "2", "2", "4", "5", "4", "2", "5", "2", "4", "3", "2", "1",
"2", "2", "6", "5", "2", "1", "2", "1", "3", "6", "4", "4", "6",
"3", "4", "4", "2", "2", "1", "4", "1", "2", "3", "2", "2", "5",
"2", "3", "7", "7", "3", "2", "2", "3", "3", "1", "5", "2", "1",
"4", "2", "5", "2", "5", "4", "4", "2", "5", "6", "2", "1", "2",
"4", "4", "2", "5", "2", "4", "2", "5", "2", "7")), row.names = c(NA,
-367L), class = c("tbl_df", "tbl", "data.frame"))

I was thinking I could create a new column made of the other columns' name if a condition is met (in this case the fact that a column has a 1 and not NA).

Something like this

Another problem is all around the dataset there are other columns, so I need to specify exactly the columns to analyze.

Thank you so much for your time.

Body:
Explanation of Answer

Here you can get what I think you're after with two successive applications of tidyr::pivot_longer().

The first pivot_longer() consolidates all the different scenarios into a single column so there is still one row per participant and one column that indicates what scenario group that participant was in.

Next you use pivot_longer() again to get the different adjectives into a single column.

I run a dplyr::mutate() with stringr::str_extract() to parse the adjective column.

Since you have some important metadata embedded in the scenario name, you can parse that out with tidyr::separate()

Also it looks like you have two different spellings of GIL/GILL so I cleaned that up which will help them sort together, but omit if that was intentional.

Since these seem like discrete variables I think it will be easier downstream if you convert them all to factors with mutate(across(everything(), factor)).

Once cleaned in this way, the data should be much easier to work with and visualize.

Main Answer

library(tidyverse)

d <- structure(list(D_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA), A_SM_GIL = c(NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA), A_DF_GIL = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), N_DF_GILL = c(1, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,
NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA), AD_BORING_1 = c("7", "6", "5", "3", "4", "3", "5", "2", "4", "5", "3", "2", "5", "3", "4", "4", "5", "5", "1", "1", "4", "7", "4", "4", "3", "6", "1", "5", "2", "5", "3", "2", "2", "7", "6", "6", "6", "5", "4", "4", "6", "7", "3", "2", "1", "5", "5", "5", "5", "5", "6", "4", "5", "3", "5", "2", "7", "5", "2", "2", "3", "1", "5", "5", "4", "5", "1", "3", "2", "6", "5", "1", "3", "3", "5", "6", "5", "6", "2", "3", "5", "6", "5", "4", "6", "5", "4", "3", "5", "5", "4", "6", "2", "4", "6", "3", "4", "5", "3", "2", "6", "6", "5", "6", "3", "2", "6", "5", "4", "2", "3", "6", "4", "7", "3", "1", "4", "4", "5", "4", "2", "5", "6", "3", "5", "6", "4", "2", "5", "5", "3", "2", "3", "1", "6", "2", "1", "5", "5", "2", "6", "1", "6", "5", "2", "5", "2", "2", "4", "2", "2", "4", "4", "6", "3", "5", "5", "3", "2", "4", "7", "4", "4", "2", "5", "3", "2", "3", "3", "4", "2", "5", "6", "4", "4", "7", "4", "6", "3", "4", "2", "4", "3", "2", "6", "5", "5", "5", "4", "2", "3", "3", "5", "5", "6", "6", "7", "4", "2", "6", "3", "6", "4", "2", "7", "6", "4", "3", "2", "5", "5", "6", "5", "2", "7", "2", "3", "5", "2", "3", "5", "5", "5", "2", "4", "3", "5", "2", "3", "3", "2", "5", "5", "3", "3", "5", "3", "2", "5", "3", "7", "5", "5", "5", "4", "2", "4", "2", "3", "4", "5", "2", "5", "7", "6", "4", "5", "6", "3", "5", "3", "2", "4", "5", "5", "3", "1", "4", "7", "5", "6", "3", "3", "4", "5", "6", "5", "4", "1", "5", "2", "3", "7", "3", "4", "5", "3", "3", "5", "5", "2", "5", "4", "5", "2", "6", "2", "6", "4", "2", "3", "4", "6", "3", "2", "5", "2", "6", "2", "1", "3", "4", "1", "2", "5", "4", "3", "7", "5", "6", "6", "3", "4", "2", "4", "5", "2", "4", "3", "3", "2", "2", "3", "5", "3", "1", "3", "2", "2", "4", "2", "2", "3", "1", "4", "2", "5", "2", "5", "5", "5", "2", "5", "5", "4", "2", "7", "5", "5", "3", "4", "5", "3", "4", "3", "2", "5"), AD_IRRITATING_1 = c("4", "7", "3", "5", "4", "3", "4", "4", "4", "3", "4", "2", "2", "4", "1", "2", "1", "4", "1", "1", "4", "7", "5", "4", "2", "2", "6", "4", "2", "5", "1", "2", "2", "4", "2", "5", "4", "6", "4", "4", "7", "6", "3", "2", "1", "4", "2", "2", "4", "2", "7", "5", "2", "5", "2", "2", "6", "2", "5", "2", "2", "4", "6", "5", "4", "3", "2", "5", "1", "4", "2", "2", "2", "2", "2", "4", "5", "3", "5", "5", "1", "4", "4", "5", "4", "4", "4", "4", "4", "2", "2", "4", "2", "5", "7", "4", "5", "3", "2", "1", "4", "6", "3", "6", "3", "2", "6", "2", "3", "2", "2", "4", "2", "7", "3", "1", "4", "5", "2", "4", "1", "5", "5", "5", "5", "6", "2", "3", "5", "5", "2", "2", "3", "2", "6", "2", "1", "4", "2", "2", "5", "2", "2", "3", "2", "4", "2", "2", "2", "2", "2", "4", "5", "5", "1", "5", "2", "5", "1", "2", "2", "4", "5", "2", "4", "3", "2", "5", "2", "4", "2", "2", "4", "3", "5", "5", "3", "3", "1", "5", "2", "5", "6", "3", "4", "2", "6", "4", "4", "2", "2", "2", "2", "2", "4", "1", "6", "3", "2", "5", "1", "4", "5", "2", "2", "2", "6", "1", "1", "2", "3", "5", "7", "5", "7", "2", "3", "2", "3", "2", "1", "2", "5", "2", "4", "4", "6", "2", "3", "3", "1", "5", "4", "4", "3", "4", "4", "2", "4", "4", "5",
"4", "6", "4", "4", "2", "5", "2", "2", "2", "7", "2", "3", "6", "5", "3", "3", "6", "1", "6", "2", "2", "5", "4", "2", "4", "1", "2", "4", "6", "4", "1", "2", "4", "2", "2", "4", "3", "1", "2", "2", "4", "4", "3", "5", "2", "4", "4", "5", "5", "3", "5", "4", "2", "2", "4", "5", "4", "2", "5", "2", "4", "3", "2", "1", "2", "2", "6", "5", "2", "1", "2", "1", "3", "6", "4", "4", "6", "3", "4", "4", "2", "2", "1", "4", "1", "2", "3", "2", "2", "5", "2", "3", "7", "7", "3", "2", "2", "3", "3", "1", "5", "2", "1", "4", "2", "5", "2", "5", "4", "4", "2", "5", "6", "2", "1", "2", "4", "4", "2", "5", "2", "4", "2", "5", "2", "7")), row.names = c(NA, -367L), class = c("tbl_df", "tbl", "data.frame"))

# make labels in case that helps
likert_breaks <- c("Strongly Disagree", "Somewhat Disagree", "Slightly Disagree", "Neutral", "Slightly Agree", "Somewhat Agree", "Strongly Agree")

# transform to long format and parse out metadata
d_long <- d %>%
  pivot_longer(cols = !starts_with("AD"),
               names_to = "group",
               values_to = "drop",
               values_drop_na = T) %>%
  select(-drop) %>%
  pivot_longer(cols = starts_with("AD"),
               names_to = "adjective",
               values_to = "likert") %>%
  mutate(adjective = str_extract(adjective, "(?<=_)(.*)(?=_)")) %>%
  separate(group, into = c("pretreat", "treat", "ad")) %>%
  mutate(ad = if_else(ad == "GILL", "GIL", ad)) %>%
  mutate(across(everything(), factor))

str(d_long)
#> tibble [150 x 5] (S3: tbl_df/tbl/data.frame)
#>  $ pretreat : Factor w/ 3 levels "A","D","N": 3 3 1 1 2 2 3 3 2 2 ...
#>  $ treat    : Factor w/ 2 levels "DF","SM": 1 1 2 2 1 1 1 1 1 1 ...
#>  $ ad       : Factor w/ 1 level "GIL": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ adjective: Factor w/ 2 levels "BORING","IRRITATING": 1 2 1 2 1 2 1 2 1 2 ...
#>  $ likert   : Factor w/ 7 levels "1","2","3","4",..: 7 4 4 4 2 4 4 4 3 4 ...

Bonus: Plotting

Since I got the impression you were hoping to look at the response profile by treatment group I wanted to provide some code to get you a plot of that fashion. Here there are a couple of important steps to make sure you get all of the bars in all plots in the right position.

Above we turned all the variables into factors to ensure they can keep even empty levels.

Then we use dplyr::summarize(n = n()) to count the responses per group per likert score.

Because the variables are all factors we can then use tidyr::complete() to expand out all of the groups and fill 0 when there was no response of that type. This way we will force the graph to keep space for every bar even if it wasn't observed in the data.

# bonus: plot the data
d_long %>%
  group_by(across(everything())) %>%
  summarize(n = n(), .groups = "drop") %>%
  complete(pretreat, treat, ad, adjective, likert, fill = list(n = 0)) %>%
  ggplot(aes(x = adjective, y = n, fill = likert)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_grid(pretreat ~ treat) +
  scale_fill_viridis_d(option = "A", begin = 0, end = 0.85, labels = likert_breaks, drop = F) +
  theme_bw()

Created on 2022-02-09 by the reprex package (v2.0.1)


来源: stackexchange

问:
Rails how to create a DRY domain checker?
I am creating domain checker and are wondering what the best logic is.

I am using this rails gem: https://github.com/weppos/whois

My solution on have to create this:

I have 1 input field which the user type the domain name their want. And when it is submitted it renders all the available top domains.

In my action I would have:

```<code>@domain = params[:domain]
@dk = Whois.whois("#{@domain}.dk")
@com = Whois.whois("#{@domain}.com")
@it = Whois.whois("#{@domain}.it")
@no = Whois.whois("#{@domain}.no")
@se = Whois.whois("#{@domain}.se")
@is = Whois.whois("#{@domain}.is")

And 50 more domains ...
</code>```

And then I would have a helper to apply a class to the instance variable dependent on it is available or not. Example named domain_check. So that I could write in the view <%= domain_check(@is) %>

Is there not a better solution to create a domain checker instead of creating about 50 repeated instance variables?

UPDATE:

```<code>module PublicHelper
require 'whois'
def domain_checker(obj, options={})
    options[:info]          ||= obj
    options[:info_class]    ||= 'info'
    options[:pinfo]         ||= obj
    options[:pinfo_class]   ||= 'pinfo'
if obj.available?
    content_tag(:span, options[:pinfo], :class => options[:pinfo_class])
    else
    content_tag(:span, options[:info], :class => options[:info_class])
    end
end
end
</code>```

In view :

```<code><% @results.each do |webhost| %>
<%= domain_checker(webhost) %><br />
  <% end %>
</code>```

I get this error:

```<code>NoMethodError in Public#domain

Showing C:/Rails/webhostapp/app/views/public/domain.html.erb where line #2 raised:

undefined method `available?' for #<Array:0x23eb3f0>

Extracted source (around line #2):

1: ï»¿<% @results.each do |webhost| %>
2: <%= domain_checker(webhost) %><br />
3:   <% end %>
</code>```

答:
Here's how I'd do it:

Controller:

```<code>country_codes = ['.dk', '.com', '.it', '.no'] # etc. could move this to a config if needed

@domain = params[:domain]

@results = {}
country_codes.each do |cc|
  @results[cc] = Whois.whois(@domain + cc).available?
end
</code>```

Then @results is:

```<code>{".dk" => true, ".com" => false} # etc.
</code>```

Then in the view (you can move it to a helper if desired):

```<code><ul>
  <% @results.each_pair do |country_code, available| %>
    <% klass = available ? "pinfo" : "info" %>
    <li><%= @domain + country_code %><span class="<%= klass %>"></span></li>
  <% end %>
</ul>
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I am creating domain checker and are wondering what the best logic is.

I am using this rails gem: https://github.com/weppos/whois

My solution on have to create this:

I have 1 input field which the user type the domain name their want. And when it is submitted it renders all the available top domains.

In my action I would have:

@domain = params[:domain]
@dk = Whois.whois("#{@domain}.dk")
@com = Whois.whois("#{@domain}.com")
@it = Whois.whois("#{@domain}.it")
@no = Whois.whois("#{@domain}.no")
@se = Whois.whois("#{@domain}.se")
@is = Whois.whois("#{@domain}.is")

And 50 more domains ...

And then I would have a helper to apply a class to the instance variable dependent on it is available or not. Example named domain_check. So that I could write in the view <%= domain_check(@is) %>

Is there not a better solution to create a domain checker instead of creating about 50 repeated instance variables?

UPDATE:

module PublicHelper
require 'whois'
def domain_checker(obj, options={})
    options[:info]          ||= obj
    options[:info_class]    ||= 'info'
    options[:pinfo]         ||= obj
    options[:pinfo_class]   ||= 'pinfo'
if obj.available?
    content_tag(:span, options[:pinfo], :class => options[:pinfo_class])
    else
    content_tag(:span, options[:info], :class => options[:info_class])
    end
end
end

In view :

<% @results.each do |webhost| %>
<%= domain_checker(webhost) %>

  <% end %>

I get this error:

NoMethodError in Public#domain

Showing C:/Rails/webhostapp/app/views/public/domain.html.erb where line #2 raised:

undefined method `available?' for #<Array:0x23eb3f0>

Extracted source (around line #2):

1: ï»¿<% @results.each do |webhost| %>
2: <%= domain_checker(webhost) %>

3:   <% end %>

Body:
Here's how I'd do it:

Controller:

country_codes = ['.dk', '.com', '.it', '.no'] # etc. could move this to a config if needed

@domain = params[:domain]

@results = {}
country_codes.each do |cc|
  @results[cc] = Whois.whois(@domain + cc).available?
end

Then @results is:

{".dk" => true, ".com" => false} # etc.

Then in the view (you can move it to a helper if desired):

<ul>
  <% @results.each_pair do |country_code, available| %>
    <% klass = available ? "pinfo" : "info" %>
    <li><%= @domain + country_code %><span class="<%= klass %>"></span></li>
  <% end %>
</ul>


问:
How AWS Direct Connect works?
How does AWS Direct Connect work?

From AWS docs:

AWS Direct Connect establishes a dedicated network connection between your on-premises network and AWS ... bypassing your internet service provider.

Please explain what it actually means in practical sense? And why would it be any better?

答:
NOTE: I know there are AWS docs (worst docs I've ever seen) and some
other articles and answers on SO, but none of those explain what it
actually means in practice or miss some important notes for understanding for somebody who's only used to public internet and can't imagine how it's possible to "bybass public internet". So I decided
to create this post from my collective knowledge and also provided a real example from our company case. Hope it will be
useful for somebody.

So, from AWS docs:

AWS Direct Connect establishes a dedicated network connection between your on-premises network and AWS ... bypassing your internet service provider.

But to understand what it actually means, lets refresh some knowledge.

Public Internet

Internet cable goes from your PC to your ISP (Internet Service Provider) switch located somewhere in your apartments building, usually.

And then... it gets connected to another switch, and it goes on and on like that, travelling through other cables and switches until it reaches the target PC you wanted to reach. Each switch knows where to send the signal next (how: it's a different topic).

So the signal first travels to some main switch of your ISP (where it does all the pricing/monitoring etc). ISP itself is then connected to another bigger ISP (they have some sort of partnership where your ISP pays that another ISP for using its cables, just like you do with your own ISP). In total, lots of physical cables literally lay down around the world going undersea/underground/over-the-air allowing the whole world to be connected to each other.

Problem

There are millions of internet users. You can imagine how many switches and cables is needed to support all that big network, and how much traffic (hello TikTok) is traveling. Because of that:

Your signal doesn't travel via the most optimal route through switches, when it needs to go from your PC to another target machine (some AWS machine in our case). Especially when you're on different continents.

Big traffic also makes your ping fluctuate, depending on the load on each individual switch.

There are all sorts of switches, we can't really trust them. Moreover, ISPs aren't required to be compliant with PCI security standard, or any other. If you want a secure connection, you have to use VPN (IPSec, OSI layer 3), but it costs in terms of performance and ping speed.

AWS Direct Connect

AWS own internet

AWS came here and said - "let me create my own internet around the world. I literally lay down my own physical cables and I'll connect them to only selected (partner) data centers. So you can use those instead of public internet" AWS may still probably lease someone's cables (especially underseas one's), but most of the cables/switches are their own ones. Benefits:

It's a much smaller net. Less switches and they're more reliable. Cables go almost straight to AWS. Therefore it's faster.

AWS implements MACsec OSI layer 2 security at hardware level. So no VPN required (though you could still use it). It's faster.

How to connect

Obviously, AWS can't connect to each PC in the world just like public ISPs, otherwise it would be the same public internet network, sort of speaking, and wouldn't make sense.

AWS has a partnership with selected data centers around the world to which AWS did the physical connection and put their own switch there ("AWS Direct Connect Cage"). So if you put your server in such data center (or at least your server connects to such data center from other nearest location via public internet) you can quickly enter AWS network where your signal will travel much faster. So you don't even need a public ISP in such case.

You do this to:

Reduce latency and improve stability into your AWS environment.

Even reduce latency between non-AWS endpoints. When both endpoints use public internet to only connect to the nearest AWS cage, while then cage-to-cage traffics goes through AWS internal network. And voila!

Results

In our company case, we managed to decrease the latency around 5 times (i.e. 0.5 seconds vs 2.5 seconds) for non-AWS connectivity.

ContentLicense: CC BY-SA 4.0

Title: How AWS Direct Connect works?


问:
Python typing annotations in try/except blocks
I don't understand why mypy is complaining about wrong type in one of my methods:

```<code>    def check_earliest(self, symbol: str) -> Optional[datetime]:
        """Return earliest date of available data for a given symbol."""
        try:
            return self.read(symbol).index.min()
        except (KeyError, AttributeError):
            return None
</code>```

The error is:

Item "None" of "Optional[Any]" has no attribute "index"

I accept that self.read(symbol) may return None and then trying to access index attribute on it would result in AttributeError. But that's exactly why I put it in a try/except block in line with It's easier to ask for forgiveness than permission.

Then why is it an error?

答:
You may do it like this

```<code>def check_earliest(self, symbol: str) -> Optional[SomeType]:
        """Return earliest date of available data for a given symbol."""
        try:
            data = self.read(symbol) # data is Optional[SomeType] and have't index attr
            if data: # Check data is not None
                return data.index.min() # and here linter knows what type it is

        except (KeyError, AttributeError):
            return None
</code>```

Or if yours python version >=3.8, like this:

```<code>def check_earliest(self, symbol: str) -> Optional[SomeType]:
        """Return earliest date of available data for a given symbol."""
        try:
            if data := self.read(symbol):
                return data.index.min()

        except (KeyError, AttributeError):
            return None
</code>```

This is because Optional[SomeType] object must be checked in if expression to let linter knows what it is.

Body:
I don't understand why mypy is complaining about wrong type in one of my methods:

    def check_earliest(self, symbol: str) -> Optional[datetime]:
        """Return earliest date of available data for a given symbol."""
        try:
            return self.read(symbol).index.min()
        except (KeyError, AttributeError):
            return None

The error is:

Item "None" of "Optional[Any]" has no attribute "index"

I accept that self.read(symbol) may return None and then trying to access index attribute on it would result in AttributeError. But that's exactly why I put it in a try/except block in line with It's easier to ask for forgiveness than permission.

Then why is it an error?

Body:
You may do it like this

def check_earliest(self, symbol: str) -> Optional[SomeType]:
        """Return earliest date of available data for a given symbol."""
        try:
            data = self.read(symbol) # data is Optional[SomeType] and have't index attr
            if data: # Check data is not None
                return data.index.min() # and here linter knows what type it is

        except (KeyError, AttributeError):
            return None

Or if yours python version >=3.8, like this:

def check_earliest(self, symbol: str) -> Optional[SomeType]:
        """Return earliest date of available data for a given symbol."""
        try:
            if data := self.read(symbol):
                return data.index.min()

        except (KeyError, AttributeError):
            return None

This is because Optional[SomeType] object must be checked in if expression to let linter knows what it is.


问:
How to save Fragment recyclerview data on change fragment?
I have been trying to use onStateInstance but always return null and i dont know how to use ViewModel.

Im getting data from db to recyclerview and i dont want to download again data when navigate to another fragment. (I will update data with swipeRefreshLayout).

```<code>@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.fragment_search, container, false);

           initComponents(v);
           initRecyclerViewDB(v);

    return v;
}
</code>```

I need to save my list and dont call method initRecyclerView if my list is not null.

```<code>public void initRecyclerViewDB(View v){
    db.collection("post").orderBy("postBy", Query.Direction.ASCENDING).whereNotEqualTo("postBy",mAuth.getCurrentUser().getUid())
            .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            postList.clear();
            for (DocumentSnapshot ds:queryDocumentSnapshots.getDocuments()) {
                postList.add(ds.toObject(Post.class));
            }
            postAdapter.notifyDataSetChanged();
        }
    });
</code>```

In resume i want to save an arraylist to put in my recyclerview.

答:
The answer was using a view model:

```<code>public class YourViewModel extends ViewModel {
private final MutableLiveData<ArrayList<Post>> selectedItem = new MutableLiveData<>();

public void setData(ArrayList<YourList> item){
    selectedItem.setValue(item);
}

public LiveData<ArrayList<YourList>> getSelectedItem(){

    return selectedItem;
 }
}
</code>```

OnCreateView:

```<code> @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.fragment_search, container, false);

    viewModel = new ViewModelProvider(requireActivity()).get(PostViewModel.class);
    viewModel.getSelectedItem().observe(getViewLifecycleOwner(), new Observer<ArrayList<Post>>() {
        @Override
        public void onChanged(ArrayList<Post> posts) {
            postList.clear();
            postList.addAll(posts);
            postAdapter.notifyDataSetChanged();
        }
    });

    if (postList.isEmpty()){
        initRecyclerViewDB(v);
    }else {
        Toast.makeText(getContext(), "Save", Toast.LENGTH_SHORT).show();
    }

    initComponents(v);

    return v;
}
</code>```

If list is empty will import data from db, if not will use viewModel.

Body:
I have been trying to use onStateInstance but always return null and i dont know how to use ViewModel.

Im getting data from db to recyclerview and i dont want to download again data when navigate to another fragment. (I will update data with swipeRefreshLayout).

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.fragment_search, container, false);

           initComponents(v);
           initRecyclerViewDB(v);

    return v;
}

I need to save my list and dont call method initRecyclerView if my list is not null.

public void initRecyclerViewDB(View v){
    db.collection("post").orderBy("postBy", Query.Direction.ASCENDING).whereNotEqualTo("postBy",mAuth.getCurrentUser().getUid())
            .get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            postList.clear();
            for (DocumentSnapshot ds:queryDocumentSnapshots.getDocuments()) {
                postList.add(ds.toObject(Post.class));
            }
            postAdapter.notifyDataSetChanged();
        }
    });

In resume i want to save an arraylist to put in my recyclerview.

Body:
The answer was using a view model:

public class YourViewModel extends ViewModel {
private final MutableLiveData<ArrayList<Post>> selectedItem = new MutableLiveData<>();

public void setData(ArrayList<YourList> item){
    selectedItem.setValue(item);
}

public LiveData<ArrayList<YourList>> getSelectedItem(){

    return selectedItem;
 }
}

OnCreateView:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =inflater.inflate(R.layout.fragment_search, container, false);

    viewModel = new ViewModelProvider(requireActivity()).get(PostViewModel.class);
    viewModel.getSelectedItem().observe(getViewLifecycleOwner(), new Observer<ArrayList<Post>>() {
        @Override
        public void onChanged(ArrayList<Post> posts) {
            postList.clear();
            postList.addAll(posts);
            postAdapter.notifyDataSetChanged();
        }
    });

    if (postList.isEmpty()){
        initRecyclerViewDB(v);
    }else {
        Toast.makeText(getContext(), "Save", Toast.LENGTH_SHORT).show();
    }

    initComponents(v);

    return v;
}

If list is empty will import data from db, if not will use viewModel.


问:
jest .each name access object key
Is it possible to access an object's key inside the name portion of a .each?

```<code>let accounts =
    [
        {
            details:
            {
                company_name:
                    "company_name",
                email,
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "company_name",
                "email",
                "type",
            ],
            type:
                "creator"
        },
        {
            details:
            {
                email,
                first_name:
                    "first_name",
                last_name:
                    "last_name",
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "email",
                "first_name",
                "last_name",
                "type",
            ],
            type:
                "user"
        },
    ]

describe.each(accounts)(
    "%s", // <-- access the 'type' key, e.g. account.type
    function (account)
    {
        // test code
    }
)
</code>```

答:
Another alternative is to create a wrapper class and stick to a simple convention:

```<code>class TestCase {
  constructor(value) {
   this._value = value;
  }

  get value() {
   return this._value;
  }

  toString() {
    return JSON.stringify(this._value);
  }
}
</code>```

Then a test will look like this:

```<code>const testCases = accounts.map(TestCase)
describe.each(accounts)(
  "%s", // <-- you can customize this in TestCase toString
    function ({value: account})
    {
        // test code
    }
)
</code>```

Title: jest .each name access object key

Body:
Is it possible to access an object's key inside the name portion of a .each?

let accounts =
    [
        {
            details:
            {
                company_name:
                    "company_name",
                email,
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "company_name",
                "email",
                "type",
            ],
            type:
                "creator"
        },
        {
            details:
            {
                email,
                first_name:
                    "first_name",
                last_name:
                    "last_name",
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "email",
                "first_name",
                "last_name",
                "type",
            ],
            type:
                "user"
        },
    ]

describe.each(accounts)(
    "%s", // <-- access the 'type' key, e.g. account.type
    function (account)
    {
        // test code
    }
)

Body:
Another alternative is to create a wrapper class and stick to a simple convention:

class TestCase {
  constructor(value) {
   this._value = value;
  }

  get value() {
   return this._value;
  }

  toString() {
    return JSON.stringify(this._value);
  }
}

Then a test will look like this:

const testCases = accounts.map(TestCase)
describe.each(accounts)(
  "%s", // <-- you can customize this in TestCase toString
    function ({value: account})
    {
        // test code
    }
)


问:
How to remove blank space between two text views in SwiftUI
I have a problem with a Text view. I have a List of menu products, and the products contains a description. But, when a product don't have a description, there is a blank space. I do not want the blank space. How can I remove the blank space ?
This is what I Have : [First Picture]
This is what I want : Second Picture

I will share the code Below :

```<code>struct SectionVieww: View {
    @EnvironmentObject var syncViewModel : SyncViewModel
    @StateObject var coreDataViewModel = CoreDataViewModel()
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: [])
    private var menus : FetchedResults<LocalMenu>
    var menuType : MenuType
    var body: some View {
        let filteredMenus = syncViewModel.menu
            .filter({$0.type == menuType.id})
        if !filteredMenus.isEmpty {
            Section(header:
                        Text("\(menuType.text.capitalized)")
                        .font(.system(size: 20, weight: .bold, design: .rounded))
                        .foregroundColor(.black)
            ) {
                ForEach(filteredMenus) { men in
                    HStack {
                        WebImage(url: men.image.getThumbnailUrl(), options: .refreshCached)
                          .onFailure(perform: { (error) in
                          } )
                                .resizable()
                                .frame(width: 80, height: 80)
                                .cornerRadius(10)
                            VStack(alignment: .leading, spacing: 10) {
                                Spacer()
                                Text(men.name)
                                    .font(.system(size: 16))
                                    .foregroundColor(.black)
                                    .fontWeight(.semibold)

                                Text(men.welcomeDescription ?? "")
                                    .font(.caption)
                                    .foregroundColor(.gray)
                                HStack {
                                    Text("\(men.price) lei")
                                        .fontWeight(.bold)
                                    Text("\(men.ingredients.map({$0.grams}).reduce(0, +), specifier: "%.0f")g")
                                        .font(.system(size: 16))
                                        .fontWeight(.medium)
                                        .foregroundColor(.gray)
                                }
                                Spacer()
                            }

                        Spacer()
                        Button {
                                coreDataViewModel.addTask(name: men.name, grams: men.ingredients.map({$0.grams}).reduce(0, +), price: Int((men.price)), id: men.id)
                        } label: {
                            Image(systemName: "plus")
                                .font(.title)
                                .foregroundColor(Color.onboardingColor)
                        }
                        .buttonStyle(PlainButtonStyle())
                    }.background(NavigationLink("", destination:
                                ItemMenuView(meniu: men)
                                                    .toolbar {
                                                        ToolbarItem(placement: .navigationBarTrailing) {
                                                            ToolbarButtons(numberOfProducts: menus.count)
                                                    }
                    }
                                               ).opacity(0)
                    )
            }
            }

        } else {
            EmptyView()
        }
    }

}

</code>```
```<code>struct MeniuriView: View {
    @EnvironmentObject var syncViewModel : SyncViewModel

    var body: some View {

        List  {
            ForEach(syncViewModel.menuType) { type in
                SectionVieww(menuType: type)
            }
        }
        .listStyle(PlainListStyle())

    }
            }
</code>```

答:
Put it conditionally, like</p>
```<code>if let description = men.welcomeDescription {
  Text(description)
    .font(.caption)
    .foregroundColor(.gray)
}
</code>```

Body:
I have a problem with a Text view. I have a List of menu products, and the products contains a description. But, when a product don't have a description, there is a blank space. I do not want the blank space. How can I remove the blank space ?
This is what I Have : [First Picture]
This is what I want : Second Picture

I will share the code Below :

struct SectionVieww: View {
    @EnvironmentObject var syncViewModel : SyncViewModel
    @StateObject var coreDataViewModel = CoreDataViewModel()
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: [])
    private var menus : FetchedResults<LocalMenu>
    var menuType : MenuType
    var body: some View {
        let filteredMenus = syncViewModel.menu
            .filter({$0.type == menuType.id})
        if !filteredMenus.isEmpty {
            Section(header:
                        Text("\(menuType.text.capitalized)")
                        .font(.system(size: 20, weight: .bold, design: .rounded))
                        .foregroundColor(.black)
            ) {
                ForEach(filteredMenus) { men in
                    HStack {
                        WebImage(url: men.image.getThumbnailUrl(), options: .refreshCached)
                          .onFailure(perform: { (error) in
                          } )
                                .resizable()
                                .frame(width: 80, height: 80)
                                .cornerRadius(10)
                            VStack(alignment: .leading, spacing: 10) {
                                Spacer()
                                Text(men.name)
                                    .font(.system(size: 16))
                                    .foregroundColor(.black)
                                    .fontWeight(.semibold)

                                Text(men.welcomeDescription ?? "")
                                    .font(.caption)
                                    .foregroundColor(.gray)
                                HStack {
                                    Text("\(men.price) lei")
                                        .fontWeight(.bold)
                                    Text("\(men.ingredients.map({$0.grams}).reduce(0, +), specifier: "%.0f")g")
                                        .font(.system(size: 16))
                                        .fontWeight(.medium)
                                        .foregroundColor(.gray)
                                }
                                Spacer()
                            }

                        Spacer()
                        Button {
                                coreDataViewModel.addTask(name: men.name, grams: men.ingredients.map({$0.grams}).reduce(0, +), price: Int((men.price)), id: men.id)
                        } label: {
                            Image(systemName: "plus")
                                .font(.title)
                                .foregroundColor(Color.onboardingColor)
                        }
                        .buttonStyle(PlainButtonStyle())
                    }.background(NavigationLink("", destination:
                                ItemMenuView(meniu: men)
                                                    .toolbar {
                                                        ToolbarItem(placement: .navigationBarTrailing) {
                                                            ToolbarButtons(numberOfProducts: menus.count)
                                                    }
                    }
                                               ).opacity(0)
                    )
            }
            }

        } else {
            EmptyView()
        }
    }

}

struct MeniuriView: View {
    @EnvironmentObject var syncViewModel : SyncViewModel

    var body: some View {

        List  {
            ForEach(syncViewModel.menuType) { type in
                SectionVieww(menuType: type)
            }
        }
        .listStyle(PlainListStyle())

    }
            }

Body:
Put it conditionally, like

if let description = men.welcomeDescription {
  Text(description)
    .font(.caption)
    .foregroundColor(.gray)
}


问:
How avoid __res_querydomain - sendmail compilation problem
During sendmail compilation on Debian (raspberry) I've encountered problem with __res_querydomain  undefinded funtion.
How avoid it?</p>

答:
In sendmail/README is:

"""

```<code>  Fedora Core 5, 64 bit version
    If the ld stage fails with undefined functions like
    __res_querydomain, __dn_expand
    then add these lines to devtools/Site/site.config.m4

    APPENDDEF(`confLIBDIRS', `-L/usr/lib64')
    APPENDDEF(`confINCDIRS', `-I/usr/include/bind9')

    and rebuild (sh ./Build -c).

    Problem noted by Daniel Krones, solution suggested by
    Anthony Howe.
</code>```

"""

First of all this is not only Fedora Core 5, 64 bit version problem.

In my case that was on Debian.

Real solution is use proper parameter -L for glib library:

on my raspberry it was:

-L/usr/lib/arm-linux-gnueabihf/

example of compilation comand is:
./Build -c -I/usr/local/include/sasl2/ -L/usr/lib/ -L/usr/lib/sasl2 -L/usr/lib/arm-linux-gnueabihf/

Body:
In sendmail/README is:

"""

  Fedora Core 5, 64 bit version
    If the ld stage fails with undefined functions like
    __res_querydomain, __dn_expand
    then add these lines to devtools/Site/site.config.m4

    APPENDDEF(`confLIBDIRS', `-L/usr/lib64')
    APPENDDEF(`confINCDIRS', `-I/usr/include/bind9')

    and rebuild (sh ./Build -c).

    Problem noted by Daniel Krones, solution suggested by
    Anthony Howe.

"""

First of all this is not only Fedora Core 5, 64 bit version problem.

In my case that was on Debian.

Real solution is use proper parameter -L for glib library:

on my raspberry it was:

-L/usr/lib/arm-linux-gnueabihf/

example of compilation comand is:
./Build -c -I/usr/local/include/sasl2/ -L/usr/lib/ -L/usr/lib/sasl2 -L/usr/lib/arm-linux-gnueabihf/


问:
Delete image Doc from firebase firestore
I am trying to delete a document from firebase firestore collection
how can I do that , I tried few things but no success
here is my code

here is the Firestore hook

```<code>
const useFirestore = (somecollection) => {

    const [docs, setDocs] = useState([]);

    useEffect (() => {
        // new collection reference
          const newcoll = collection(projectFirestore, somecollection);
          const q = query(newcoll, orderBy('createdAt', 'desc'));
          const unsub = onSnapshot(q, (snapshot) => {
            let documents = [];
            snapshot.forEach(doc => {
               documents.push({...doc.data(), id: doc.id})
            })
            setDocs(documents);
          });

          return () => unsub;

    },[somecollection]);

    return { docs };
}
</code>```

the imagegrid where all the images are shown (only the js without the jsx return)

```<code>const { docs } = useFirestore('images');

  const handleDelete = (e) => {
    docs.forEach(doc => {
      deleteDoc(doc);
    })

</code>```

now , on the imagegrid jsx I have all the uploaded images that store the document in them.

so I added a button to every image and I want that when I click the button its fires a handleDelete() that delete this specific image document from the firestore

答:
If you are trying to have a button that deletes a single document, you likely do NOT want to be looping over all of the docs and calling deleteDoc().

Instead, your UI would likely display each image and include its ID for each button.  Something like:

```<code>const deleteOneImageDoc = async (docId) => {
  try {
    let docRef = doc(myFirestore, `images/${docId}`);
    await deleteDoc(docRef);
  } catch (ex) {
    console.error(`Delete FAILED: ${ex.message}`);
    throw ex;
  }
};

return <div>
  docs.map((imgDoc) => {
    return (
      <div key={imgDoc.id}>
        <img src={imgDoc.url}/>
        <button onClick={() => deleteOneImageDoc(imgDoc.id)}>
          delete {imgDoc.id}
        </button>
      </div>);
    })
  </div>;
</code>```

Body:
I am trying to delete a document from firebase firestore collection
how can I do that , I tried few things but no success
here is my code

here is the Firestore hook

const useFirestore = (somecollection) => {

    const [docs, setDocs] = useState([]);

    useEffect (() => {
        // new collection reference
          const newcoll = collection(projectFirestore, somecollection);
          const q = query(newcoll, orderBy('createdAt', 'desc'));
          const unsub = onSnapshot(q, (snapshot) => {
            let documents = [];
            snapshot.forEach(doc => {
               documents.push({...doc.data(), id: doc.id})
            })
            setDocs(documents);
          });

          return () => unsub;

    },[somecollection]);

    return { docs };
}

the imagegrid where all the images are shown (only the js without the jsx return)

const { docs } = useFirestore('images');

  const handleDelete = (e) => {
    docs.forEach(doc => {
      deleteDoc(doc);
    })

now , on the imagegrid jsx I have all the uploaded images that store the document in them.

so I added a button to every image and I want that when I click the button its fires a handleDelete() that delete this specific image document from the firestore

Body:
If you are trying to have a button that deletes a single document, you likely do NOT want to be looping over all of the docs and calling deleteDoc().

Instead, your UI would likely display each image and include its ID for each button.  Something like:

const deleteOneImageDoc = async (docId) => {
  try {
    let docRef = doc(myFirestore, `images/${docId}`);
    await deleteDoc(docRef);
  } catch (ex) {
    console.error(`Delete FAILED: ${ex.message}`);
    throw ex;
  }
};

return <div>
  docs.map((imgDoc) => {
    return (
      <div key={imgDoc.id}>
        <img src={imgDoc.url}/>
        <button onClick={() => deleteOneImageDoc(imgDoc.id)}>
          delete {imgDoc.id}
        </button>
      </div>);
    })
  </div>;


问:
How to swap 2 button positions in Kotlin programatically by preference?
So, I have 2 buttons and I wanna swap them by using a preference but Yeah, I can't make them switch properly

Here is my code for preference

```<code>    // In MainActivity.kt

    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {

                // Buttons swaped.

            } else {

               // Buttons back to normal.

            }
        }
</code>```

I tried again to make them swap but I got some ugly results like this:

```<code>    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {
                val posX = cleanBtn.x
                val posY = cleanBtn.y
                analyzeBtn.x = posX
                analyzeBtn.y = posY
            } else {
                cleanBtn.setPadding(0, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 0, 0)
            }
        }
    }
</code>```
```<code>    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {
                cleanBtn.setPadding(640, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 640, 0)
            } else {
                cleanBtn.setPadding(0, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 0, 0)
            }
        }
    }
</code>```

I have to mention I tried to use set.Margins but isn't working

How can I swap them by using this method?

答:
If they're in a LinearLayout and they're the only two children views of that LinearLayout, you can reverse the order of the children like this:

```<code>findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toList()
    removeAllViews()
    for (child in children.asReversed()) {
        addView(child)
    }
}
</code>```

If they need different sized margins, you can swap their layout params too:

```<code>findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toList()
    children[0].layoutParams = children[1].layoutParams
        .also { children[1].layoutParams = children[0].layoutParams }
    removeAllViews()
    for (child in children.asReversed()) {
        addView(child)
    }
}
</code>```

If there are more than two children views, you can search for and swap them in a mutable list before adding them all back to the parent.

```<code>findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toMutableList()
    val buttonAIndex = children.indexOf(findViewById(R.id.buttonA))
    val buttonBIndex = children.indexOf(findViewById(R.id.buttonB))
    children[buttonAIndex] = children[buttonBIndex]
        .also { children[buttonBIndex] = children[buttonAIndex] }
    children[buttonAIndex].layoutParams = children[buttonBIndex].layoutParams
        .also { children[buttonBIndex].layoutParams = children[buttonAIndex].layoutParams }
    removeAllViews()
    for (child in children) {
        addView(child)
    }
}
</code>```

Body:
So, I have 2 buttons and I wanna swap them by using a preference but Yeah, I can't make them switch properly

Here is my code for preference

    // In MainActivity.kt

    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {

                // Buttons swaped.

            } else {

               // Buttons back to normal.

            }
        }

I tried again to make them swap but I got some ugly results like this:

    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {
                val posX = cleanBtn.x
                val posY = cleanBtn.y
                analyzeBtn.x = posX
                analyzeBtn.y = posY
            } else {
                cleanBtn.setPadding(0, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 0, 0)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
        if (isButtonPositionFlipped != shouldFlipButtonPosition) {
            isButtonPositionFlipped = shouldFlipButtonPosition
            val cleanBtn = findViewById<Button>(R.id.cleanBtn)
            val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
            if (isButtonPositionFlipped) {
                cleanBtn.setPadding(640, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 640, 0)
            } else {
                cleanBtn.setPadding(0, 0, 0, 0)
                analyzeBtn.setPadding(0, 0, 0, 0)
            }
        }
    }

I have to mention I tried to use set.Margins but isn't working

How can I swap them by using this method?

Body:
If they're in a LinearLayout and they're the only two children views of that LinearLayout, you can reverse the order of the children like this:

findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toList()
    removeAllViews()
    for (child in children.asReversed()) {
        addView(child)
    }
}

If they need different sized margins, you can swap their layout params too:

findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toList()
    children[0].layoutParams = children[1].layoutParams
        .also { children[1].layoutParams = children[0].layoutParams }
    removeAllViews()
    for (child in children.asReversed()) {
        addView(child)
    }
}

If there are more than two children views, you can search for and swap them in a mutable list before adding them all back to the parent.

findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
    val children = children.toMutableList()
    val buttonAIndex = children.indexOf(findViewById(R.id.buttonA))
    val buttonBIndex = children.indexOf(findViewById(R.id.buttonB))
    children[buttonAIndex] = children[buttonBIndex]
        .also { children[buttonBIndex] = children[buttonAIndex] }
    children[buttonAIndex].layoutParams = children[buttonBIndex].layoutParams
        .also { children[buttonBIndex].layoutParams = children[buttonAIndex].layoutParams }
    removeAllViews()
    for (child in children) {
        addView(child)
    }
}


问:
How to get-adgroup members by their Name or SamAccountName
i would like to extract members from an AD Group that contains Members and security group.

```<code>Example, Group_A:
User1
User2
User3
Group_B
</code>```

When I run my script, it shows:

CN=User1,OU=Users,DC=Contoso,DC=com
CN=User2,OU=Users,DC=Contoso,DC=com
CN=User3,OU=Users,DC=Contoso,DC=com
CN=Group_B,OU=Users,DC=Contoso,DC=com

Is there another way to show their Name and/or SamAccountname?

```<code>$Groups =
@"
GroupNames;
Group_A
"@ | ConvertFrom-Csv -Delimiter ';'

$ADGroups =
Foreach ($Group in $Groups){
Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members }

$ADGroups.Members
</code>```

答:
As the other helpful answers show, if you want to play safe, you can use Get-ADGroupMember to get the group membership, this would also be useful because you would be able to distinguish the ObjectClass of each member.

You could also do string manipulation over the elements (distinguishedName) of the member attribute of the AD Group by following this Q&A.

If the members of the group are on different Domains, this should work however it would be quite slow most likely.

```<code>foreach($group in $groups) {
    $membership = Get-ADGroup $Group -Properties Member
    $membership.Member | Group-Object { ($_ -split '(?=DC=)',2)[1] } |
    ForEach-Object {
        [adsi]$ldap = 'LDAP://{0}' -f $_.Name
        [string]$domain = $ldap.Name

        foreach($member in $_.Group) {
            $obj = Get-ADObject $member -Server $domain
            [pscustomobject]@{
                MemberOf       = $membership.Name
                Domain         = $domain
                SamAccountName = $obj.SamAccountName
                ObjectClass    = $obj.ObjectClass
            }
        }
    }
}
</code>```

Body:
i would like to extract members from an AD Group that contains Members and security group.

Example, Group_A:
User1
User2
User3
Group_B

When I run my script, it shows:

CN=User1,OU=Users,DC=Contoso,DC=com
CN=User2,OU=Users,DC=Contoso,DC=com
CN=User3,OU=Users,DC=Contoso,DC=com
CN=Group_B,OU=Users,DC=Contoso,DC=com

Is there another way to show their Name and/or SamAccountname?

$Groups =
@"
GroupNames;
Group_A
"@ | ConvertFrom-Csv -Delimiter ';'

$ADGroups =
Foreach ($Group in $Groups){
Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members }

$ADGroups.Members

Body:
As the other helpful answers show, if you want to play safe, you can use Get-ADGroupMember to get the group membership, this would also be useful because you would be able to distinguish the ObjectClass of each member.

You could also do string manipulation over the elements (distinguishedName) of the member attribute of the AD Group by following this Q&A.

If the members of the group are on different Domains, this should work however it would be quite slow most likely.

foreach($group in $groups) {
    $membership = Get-ADGroup $Group -Properties Member
    $membership.Member | Group-Object { ($_ -split '(?=DC=)',2)[1] } |
    ForEach-Object {
        [adsi]$ldap = 'LDAP://{0}' -f $_.Name
        [string]$domain = $ldap.Name

        foreach($member in $_.Group) {
            $obj = Get-ADObject $member -Server $domain
            [pscustomobject]@{
                MemberOf       = $membership.Name
                Domain         = $domain
                SamAccountName = $obj.SamAccountName
                ObjectClass    = $obj.ObjectClass
            }
        }
    }
}