Home > Enterprise >  R how to create new column based on having values above and below a certain threshold
R how to create new column based on having values above and below a certain threshold

Time:07-25

How do I create a column called "northsouth" that tells me if an ID visits latitudes that are both above and below 35.

df_input <- data.frame (ID  = c(12, 12, 12, 12, 13, 13), lat = c(32, 34, 40, 39, 32, 30))
df_result <- data.frame (portID  = c(12, 13), northsouth = c("yes", "no"))

CodePudding user response:

You could do

library(tidyverse)

df_input %>%
  group_by(ID) %>%
  summarize(northsouth = max(lat) > 35 & min(lat) < 35)
#> # A tibble: 2 x 2
#>      ID northsouth
#>   <dbl> <lgl>     
#> 1    12 TRUE      
#> 2    13 FALSE

Created on 2022-07-24 by the reprex package (v2.0.1)

  • Related