问题
I am trying to write code based on a Group variable, item.map that has item information that includes a q-matrix showing which item is associated with which group.
group <- c(1,2)
ids <- c("54_a","54_b","44_a","44_c")
item.map <- data.frame(
item.id = c("54_a","54_b","44_a","44_c"),
group.1 = c(1,1,1,0),
group.2 = c(0,1,0,1))
factor <- c(54,44)
In this item.map group.1 had 3 items while group.2 has two items. Using this item.map I wanted to assign those items within the chunk of code below but I was not able to plug the item.map information.
library(stringr)
# define df for all ids and group combinations
group_g <- paste("G", 1:length(group), sep ="")
df <- data.frame(ids, group = rep(group_g, each = length(ids)))
# empty vector
vec <- NULL
for(i in 1:nrow(df)) {
res <- which(str_extract(df[i, "ids"], "[0-9]{2,}") == factor)
text <- paste("(", df[i, "group"], ", ", df[i, "ids"], ", fixed[", c(0:length(factor)) ,"]) = ", ifelse(res == 0:length(factor) | 0 == 0:length(factor), "1.0", "0.0"),";", sep = "")
vec <- c(vec, text)
}
> vec
"(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
"(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
"(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
"(G1, 44_c, fixed[0]) = 1.0;" "(G1, 44_c, fixed[1]) = 0.0;" "(G1, 44_c, fixed[2]) = 1.0;"
"(G2, 54_a, fixed[0]) = 1.0;" "(G2, 54_a, fixed[1]) = 1.0;" "(G2, 54_a, fixed[2]) = 0.0;"
"(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
"(G2, 44_a, fixed[0]) = 1.0;" "(G2, 44_a, fixed[1]) = 0.0;" "(G2, 44_a, fixed[2]) = 1.0;"
"(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"
So, based on the item.map
in the desired output, G1 should not have item 44_c and G2 should not have items 54_a and 44_a
The desired output is:
> vec
"(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
"(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
"(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
"(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
"(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"
回答1:
Here is an idea. I reshaped your item.map
dataset into a long format. Therefore item.map
got the same structur as your old dataset df
, but with the additional column used
with the required 0 and 1.
In the next step I added an if
-function in the loop, so just rows with 1 will be included in vec
.
library(stringr)
# original dataset item.map
group <- c(1,2)
ids <- c("54_a","54_b","44_a","44_c")
item.map <- data.frame(
item.id = c("54_a","54_b","44_a","44_c"),
group.1 = c(1,1,1,0),
group.2 = c(0,1,0,1))
factor <- c(54,44)
# reshape item.map
item.map2 <- item.map %>%
pivot_longer(-item.id,
names_to = "group",
values_to = "used") %>%
arrange(group) %>%
mutate(group = str_replace(group, "group.", "G"),
item.id = as.character(item.id))
# empty vector
vec <- NULL
for(i in 1:nrow(item.map2)) {
if(item.map2[i, "used"] == 1) {
res <- which(str_extract(item.map2[i, "item.id"], "[0-9]{2,}") == factor)
text <- paste("(", item.map2[i, "group"], ", ", item.map2[i, "item.id"],
", fixed[", c(0:length(factor)) ,"]) = ",
ifelse(res == 0:length(factor) | 0 == 0:length(factor),
"1.0", "0.0"),";", sep = "")
vec <- c(vec, text)
}
}
vec
Output
[1] "(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
[4] "(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
[7] "(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
[10] "(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
[13] "(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"
来源:https://stackoverflow.com/questions/64035136/manipulating-a-character-vector-by-considering-a-grouping-sequnce-in-r-4