How are you all doing?
I have dataframe (df1) in which I am generating 8 new columns (core_) using conditions from other variables partyid, vA, E3019. I am running each column individually and the code works, but I am trying to make it more elegant and trying to run the same code as a loop, and the loop don't work. First I tried:
df1$core_A <- ifelse(df1$partyid == df1$vA, df1$E3019_A, NA)
df1$core_B <- ifelse(df1$partyid == df1$vB, df1$E3019_B, NA)
df1$core_C <- ifelse(df1$partyid == df1$vC, df1$E3019_C, NA)
df1$core_D <- ifelse(df1$partyid == df1$vD, df1$E3019_D, NA)
df1$core_E <- ifelse(df1$partyid == df1$vE, df1$E3019_E, NA)
df1$core_F <- ifelse(df1$partyid == df1$vF, df1$E3019_F, NA)
df1$core_G <- ifelse(df1$partyid == df1$vG, df1$E3019_G, NA)
df1$core_H <- ifelse(df1$partyid == df1$vH, df1$E3019_H, NA)
Notice that the only differences between all the lines are the Letters at the end of the column names. So, the previous codes work. But since it is a repetitive operation and I wanted to make it more elegant, I tried to run the same code in a loop, but somehow it only gives me an error. This is what I am trying:
seq <- LETTERS[seq(1,8)]
for(i in seq){
df1$core_[[i]] <- ifelse(df1$partyid == df1$v[[i]], df1$E3019_[[i]], NA)
}
I don't understand what is wrong with the code. Can anyone tell me what am I doing wrong? Weren't they supposed to be equivalent?
EDIT: I apologize for not providing a sample of my data. Bellow there is a sample, the 10 first rows of my data. To simplify, instead of going from A to H, I restricted the data from A to C. Let me know if this is enough, I couldn't figure out how to make
structure(list(E1004 = c("AUS_2019", "AUS_2019", "AUS_2019",
"AUS_2019", "AUS_2019", "AUS_2019", "AUS_2019", "AUS_2019", "AUS_2019",
"AUS_2019"), partyid = c(NA, NA, 36002, 36002, 36001, 36001,
NA, NA, NA, 36001), vA = c(36001, 36001, 36001, 36001, 36001,
36001, 36001, 36001, 36001, 36001), vB = c(36002, 36002, 36002,
36002, 36002, 36002, 36002, 36002, 36002, 36002), vC = c(36003,
36003, 36003, 36003, 36003, 36003, 36003, 36003, 36003, 36003
), E3019_A = c(10L, 7L, NA, 0L, NA, 6L, 8L, NA, 8L, 8L), E3019_B = c(4L,
5L, NA, 9L, NA, 4L, 2L, 10L, 3L, 5L), E3019_C = c(2L, 3L, NA,
5L, NA, 3L, 5L, NA, 6L, 0L)), row.names = c(NA, 10L), class = "data.frame")
Is this enough? Not sure, it is the first time I am making a question using my original dataset.