Вы можете попробовать этот подход, используя pivot_longer()
library(tidyverse)
df <- data.frame(Doc_Name = c("Doc1", "Doc2", "Doc3"),
Q1_Value = c("yes", "no", "yes"),
Q1_Text = c("positive", "negative", "positive"),
Q2_Value= as.character(c(1,0,2)),
Q2_Text = c("1 for rate", "0 for rate", "2 for rate"))
# Doc_Name Q1_Value Q1_Text Q2_Value Q2_Text
# 1 Doc1 yes positive 1 1 for rate
# 2 Doc2 no negative 0 0 for rate
# 3 Doc3 yes positive 2 2 for rate
df2 <- df %>%
pivot_longer(-c(Doc_Name, Q1_Text, Q2_Text), names_to = "Question", values_to = "Value") %>%
pivot_longer(-c(Doc_Name, Question, Value), values_to = "Text") %>%
mutate(Question = str_replace_all(Question, regex("(?<=)_Value.*"), "")) %>%
select(Doc_Name, Question, Value, Text)
# Doc_Name Question Value Text
# <chr> <chr> <chr> <chr>
# 1 Doc1 Q1 yes positive
# 2 Doc1 Q1 yes 1 for rate
# 3 Doc1 Q2 1 positive
# 4 Doc1 Q2 1 1 for rate
# 5 Doc2 Q1 no negative
# 6 Doc2 Q1 no 0 for rate
# 7 Doc2 Q2 0 negative
# 8 Doc2 Q2 0 0 for rate
# 9 Doc3 Q1 yes positive
# 10 Doc3 Q1 yes 2 for rate
# 11 Doc3 Q2 2 positive
# 12 Doc3 Q2 2 2 for rate