I have an example data set, where I would like the column "type" to be copied every 6 rows
Original data:
capacity_ex <- data.frame("type" = c("p", "t", "t", "r,"),
"1_q1" = 1:4,
"1_q2" = c("a", "b", "c", "d"),
'1_q3' = 10:13,
'1_q4' = 100:103,
'1_q5' = 110:113,
'1_q6' = 11:14,
"2_q1" = 22:25,
"2_q2" = c("i", "j", "k", "l"),
'2_q3' = 20:23,
'2_q4' = 200:203,
'2_q5' = 210:213,
'2_q6' = 21:24,
"3_q1" = 90:93,
"3_q2" = c("p", "q", "r", "s"),
'3_q3' = 10:13,
'3_q4' = 300:303,
'3_q5' = 310:313,
'3_q6' = 31:34,check.names = FALSE)
How I would like the data to be formatted
capacity_want <- data.frame("type" = c("p", "t", "t", "r,"),
"1_q1" = 1:4,
"1_q2" = c("a", "b", "c", "d"),
'1_q3' = 10:13,
'1_q4' = 100:103,
'1_q5' = 110:113,
'1_q6' = 11:14,
"type" = c("p", "t", "t", "r,"),
"2_q1" = 22:25,
"2_q2" = c("i", "j", "k", "l"),
'2_q3' = 20:23,
'2_q4' = 200:203,
'2_q5' = 210:213,
'2_q6' = 21:24,
"type" = c("p", "t", "t", "r,"),
"3_q1" = 90:93,
"3_q2" = c("p", "q", "r", "s"),
'3_q3' = 10:13,
'3_q4' = 300:303,
'3_q5' = 310:313,
'3_q6' = 31:34,check.names = FALSE)
Type could have any kind of additional name at the end to keep each name unique. This sample data is very abbreviated - my data set is much longer so don't want to do this manually
I tried to write a loop to do this but my looping skills are minimal. This is what I tried with many errors that I couldn't work out
capacity_test_fix <- do.call(
lapply(seq(2, (ncol(capacity_ex), 6), \(i){
capacity_ex %>%
mutate("type" = type, .after = (i+6)) %>%
})))
Happy to provide better context if needed!