My data looks like this:
company_name <- c("A","A","A","A","B","B","B")
year <- c(1985, 1986, 1987, 1988, 1985, 1986, 1987)
value <- c(0, 1, 0, 0, 1, 0, 0)
df <- data.frame(company_name, year, value)
If the value is 1, I want to switch this row of value (value=1) with the next row (value=0). (group_by should be used for company_name) My output must be like this:
value <- c(0, 0, 1, 0, 0, 1, 0)
new_df <- data.frame(company_name, year, value)
CodePudding user response:
Here is one potential solution:
library(dplyr)
company_name <- c("A","A","A","A","B","B","B")
year <- c(1985, 1986, 1987, 1988, 1985, 1986, 1987)
value <- c(0, 1, 0, 0, 1, 0, 0)
df <- data.frame(company_name, year, value)
answer <- df %>%
group_by(company_name) %>%
mutate(value = ifelse(lag(value, default = 0) == 1, lag(value), 0))
value <- c(0, 0, 1, 0, 0, 1, 0)
new_df <- data.frame(company_name, year, value)
all_equal(answer, new_df)
#> [1] TRUE
Created on 2022-11-25 by the reprex package (v2.0.1)
CodePudding user response:
Using dplyr::lag()
and dplyr::lead()
to test and replace values:
library(dplyr)
df %>%
group_by(company_name) %>%
mutate(value = case_when(
value == 1 ~ lead(value),
lag(value) == 1 ~ 1,
TRUE ~ value
))
# A tibble: 7 × 3
# Groups: company_name [2]
company_name year value
<chr> <dbl> <dbl>
1 A 1985 0
2 A 1986 0
3 A 1987 1
4 A 1988 0
5 B 1985 0
6 B 1986 1
7 B 1987 0