Home > Back-end >  Want to mutate values of one category with duplicate values from another category in R
Want to mutate values of one category with duplicate values from another category in R

Time:04-24

I have a Weather dataset with categorical values in a column named "City". In that, a city "Ahmedabad" is missing values entirely for the year 2019. Upon searching I found that "Hyderabad" double the number of values for the same year. I would like to extractpic of duplicate entries one set of values from "Hyderabad" and place that in "Ahmedabad"

CodePudding user response:

You could try this approach, using tidyverse and lubridate

library(tidyverse)
library(lubridate)

dt = dt %>% arrange(City, Date)
bind_rows(
  dt %>% filter(City!="Hyderabad" | year(Date)!=2019),
  dt %>% filter(City=="Hyderabad" & year(Date)==2019) %>% 
    mutate(City=if_else(row_number()%%2==0,"Ahmedabad", City))
)

Or, using data.table

library(data.table)

dt = setDT(dt)[order(City,Date)]
rbind(
  dt[City!="Hyderabad" | year(Date)!=2019],
  dt[City=="Hyderabad" & year(Date)==2019][seq(1,.N,2), City:="Ahmedabad"]
)
  • Related