group by summarize question. again. thanks

name place  first_time money_spent city
Anna Florida TRUE          44      Tampa
Anna Florida FALSE         22      Fort
Anna Florida FALSE         289     Bay
Anna Florida FALSE         0       Miami
Ryan Mexico  FALSE         0       Cancun
Ryan Mexico  TRUE         122      Tijuana
Ryan Mexico  FALSE         0       Puebla

hi, so I am curious for within each group(name, place) how many cities have a non-zero, positive value value for money_spent where first-time == FALSE

so for group "Anna/Florida", the result is 2. (Fort, 22, and Bay 289, but Miami has 0)
so for "Ryan/Mexico", the result is 0, since Cancun, 0 and Puebla, 0.

thank you

dat <- data.frame(
  name =
    c("Anna", "Anna", "Anna", "Anna", "Ryan", "Ryan", "Ryan"),
  place =
    c("Florida", "Florida", "Florida", "Florida", "Mexico", "Mexico", "Mexico"),
  first_time =
    c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
  money_spent =
    c(44, 22, 289, 0, 0, 122, 0),
  city =
    c("Tampa", "Fort", "Bay", "Miami", "Cancun", "Tijuana", "Puebla"))

# data have no duplicates of cities meeting the test
dat[which(dat[3] == TRUE & dat[4] > 0), c(1,5)] 
#>   name    city
#> 1 Anna   Tampa
#> 6 Ryan Tijuana

Created on 2022-11-13 by the reprex package (v2.0.1)

hi techno that is not what I am looking for. I am trying to do a group by and summarize to get number of "city" that have non-zero positive value in money_spent, not including any row that is has "TRUE" in the value of first_time.thank u so much

Just reverse the first test in which()

is there a tidy verse solution sorry this confuses me

your_data %>% filter(something == elsewise & whatever > however)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.