M.O
M.O

Reputation: 473

Append value to vector if condition met in other column

I'm looping over several dataframes, and I'm trying to fill a vector depending on the value in df["TEST"].

For each row of the dataframe - except for the last two - if the value in df["TEST"] is 1, I want to append the corresponding value in df["HOD"] to my vector v.

Here is the structure of the dataframes I'm looping over:

df <- structure(list(GAME = structure(c("Tottenham/Crystal Palace", 
"Valencia/Leganes", "Atl. Madrid/Celta Vigo", "Granada CF/Barcelona", 
"Napoli/Cagliari", "Brescia/Juventus", "CASH", "TOTAL"), .Names = c(NA, 
NA, NA, NA, NA, NA, "GAME", "GAME")), WEIGHT = c(2.23823589057186, 
2.61685996579251, 2.63527504587693, 0.457669317949777, 2.64922876342808, 
11.1833333333333, 78.2193976830475, 100), RECO = structure(c("Tottenham", 
"Valencia", "Atl. Madrid", "Granada CF", "Napoli", "Juventus", 
"/", "/"), .Names = c(NA, NA, NA, NA, NA, NA, "RECO", "RECO")), 
    SIDE = structure(c("back", "back", "back", "back", "back", 
    "back", "/", "/"), .Names = c(NA, NA, NA, NA, NA, NA, "SIDE", 
    "SIDE")), LEAGUE = structure(c("Premier League", "Liga", 
    "Liga", "Liga", "Serie A", "Serie A", "/", "/"), .Names = c(NA, 
    NA, NA, NA, NA, NA, "LEAGUE", "LEAGUE")), IDGAME = structure(c("G233051", 
    "G233135", "G233113", "G233112", "G233175", "G233158", "/", 
    "/"), .Names = c(NA, NA, NA, NA, NA, NA, "IDGAME", "IDGAME"
    )), TEST = structure(c("1", "0", "0", "1", "0", "1", "1", 
    "1"), .Names = c(NA, NA, NA, NA, NA, NA, "TEST", "TEST")), 
    HOD = structure(c("1.3", "1.5", "1.36", "8", "1.25", "1.36", 
    "1", "1"), .Names = c(NA, NA, NA, NA, NA, NA, "HOD", "HOD"
    )), AMOUNT = c(0.283340944091571, 0.331271416201643, 0.333602603096921, 
    0.0579369034228574, 0.335369020801678, 1.41571147085297, 
    1, 1), LONG = c(-0.283340944091571, -0.331271416201643, -0.333602603096921, 
    -0.0579369034228574, -0.335369020801678, -1.41571147085297, 
    93.452071771034, 96.2093041295016), SHORT = c(0, 0, 0, 0, 
    0, 0, 1, 1), CTR = c(0.368343227319042, 0, 0, 0.463495227382859, 
    0, 1.92536760036004, 93.452071771034, 96.2092778260959)), row.names = c(NA, 
-8L), class = "data.frame")

Below is what I've tried. I also tried with ifelse but couldn't find a workaround.

v <-vector()
  for(j in seq(38)){  
    n <-paste0("FWKD",j)
    if(exists(n)){
      df<-get(n)
      apply(df, 1, function(x){
        if(df[[x,"TEST"]] ==1){
          v <- append(v, df[[x,"HOD"]])
        }
      })
    }
  }
  assign("OUTPUT1",as.data.frame(v))

Thank you for your help

Upvotes: 0

Views: 762

Answers (2)

guilherme santos
guilherme santos

Reputation: 108

The following code might work, i'm assuming that you have a list of data frames, since you said you are trying to loop over several of them. At the end, with the do.call function, you will have a vector with all the values of HOD, over every data.frame. altough, i'm not entirely sure from the question if that's what you want.

get_hod <- function(df){
    df = df[1:(nrow(df) - 2), ]
    output <- df[df$TEST == 1, "HOD"] 
     
    return(output)
}

## df_list is a list of data.frame objects
df_hod <- lapply(df_list, get_hod) # using lapply instead of for loop 
my_vector <- do.call(c, df_hod)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389065

You can do :

#Remove last 2 rows
tmp <- head(df, -2)
#Get the corresponding value of HOD where TEST = 1
v <- as.numeric(tmp$HOD[tmp$TEST == 1])
v
#[1] 1.30 8.00 1.36

Upvotes: 1

Related Questions