R 如何根据另一列中的更改创建二进制变量?
我试图计算每个ID的柱面从西向东和从西向东的变化次数R 如何根据另一列中的更改创建二进制变量?,r,R,我试图计算每个ID的柱面从西向东和从西向东的变化次数 structure(list(ID = c(30767L, 30767L, 30767L, 30767L, 30767L, 30767L, 30767L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L
structure(list(ID = c(30767L, 30767L, 30767L, 30767L, 30767L,
30767L, 30767L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L,
30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L,
30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L,
30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L, 30759L,
30759L, 30759L, 30759L), shore = c("West", "West", "West", "West",
"West", "West", "West", "West", "West", "West", "West", "West",
"East", "West", "East", "East", "West", "West", "West", "West",
"West", "West", "West", "West", "West", "West", "East", "West",
"West", "West", "West", "West", "East", "East", "East", "East",
"East", "East", "East", "East")), row.names = c(NA, -40L), groups = structure(list(
ID = c(30759L, 30767L), .rows = list(8:40, 1:7)), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
基本上,我首先要做的是确定没有变化为-东西向运动为0,东西向运动为1。。见下面的例子
ID Shore Direction
1 30759 West -
2 30759 West -
3 30759 West -
4 30759 East 0
5 30759 West 1
6 30759 East 0
7 30759 East -
8 30759 West 1
9 30759 West -
10 30759 West -
按
ID
分组,然后使用case\u when
和lag
计算变量
library(dplyr)
DF %>%
group_by(ID) %>%
mutate(dir = case_when(
shore == "West" & lag(shore) == "East" ~ 1L,
shore == "East" & lag(shore) == "West" ~ 0L,
TRUE ~ NA_integer_)) %>%
ungroup
这里有一种方法,使用
dplyr
:
df %>%
dplyr::mutate(prev = lag(shore),
direction = dplyr::case_when(shore == "West" & prev == "East" ~ 1,
shore == "East" & prev == "West" ~ 0,
TRUE ~ NA_real_))
lag()
函数给出了shore
列的上一个条目(在本例中)。然后,我添加了一个方向列,当方向从东到西变化时,它是1
,当方向从西到东变化时,它是0
,反之亦然。然后,您可以删除prev
列。我认为这不是我想要的,请参阅编辑后的文章。请修改它。