使用标识符对唯一的观察进行编码。

huangapple go评论104阅读模式
英文:

Encode unique observations using identifier

问题

我有一个数据框,其中一个列由字符串组成,这些字符串是旅程的唯一标识符。一个可重现的数据框:

df <- data.frame(tours = c("ansc123123", "ansc123123", "ansc123123", "baa3999", "baa3999", "baa3999"),
                 order = rep(c(1, 2, 3), 2))

现在我的实际数据要大得多,包含更多观察值和唯一标识符,但我希望得到一个输出,格式类似于当你执行类似以下操作时(但不是手动编码),以便具有相同“tours”值的旅程被编码为相同的旅程。

df$journey <- c(1, 1, 1, 2, 2, 2)
英文:

I have a data frame where one column is consisting of strings, which is a unique identifier to a journey. A reproducible data frame:

df &lt;- data.frame(tours = c(&quot;ansc123123&quot;, &quot;ansc123123&quot;, &quot;ansc123123&quot;, &quot;baa3999&quot;, &quot;baa3999&quot;, &quot;baa3999&quot;),
                 order = rep(c(1, 2, 3), 2))

Now my real data is much larger with many more observations and unique identifiers, but I would like to have an output on the format as when you do something like this (but not manually encoded), so that the journeys with the same tours value get encoded as the same journey.

df$journey &lt;- c(1, 1, 1, 2, 2, 2)

答案1

得分: 2

你可以将它转换为一个 factor

df$journey <- as.integer(factor(df$tours))

df$journey
#[1] 1 1 1 2 2 2

或者使用 matchunique

match(df$tours, unique(df$tours))

也可以使用 factor 并使用 unclass 获取整数值。这里保存了 levels,可以用它们恢复原始值。

df$journey <- unclass(factor(df$tours))

df$journey
#[1] 1 1 1 2 2 2
#attr(,&quot;levels&quot;)
#[1] &quot;ansc123123&quot; &quot;baa3999&quot;   

levels(df$journey)[df$journey]
#[1] &quot;ansc123123&quot; &quot;ansc123123&quot; &quot;ansc123123&quot; &quot;baa3999&quot;    &quot;baa3999&quot;   
#[6] &quot;baa3999&quot;   
英文:

You can convert it to a factor.

df$journey &lt;- as.integer(factor(df$tours))

df$journey
#[1] 1 1 1 2 2 2

Or use match and unique.

match(df$tours, unique(df$tours))

Its also possible to use factor and get the integer values with unclass. Here the levels are saved, what allows to come back to the original values.

df$journey &lt;- unclass(factor(df$tours))

df$journey
#[1] 1 1 1 2 2 2
#attr(,&quot;levels&quot;)
#[1] &quot;ansc123123&quot; &quot;baa3999&quot;   

levels(df$journey)[df$journey]
#[1] &quot;ansc123123&quot; &quot;ansc123123&quot; &quot;ansc123123&quot; &quot;baa3999&quot;    &quot;baa3999&quot;   
#[6] &quot;baa3999&quot;   

答案2

得分: 0

使用`dplyr`的方法是使用`mutate`与`cur_group_id()`和`.by`命令:

df %>%
mutate(journey = cur_group_id(), .by = tours)

或者对于较旧版本的`dplyr`:

df %>%
group_by(tours) %>%
mutate(journey = group_indices())

输出:

tours order journey

1 ansc123123 1 1

2 ansc123123 2 1

3 ansc123123 3 1

4 baa3999 1 2

5 baa3999 2 2

6 baa3999 3 2


<details>
<summary>英文:</summary>

A `dplyr` approach could be to use `mutate` with `cur_group_id()` and the `.by` command:

df %>%
mutate(journey = cur_group_id(), .by = tours)

Or for older versions of `dplyr`:

df %>%
group_by(tours) %>%
mutate(journey = group_indices())

Output:

tours order journey

1 ansc123123 1 1

2 ansc123123 2 1

3 ansc123123 3 1

4 baa3999 1 2

5 baa3999 2 2

6 baa3999 3 2


</details>



# 答案3
**得分**: 0

```R
library(data.table)

setDT(df)

df[, journey := .GRP, tours]
英文:
library(data.table)

setDT(df)

df[, journey := .GRP, tours]

答案4

得分: 0

使用 dplyr 1.1.0 <https://dplyr.tidyverse.org/reference/group_data.html>

我们可以使用:group_indices() 返回一个整数向量,与 .data 长度相同,表示每行所属的组。

library(dplyr)
df %>%
  group_by(tours) %>%
  group_indices()

[1] 1 1 1 2 2 2
英文:

With dplyr 1.1.0 <https://dplyr.tidyverse.org/reference/group_data.html>

We could use: group_indices() returns an integer vector the same length as .data that gives the group that each row belongs to.

library(dplyr)
df %&gt;% 
  group_by(tours) %&gt;% 
  group_indices()

[1] 1 1 1 2 2 2

huangapple
  • 本文由 发表于 2023年3月7日 23:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定