I have a dataset like the following one:
ProszęAveryextendedname <- c("A","A","A","A","B","B","B")
var2 <- c("B","B","B","B","B","B","B")
var3 <- c("B","B","B","B","B","B","B")
ProszęBveryextendedname <- c("A","A","A","A","B","B","B")
var5 <- c("B","B","B","B","B","B","B")
var6 <- c("B","B","B","B","B","B","B")
df <- data.frame(ProszęAveryextendedname , var2, var3, ProszęBveryextendedname, var5, var6)
please just to the special alphabet case, as long as possible. What I would like to do is to create a code so that every time under the column that has in its name head the word 'Proszę', there is a row with value 'A', the adjacent rows should have a NA value. How would it be possible to make this with a tidyverse, iterative function or via a loop?
I would like to add the small example of this
structure(list(`Prosze podac, ile godzin dziennie sluchasz zazwyczaj tresci AUDIO wymienione wyzej, BEZ PRZERYWANIA INNA AKTYWNOSCIA.
` = c("nigdy",
"okolo 1-2 godzin", "okolo 1-2 godzin", "mniej niz 1 godzine",
"okolo 1-2 godzin"), `ogladanie tresci video, w tym granie w gry video, itp....19` = c("nigdy",
"nigdy", "mniej niz 1 godzine", "nigdy", "nigdy"), `sluchanie muzyki, audycji radiowych, podcastów, audiobooków, itp.2` = c("nigdy",
"okolo 1-2 godzin", "okolo 3- 4 godzin", "mniej niz 1 godzine",
"nigdy"), `czytanie lub wykonywanie zadan zwiazanych ze szkola/studiami, itp.` = c("nigdy",
"nigdy", "okolo 1-2 godzin", "mniej niz 1 godzine", "okolo 3- 4 godzin"
), `pisanie e-maili lub wysylanie postów w social mediach, itp.` = c("nigdy",
"nigdy", "mniej niz 1 godzine", "mniej niz 1 godzine", "okolo 1-2 godzin"
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
the solution that I would like to achieve is described above this the sample dataset
EXPECTED OUTCOME
Prosze podac, ile godzin dziennie sluchasz zazwyczaj tresci AUDIO wymienione wyzej, BEZ PRZERYWANIA INNA AKTYWNOSCIA.~1 oglad~2 sluch~3 czyta~4
<chr> <chr> <chr> <chr>
1 nigdy NA NA NA
2 okolo 1-2 godzin nigdy okolo ~ nigdy
3 okolo 1-2 godzin mniej ~ okolo ~ okolo ~
4 mniej niz 1 godzine nigdy mniej ~ mniej ~
5 okolo 1-2 godzin nigdy nigdy okolo ~
SIMPLEST EXPECTED OUTCOME
ProszeAveryextendedname var2 var3 ProszeBveryextendedname var5 var6
1 A NA NA A NA NA
2 A NA NA A NA NA
3 A NA NA A NA NA
4 A NA NA A NA NA
5 B B B B B B
6 B B B B B B
7 B B B B B B
CodePudding user response:
library(dplyr)
library(purrr)
ind <- grepl("Proszę", names(df));
purrr::map_dfc(split.default(df, cumsum(ind)),
~ .x %>% mutate(across(-1,
~ replace(.x, cur_data()[[1]] == "A", NA))))
-output
ProszęAveryextendedname var2 var3 ProszęBveryextendedname var5 var6
1 A <NA> <NA> A <NA> <NA>
2 A <NA> <NA> A <NA> <NA>
3 A <NA> <NA> A <NA> <NA>
4 A <NA> <NA> A <NA> <NA>
5 B B B B B B
6 B B B B B B
7 B B B B B B