Eu tenho esse dataframe vazio
new_df<-structure(list(id = c("R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22",
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22",
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22",
"R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22", "R_88j7lG37gLfxk22",
"R_88j7lG37gLfxk22", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4",
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4",
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4",
"R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4", "R_6DK8lERVf8lSQf4",
"R_6DK8lERVf8lSQf4"), choice = c(0, 1, 0, 0, 0, 1, 0, 0, 1, 0,
1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1), low_env = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), mid_env = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), high_env = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), low_eth = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), mid_eth = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), high_eth = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
`low_pri($25)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
`mid_pri($75)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
`high_pri($125)` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), row.names = c(NA, 24L), class = "data.frame")
e com os dados e código acima preencho metade dele com base na primeira linha do long
conjunto de dados. Como posso usar um for()
loop para aplicar esse método ao restante das linhas do long
conjunto de dados que preencherão a outra metade?
long<-structure(list(id = c("R_88j7lG37gLfxk22", "R_6DK8lERVf8lSQf4"
), t1_choice = c("2", "3"), t2_choice = c("1", "3"), t3_choice = c("1",
"2"), t4_choice = c("2", "1"), t1_p1_env = c("high_env", "mid_env"
), t1_p1_eth = c("low_eth", "mid_eth"), t1_p1_pri = c("$125",
"$25"), t1_p2_env = c("mid_env", "high_env"), t1_p2_eth = c("high_eth",
"low_eth"), t1_p2_pri = c("$25", "$75"), t1_p3_env = c("low_env",
"low_env"), t1_p3_eth = c("mid_eth", "low_eth"), t1_p3_pri = c("$75",
"$75"), t2_p1_env = c("high_env", "mid_env"), t2_p1_eth = c("low_eth",
"high_eth"), t2_p1_pri = c("$75", "$125"), t2_p2_env = c("mid_env",
"low_env"), t2_p2_eth = c("mid_eth", "low_eth"), t2_p2_pri = c("$125",
"$75"), t2_p3_env = c("mid_env", "high_env"), t2_p3_eth = c("mid_eth",
"high_eth"), t2_p3_pri = c("$75", "$75"), t3_p1_env = c("high_env",
"mid_env"), t3_p1_eth = c("high_eth", "mid_eth"), t3_p1_pri = c("$125",
"$125"), t3_p2_env = c("mid_env", "high_env"), t3_p2_eth = c("low_eth",
"low_eth"), t3_p2_pri = c("$25", "$25"), t3_p3_env = c("low_env",
"low_env"), t3_p3_eth = c("high_eth", "high_eth"), t3_p3_pri = c("$25",
"$75"), t4_p1_env = c("low_env", "high_env"), t4_p1_eth = c("low_eth",
"low_eth"), t4_p1_pri = c("$75", "$125"), t4_p2_env = c("high_env",
"mid_env"), t4_p2_eth = c("mid_eth", "mid_eth"), t4_p2_pri = c("$125",
"$25"), t4_p3_env = c("low_env", "low_env"), t4_p3_eth = c("high_eth",
"mid_eth"), t4_p3_pri = c("$25", "$125")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))
#working
# Loop through the first three rows of new_df
for (i in 1:3) {
# Extracting the required values from long1 for each row
env <- long[1,][paste0("t1_p", i, "_env")][1]
eth <- long[1,][paste0("t1_p", i, "_eth")][1]
pri <- long[1,][paste0("t1_p", i, "_pri")][1]
# Matching values from long[1,] to new_df columns in the corresponding row
new_df[i, "low_env"] <- as.numeric(env == "low_env")
new_df[i, "mid_env"] <- as.numeric(env == "mid_env")
new_df[i, "high_env"] <- as.numeric(env == "high_env")
new_df[i, "low_eth"] <- as.numeric(eth == "low_eth")
new_df[i, "mid_eth"] <- as.numeric(eth == "mid_eth")
new_df[i, "high_eth"] <- as.numeric(eth == "high_eth")
new_df[i, "low_pri($25)"] <- as.numeric(pri == "$25")
new_df[i, "mid_pri($75)"] <- as.numeric(pri == "$75")
new_df[i, "high_pri($125)"] <- as.numeric(pri == "$125")
}
# Loop through the second three rows of new_df
for (i in 1:3) {
# Extracting the required values from long[1,] for each row
env <- long[1,][paste0("t2_p", i, "_env")][1]
eth <- long[1,][paste0("t2_p", i, "_eth")][1]
pri <- long[1,][paste0("t2_p", i, "_pri")][1]
# Matching values from long[1,] to new_df columns in the corresponding row
new_df[i + 3, "low_env"] <- as.numeric(env == "low_env")
new_df[i + 3, "mid_env"] <- as.numeric(env == "mid_env")
new_df[i + 3, "high_env"] <- as.numeric(env == "high_env")
new_df[i + 3, "low_eth"] <- as.numeric(eth == "low_eth")
new_df[i + 3, "mid_eth"] <- as.numeric(eth == "mid_eth")
new_df[i + 3, "high_eth"] <- as.numeric(eth == "high_eth")
new_df[i + 3, "low_pri($25)"] <- as.numeric(pri == "$25")
new_df[i + 3, "mid_pri($75)"] <- as.numeric(pri == "$75")
new_df[i + 3, "high_pri($125)"] <- as.numeric(pri == "$125")
# Adjusting the choice column
new_df[i + 3, "choice"] <- as.numeric(long[1,][paste0("t2_choice")][1] == i)
}
# Loop through the second three rows of new_df
for (i in 1:3) {
# Extracting the required values from long[1,] for each row
env <- long[1,][paste0("t3_p", i, "_env")][1]
eth <- long[1,][paste0("t3_p", i, "_eth")][1]
pri <- long[1,][paste0("t3_p", i, "_pri")][1]
# Matching values from long[1,] to new_df columns in the corresponding row
new_df[i + 6, "low_env"] <- as.numeric(env == "low_env")
new_df[i + 6, "mid_env"] <- as.numeric(env == "mid_env")
new_df[i + 6, "high_env"] <- as.numeric(env == "high_env")
new_df[i + 6, "low_eth"] <- as.numeric(eth == "low_eth")
new_df[i + 6, "mid_eth"] <- as.numeric(eth == "mid_eth")
new_df[i + 6, "high_eth"] <- as.numeric(eth == "high_eth")
new_df[i + 6, "low_pri($25)"] <- as.numeric(pri == "$25")
new_df[i + 6, "mid_pri($75)"] <- as.numeric(pri == "$75")
new_df[i + 6, "high_pri($125)"] <- as.numeric(pri == "$125")
# Adjusting the choice column
new_df[i + 6, "choice"] <- as.numeric(long[1,][paste0("t3_choice")][1] == i)
}
for (i in 1:3) {
# Extracting the required values from long[1,] for each row
env <- long[1,][paste0("t4_p", i, "_env")][1]
eth <- long[1,][paste0("t4_p", i, "_eth")][1]
pri <- long[1,][paste0("t4_p", i, "_pri")][1]
# Matching values from long[1,] to new_df columns in the corresponding row
new_df[i + 9, "low_env"] <- as.numeric(env == "low_env")
new_df[i + 9, "mid_env"] <- as.numeric(env == "mid_env")
new_df[i + 9, "high_env"] <- as.numeric(env == "high_env")
new_df[i + 9, "low_eth"] <- as.numeric(eth == "low_eth")
new_df[i + 9, "mid_eth"] <- as.numeric(eth == "mid_eth")
new_df[i + 9, "high_eth"] <- as.numeric(eth == "high_eth")
new_df[i + 9, "low_pri($25)"] <- as.numeric(pri == "$25")
new_df[i + 9, "mid_pri($75)"] <- as.numeric(pri == "$75")
new_df[i + 9, "high_pri($125)"] <- as.numeric(pri == "$125")
# Adjusting the choice column
new_df[i + 9, "choice"] <- as.numeric(long[1,][paste0("t4_choice")][1] == i)
}
resultado esperado
Sua abordagem não parece divertida no momento. Em vez disso, você pode atribuir dados mais favoráveis
names
(ou seja, vezes como um sufixo em vez do formato de prefixo estranho)reshape
duas vezes ao formato longo desejado e usá-losmodel.matrix
para obter a codificação one-hot. Não tenho certeza de como você obteve a variável de escolha, já que a codificou, mas você saberá disso. Para obter todos os níveis, precisamostype.convert(as.is=FALSE)
obterfactor
s e definircontrasts
comoFALSE
.Dados:
Você pode usar dplyr
oi, mas se eu executar isso
outras duas consultas foram respondidas, mas ainda estão faltando, acho que não será capaz de cobrir todos os níveis, você pode ver em um caso que está chegando na 24ª linha e, neste caso, está chegando na 34ª linha, mas não consigo retificar mais