samedi 14 août 2021

R - ifelse isn't acting as a vectorized function?

I'm expecting the ifelse below to return multiple results, one for each row in the DF.

heuristics = 'rightsize'

vmStorage <- structure(
  list(
    Disk.Used.GB = c(
      46.14,4.38,57.89,57.79,79.34,108.49,
      42.69,5.05,24.07,627.59,24.15
      )
    ,
    Disk.Allocated.GB = c(
      74.53,14.98,78.61,75,250,
      150,74.65,100,79.51,800,50
    )
  ),
  row.names = c(NA,-11L),
  class = c("tbl_df", "tbl", "data.frame"))


ifelse(heuristics == 'exact',
       vmStorage$Disk.Allocated.GB,
       vmStorage$Disk.Used.GB * 1.25)

I'm getting [1] 57.675. I use this with DFs all the time. Feeling dense at the moment.

Aucun commentaire:

Enregistrer un commentaire