40

我有一个数据框 ( all_data),其中有一个站点列表 (1... 到 n) 及其分数,例如

  site  score
     1    10
     1    11  
     1    12
     4    10 
     4    11
     4    11
     8    9
     8    8
     8    7

我想创建一个按数字顺序对站点的每个级别进行编号的列,例如计数器。在示例中,站点(1、4 和 8)将在“数字”列中具有从 1 到 3 的对应计数器:

site  score number
     1    10    1
     1    11    1 
     1    12    1 
     4    10    2
     4    11    2
     4    11    2
     8    9     3
     8    8     3 
     8    7     3

我相信这一定很容易解决,但我还没有找到办法。

4

11 回答 11

29

尝试Data$number <- as.numeric(as.factor(Data$site))

附带说明:一方面我和@Chase 的解决方案与另一方面@DWin 的解决方案之间的区别在于数字的顺序。两者as.factorfactor都会自动对级别进行排序,而 @DWin 的解决方案不会发生这种情况:

Dat <- data.frame(site = rep(c(1,8,4), each = 3), score = runif(9))

Dat$number <- as.numeric(factor(Dat$site))
Dat$sitenum <- match(Dat$site, unique(Dat$site) ) 

> Dat
  site     score number sitenum
1    1 0.7377561      1       1
2    1 0.3131139      1       1
3    1 0.7862290      1       1
4    8 0.4480387      3       2
5    8 0.3873210      3       2
6    8 0.8778102      3       2
7    4 0.6916340      2       3
8    4 0.3033787      2       3
9    4 0.6552808      2       3
于 2011-05-24T15:32:31.650 回答
23

另外两个选项:

1)使用包中的.GRP功能data.table

library(data.table)
setDT(dat)[, num := .GRP, by = site]

使用下面的示例数据集会导致:

> dat
    site      score num
 1:    1 0.14945795   1
 2:    1 0.60035697   1
 3:    1 0.94643075   1
 4:    8 0.68835336   2
 5:    8 0.50553372   2
 6:    8 0.37293624   2
 7:    4 0.33580504   3
 8:    4 0.04825135   3
 9:    4 0.61894754   3
10:    8 0.96144729   2
11:    8 0.65496051   2
12:    8 0.51029199   2

2)使用以下group_indices功能dplyr

dat$num <- group_indices(dat, site)

或者当您想解决非标准评估时:

library(dplyr)
dat %>% 
  mutate(num = group_indices_(dat, .dots = c('site')))

这导致:

   site      score num
1     1 0.42480366   1
2     1 0.98736177   1
3     1 0.35766187   1
4     8 0.06243182   3
5     8 0.55617002   3
6     8 0.20304632   3
7     4 0.90855921   2
8     4 0.25215078   2
9     4 0.44981251   2
10    8 0.60288270   3
11    8 0.46946587   3
12    8 0.44941782   3

可以看出,dplyr给出了组号的不同顺序。


如果每次组更改时都需要另一个号码,还有其他几个选项:

1)以 R 为底:

# option 1:
dat$num <- cumsum(c(TRUE, head(dat$site, -1) != tail(dat$site, -1)))

# option 2:
x <- rle(dat$site)$lengths
dat$num <- rep(seq_along(x), times=x)

2)data.table包:

library(data.table)
setDT(dat)[, num := rleid(site)]

这一切都导致:

> dat
   site      score num
1     1 0.80817855   1
2     1 0.07881334   1
3     1 0.60092828   1
4     8 0.71477988   2
5     8 0.51384565   2
6     8 0.72011650   2
7     4 0.74994627   3
8     4 0.09564052   3
9     4 0.39782587   3
10    8 0.29446540   4
11    8 0.61725367   4
12    8 0.97427413   4

使用数据:

dat <- data.frame(site = rep(c(1,8,4,8), each = 3), score = runif(12))
于 2016-09-26T15:43:39.577 回答
16

这应该是相当有效和可以理解的:

Dat$sitenum <- match(Dat$site, unique(Dat$site))  
于 2011-05-24T15:38:52.037 回答
14

在新的dplyr1.0.0 中,我们可以使用cur_group_id()它为组提供唯一的数字标识符。

library(dplyr)
df %>% group_by(site) %>% mutate(number = cur_group_id())

#  site score number
#  <int> <int>  <int>
#1     1    10      1
#2     1    11      1
#3     1    12      1
#4     4    10      2
#5     4    11      2
#6     4    11      2
#7     8     9      3
#8     8     8      3
#9     8     7      3

数据

df <- structure(list(site = c(1L, 1L, 1L, 4L, 4L, 4L, 8L, 8L, 8L), 
score = c(10L, 11L, 12L, 10L, 11L, 11L, 9L, 8L, 7L)), 
class = "data.frame", row.names = c(NA, -9L))
于 2020-06-10T00:12:29.500 回答
10

使用来自@Jaap 的数据,使用的另一种dplyr可能性dense_rank()可能是:

dat %>%
 mutate(ID = dense_rank(site))

   site     score ID
1     1 0.1884490  1
2     1 0.1087422  1
3     1 0.7438149  1
4     8 0.1150771  3
5     8 0.9978203  3
6     8 0.7781222  3
7     4 0.4081830  2
8     4 0.2782333  2
9     4 0.9566959  2
10    8 0.2545320  3
11    8 0.1201062  3
12    8 0.5449901  3

或类似rleid()dplyr方法,首先排列数据:

dat %>%
 arrange(site) %>%
 mutate(ID = with(rle(site), rep(seq_along(lengths), lengths)))

   site     score ID
1     1 0.1884490  1
2     1 0.1087422  1
3     1 0.7438149  1
4     4 0.4081830  2
5     4 0.2782333  2
6     4 0.9566959  2
7     8 0.1150771  3
8     8 0.9978203  3
9     8 0.7781222  3
10    8 0.2545320  3
11    8 0.1201062  3
12    8 0.5449901  3

或使用duplicated()and cumsum()

df %>%
 mutate(ID = cumsum(!duplicated(site)))

与以下相同base R

df$ID <- with(rle(df$site), rep(seq_along(lengths), lengths))

或者:

df$ID <- cumsum(!duplicated(df$site))
于 2019-02-21T19:43:54.837 回答
3

您可以将站点转换为一个因子,然后返回该因子的数字或整数值:

dat <- data.frame(site = rep(c(1,4,8), each = 3), score = runif(9))
dat$number <- as.integer(factor(dat$site))
dat

  site     score number
1    1 0.5305773      1
2    1 0.9367732      1
3    1 0.1831554      1
4    4 0.4068128      2
5    4 0.3438962      2
6    4 0.8123883      2
7    8 0.9122846      3
8    8 0.2949260      3
9    8 0.6771526      3
于 2011-05-24T15:35:29.650 回答
2

使用该软件包的另一种解决方案data.table

Jaap 提供的更完整数据集的示例:

setDT(dat)[, number := frank(site, ties.method = "dense")]
dat
    site     score number
 1:    1 0.3107920      1
 2:    1 0.3640102      1
 3:    1 0.1715318      1
 4:    8 0.7247535      3
 5:    8 0.1263025      3
 6:    8 0.4657868      3
 7:    4 0.6915818      2
 8:    4 0.3558270      2
 9:    4 0.3376173      2
10:    8 0.7934963      3
11:    8 0.9641918      3
12:    8 0.9832120      3
于 2018-06-21T11:11:10.153 回答
0

另一种方法。我认为即使您对 R 知之甚少,也很容易获得:

library(dplyr)
df <- data.frame('site' = c(1, 1, 1, 4, 4, 4, 8, 8, 8))
df <- mutate(df, 'number' = cumsum(site != lag(site, default=-1)))
于 2020-02-25T18:15:41.250 回答
0

如果您想保留现有列并分配回相同的数据框...

my_df <- my_df %>%
    select(everything()) %>% 
    group_by(geo) %>% 
    mutate(geo_id = cur_group_id())

你可以像这样做多列......

my_df <- my_df %>%
    select(everything()) %>% 
    group_by(geo) %>% 
    mutate(geo_id = cur_group_id()) %>% 
    group_by(state) %>% 
    mutate(state_id = cur_group_id()) %>% 
    group_by(name) %>% 
    mutate(name_id = cur_group_id())
于 2021-06-07T09:42:02.150 回答
0

我最近也需要一个解决方案。没有找到这个线程,开始我的并被重定向到这里(谢谢)。很高兴看到许多解决方案,但对我来说(我觉得这是一种很好的做法),可扩展的解决方案很重要。因此,对以下几种解决方案进行了基准测试。

df <- data.table(country = rep(c('a', 'b', 'b', 'c', 'c', 'c'), 1e7)
                 )

a <-
microbenchmark(factor = {df[, group_id := as.integer(factor(country))]}
               , unique_match = df[, group_id := match(country, unique(country))]
               , rle = df[ , group_id := with(rle(country), rep(seq_along(lengths), lengths))]
               , dup_cumsum = df[,  group_id := cumsum(!duplicated(country))]
               , frank = df[, group_id := frank(country, ties.method = "dense")]
               , GRP = df[, group_id := .GRP, country]
               , rleid = df[, group_id := rleid(country)]
               , cumsum_head_tail = df[, group_id := cumsum(c(TRUE, head(country, -1) != tail(country, -1)))]
               , times = 50
               )
autoplot(a)

基准 50 倍

看来讲台是由data.table. 尽管如此,学习替代品还是很棒的,例如cumsum(!duplicated(country))。什么脑筋急转弯!

于 2021-10-18T14:11:31.130 回答
0

如果site列的数字是无序的,我们可以as_factor()结合使用fct_inorder()fromforcats包:

library(tibble)
library(dplyr)
library(forcats)
all_data_unordered <- tibble(site  = c(1,1,1,8,8,8,4,4,4),
                             score = c(10,11,12,10,11,11,9,8,7))

all_data_unordered |> 
  mutate(number = as_factor(site) |> fct_inorder() |> as.integer())
#> # A tibble: 9 × 3
#>    site score number
#>   <dbl> <dbl>  <int>
#> 1     1    10      1
#> 2     1    11      1
#> 3     1    12      1
#> 4     8    10      2
#> 5     8    11      2
#> 6     8    11      2
#> 7     4     9      3
#> 8     4     8      3
#> 9     4     7      3

reprex 包于 2021-11-05 创建(v2.0.1)

于 2021-11-05T13:11:21.937 回答