0
target weight         week.3        week.4         week.5
7.4                     5.0           6.9            7.5

how can I match or determine in what week they the target weight match maybe greater than or equal to the target weight? what code can I use?

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 2
    Chai Decena, welcome to SO! Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Jul 17 '19 at 04:47

1 Answers1

0

Hopefully your data is in a data.frame as it should be. Then you can do it like this.

library(tidyr)
library(dplyr)

df <- data.frame(target=7.4, week3=5.0, week4=6.9, week5=7.5)
df <- df %>% 
  gather(key=week, value=weight, starts_with("week")) %>% 
  mutate(achieved = weight > target) %>% 
  print()
#>   target  week weight achieved
#> 1    7.4 week3    5.0    FALSE
#> 2    7.4 week4    6.9    FALSE
#> 3    7.4 week5    7.5     TRUE
df$week[df$achieved]
#> [1] "week5"

Created on 2019-07-17 by the reprex package (v0.3.0)

Simon Woodward
  • 1,946
  • 1
  • 16
  • 24