0

I have collected several different datasets and formatted them into one and it looks something like this:

df <- data.frame(database = c("A","A","A","B","B","B","C","C","C"),
                         longitude = c(45,45,45,11,11,11,43,43,43),
                         latitude = c(12,12,12,42,42,42,12,12,12),
                         interval = c(0,2,2,2,0,4,0,2,3))

I noticed that from one of the datasets longitude and latitude are switched around. So for database==B I want to switch the values between the longitude and latitude columns. So it ends up like this:

df1 <- data.frame(database = c("A","A","A","B","B","B","C","C","C"),
                         longitude = c(45,45,45,42,42,42,43,43,43),
                         latitude = c(12,12,12,11,11,11,12,12,12),
                         interval = c(0,2,2,2,0,4,0,2,3))
starski
  • 141
  • 6
  • Using the dplyr package, try: df2 <- df %>% mutate(longitude2 = case_when(longitude < mean(longitude) ~ latitude, TRUE ~ longitude), latitude2 = case_when(latitude > mean(latitude) ~ longitude, TRUE ~ latitude)) This works for at least the example sample you have provided. – fsure Aug 11 '23 at 15:35

0 Answers0