0

I would like to create a variable x that is just the values ​​of V1, which correspond to Sunday, that is, the variable x will only contain: 0,1,0,0,5,0,1,0,0,9,4.

df <- structure(
  list(date = c("01-08-2021","01-08-2021","01-08-2021","01-08-2021","01-08-2021",
                "08-08-2021","08-08-2021","08-08-2021","08-08-2021","08-08-2021","08-08-2021",
                "13-08-2021","13-08-2021","13-08-2021","13-08-2021","13-08-2021"),
       Week= c("Sunday","Sunday","Sunday","Sunday","Sunday","Sunday","Sunday","Sunday",
               "Sunday","Sunday","Sunday","Friday","Friday","Friday","Friday","Friday"),
       V1 = c(0,1,0,0,5,0,1,0,0,9,4,3,4,5,6,7), V2 = c(2,0,0,6,9,2,0,0,7,8,1,3,5,6,4,3)),
  class = "data.frame", row.names = c(NA, -16L))

1 Answers1

-1

Just subset using == to create a logical vector, and extract the values of 'V1', assign it to an object 'x'

x <- with(df, V1[Week == 'Sunday'])
x
#[1] 0 1 0 0 5 0 1 0 0 9 4
akrun
  • 874,273
  • 37
  • 540
  • 662