为一个变量创建不同的组
Create distinctive groups for one variable
我正在尝试根据一个变量定义不同的组。这是一个简单的问题,但我无法弄清楚。
在我的数据集中,每棵树都有许多独特的组("dist" 变量中的值为 1)。我想创建一个新变量,它将分配给每个组独特的独特价值。
我的数据如下:
Tree_ID dist
1 0
1 1
1 1
1 0
1 1
1 0
我想创建一个新变量,它将分配给不同的组 "dist == 1" 唯一值 (unique_gr)。
Tree_ID dist unique_gr
1 0 0
1 1 1
1 1 1
1 0 0
1 1 2
1 0 0
我曾尝试使用 "ifelse" 函数来检查当前行,当 "dist == 0" 表示没有组时
ifelse(dist == 1, "unique_gr", 0) # checking the current row
主要问题是如何在 "unique_gr" 中为每个不同的组指定唯一值 different/increasing(例如 1,2,3,4..)?
感谢您的帮助。
来自 tidyverse
和 data.table
的解决方案。关键是使用rleid
函数。
# Create example data frame
dt <- read.table(text = "Tree_ID dist
1 0
1 1
1 1
1 0
1 1
1 0 ",
header = TRUE, stringsAsFactors = FALSE)
library(tidyverse)
library(data.table)
dt2 <- dt %>%
mutate(unique_gr = rleid(dist)) %>%
mutate(unique_gr = ifelse(dist != 0 & first(dist) == 0, unique_gr/2,
ifelse(dist != 0 & first(dist) != 0, (unique_gr + 1)/2, 0)))
dt2
Tree_ID dist unique_gr
1 1 0 0
2 1 1 1
3 1 1 1
4 1 0 0
5 1 1 2
6 1 0 0
请注意,如果 dist
的开头不是 0
,此解决方案也适用,如以下示例所示。
# Create example data frame with the beginning of dist is not 0
dt_1 <- read.table(text = "Tree_ID dist
1 1
1 1
1 1
1 0
1 1
1 0 ",
header = TRUE, stringsAsFactors = FALSE)
dt2_1 <- dt_1 %>%
mutate(unique_gr = rleid(dist)) %>%
mutate(unique_gr = ifelse(dist != 0 & first(dist) == 0, unique_gr/2,
ifelse(dist != 0 & first(dist) != 0, (unique_gr + 1)/2, 0)))
dt2_1
Tree_ID dist unique_gr
1 1 1 1
2 1 1 1
3 1 1 1
4 1 0 0
5 1 1 2
6 1 0 0
这是另一个使用 data.table
的选项
library(data.table)
setDT(df1)[, unique_gr := rleid(dist)*dist, Tree_ID][unique_gr != 0,
unique_gr := match(unique_gr, unique(unique_gr))]
# Tree_ID dist unique_gr
#1: 1 0 0
#2: 1 1 1
#3: 1 1 1
#4: 1 0 0
#5: 1 1 2
#6: 1 0 0
我正在尝试根据一个变量定义不同的组。这是一个简单的问题,但我无法弄清楚。
在我的数据集中,每棵树都有许多独特的组("dist" 变量中的值为 1)。我想创建一个新变量,它将分配给每个组独特的独特价值。
我的数据如下:
Tree_ID dist
1 0
1 1
1 1
1 0
1 1
1 0
我想创建一个新变量,它将分配给不同的组 "dist == 1" 唯一值 (unique_gr)。
Tree_ID dist unique_gr
1 0 0
1 1 1
1 1 1
1 0 0
1 1 2
1 0 0
我曾尝试使用 "ifelse" 函数来检查当前行,当 "dist == 0" 表示没有组时
ifelse(dist == 1, "unique_gr", 0) # checking the current row
主要问题是如何在 "unique_gr" 中为每个不同的组指定唯一值 different/increasing(例如 1,2,3,4..)?
感谢您的帮助。
来自 tidyverse
和 data.table
的解决方案。关键是使用rleid
函数。
# Create example data frame
dt <- read.table(text = "Tree_ID dist
1 0
1 1
1 1
1 0
1 1
1 0 ",
header = TRUE, stringsAsFactors = FALSE)
library(tidyverse)
library(data.table)
dt2 <- dt %>%
mutate(unique_gr = rleid(dist)) %>%
mutate(unique_gr = ifelse(dist != 0 & first(dist) == 0, unique_gr/2,
ifelse(dist != 0 & first(dist) != 0, (unique_gr + 1)/2, 0)))
dt2
Tree_ID dist unique_gr
1 1 0 0
2 1 1 1
3 1 1 1
4 1 0 0
5 1 1 2
6 1 0 0
请注意,如果 dist
的开头不是 0
,此解决方案也适用,如以下示例所示。
# Create example data frame with the beginning of dist is not 0
dt_1 <- read.table(text = "Tree_ID dist
1 1
1 1
1 1
1 0
1 1
1 0 ",
header = TRUE, stringsAsFactors = FALSE)
dt2_1 <- dt_1 %>%
mutate(unique_gr = rleid(dist)) %>%
mutate(unique_gr = ifelse(dist != 0 & first(dist) == 0, unique_gr/2,
ifelse(dist != 0 & first(dist) != 0, (unique_gr + 1)/2, 0)))
dt2_1
Tree_ID dist unique_gr
1 1 1 1
2 1 1 1
3 1 1 1
4 1 0 0
5 1 1 2
6 1 0 0
这是另一个使用 data.table
library(data.table)
setDT(df1)[, unique_gr := rleid(dist)*dist, Tree_ID][unique_gr != 0,
unique_gr := match(unique_gr, unique(unique_gr))]
# Tree_ID dist unique_gr
#1: 1 0 0
#2: 1 1 1
#3: 1 1 1
#4: 1 0 0
#5: 1 1 2
#6: 1 0 0