Reputation: 1979
I have a list of question ids as below:
ids <- c("1_a","1_b","1_c","2_a","2_b","2_c","3_a","3_b","3_c")
1_,2_,3_
at the beginning of the ids represent grouping (factor) so there are three factors for a total of 9 questions. Considering this grouping factor variable, I would like to generate a character variable as below.
#for the first factor
(1_a, fixed[2]) = 0.0;
(1_a, fixed[3]) = 0.0;
(1_b, fixed[2]) = 0.0;
(1_b, fixed[3]) = 0.0;
(1_c, fixed[2]) = 0.0;
(1_c, fixed[3]) = 0.0;
#for the second factor
(2_a, fixed[1]) = 0.0;
(2_a, fixed[3]) = 0.0;
(2_b, fixed[1]) = 0.0;
(2_b, fixed[3]) = 0.0;
(2_c, fixed[1]) = 0.0;
(2_c, fixed[3]) = 0.0;
#for the third factor
(3_a, fixed[1]) = 0.0;
(3_a, fixed[2]) = 0.0;
(3_b, fixed[1]) = 0.0;
(3_b, fixed[2]) = 0.0;
(3_c, fixed[1]) = 0.0;
(3_c, fixed[2]) = 0.0;
The logic behind the desired output is similar to factor analysis. When it is the first question, the same question coefficient is fixed for other factors. For example, for questions 1_a, I need to have two lines that are fixed for the second and third factors so that the coefficient will be freely estimated for the first factor.
For the first factor, the coefficients in the[]
should be 2
and 3
.
For the second factor, the coefficients in the[]
should be 1
and 3
. and
For the third factor, the coefficients in the[]
should be 1
and 2
Did anyone have anything similar before?
Thanks!
Upvotes: 1
Views: 97
Reputation: 886968
If we want to create a single vector
based on the unique numeric part in 'ids', loop over the unique integers ('un1') with lapply
, extract the elements that partially matches the 'ids' with grep
, do an outer
paste with format
with the rest of the unique values (setdiff
), and unlist
the list
to create a single vector
un1 <- as.integer(unique(sub("_\\D+", "", ids)))
v1 <- unlist(lapply(un1, function(x) c(outer(grep(x, ids, value = TRUE),
setdiff(un1, x), FUN = function(u, v)
sprintf('(%s, fixed[%d]) = 0.0;', u, v)))))
cat(paste(v1, collapse="\n"), "\n")
#(1_a, fixed[2]) = 0.0;
#(1_b, fixed[2]) = 0.0;
#(1_c, fixed[2]) = 0.0;
#(1_a, fixed[3]) = 0.0;
#(1_b, fixed[3]) = 0.0;
#(1_c, fixed[3]) = 0.0;
#(2_a, fixed[1]) = 0.0;
#(2_a, fixed[1]) = 0.0;
#(2_a, fixed[1]) = 0.0;
#(2_a, fixed[3]) = 0.0;
#(2_a, fixed[3]) = 0.0;
#(2_a, fixed[3]) = 0.0;
#(3_a, fixed[1]) = 0.0;
#(3_a, fixed[1]) = 0.0;
#(3_a, fixed[1]) = 0.0;
#(3_a, fixed[2]) = 0.0;
#(3_a, fixed[2]) = 0.0;
#(3_a, fixed[2]) = 0.0;
Upvotes: 2