在R数据帧中为每组变量值添加行号

时间:2017-04-18 12:26:22

标签: r dataframe tidyverse

我有一个像这样的R数据框:

blogger; date; word
joe; 2016-11-19; ears
paul; 2017-04-02; dilapidated
joe; 2016-11-21; night
joe; 2016-11-20; girl
paul; 2017-04-01; crumpled
paul; 2017-04-03; bellow

我想先按blogger然后按date对其进行排序。然后,我想为每个博客添加一个包含行号的列。

我有这个:

# make linenumbers
df <- df %>%
    arrange(blogger,date) %>%
    mutate(linenumber = row_number())

但显然,对于每个博主来说,这并不适用于亚麻1号。我得到的输出是:

blogger; date; word; linenumber
joe; 2016-11-19; ears; 1
joe; 2016-11-20; girl; 2
joe; 2016-11-21; night; 3
paul; 2017-04-01; crumpled; 4
paul; 2017-04-02; dilapidated; 5
paul; 2017-04-03; bellow; 6

但我想要的输出是:

blogger; date; word; linenumber
joe; 2016-11-19; ears; 1
joe; 2016-11-20; girl; 2
joe; 2016-11-21; night; 3
paul; 2017-04-01; crumpled; 1
paul; 2017-04-02; dilapidated; 2
paul; 2017-04-03; bellow; 3

3 个答案:

答案 0 :(得分:2)

如果不需要对行进行排序,您可以执行以下操作:

d$linenumber <- ave(as.integer(d$blogger), d$blogger, FUN=seq_along)
# > d
#   blogger        date         word linenumber
# 1     joe  2016-11-19         ears          1
# 2    paul  2017-04-02  dilapidated          1
# 3     joe  2016-11-21        night          2
# 4     joe  2016-11-20         girl          3
# 5    paul  2017-04-01     crumpled          2
# 6    paul  2017-04-03       bellow          3

数据:

d <- read.table(header=TRUE, sep=';', text=
"blogger; date; word
joe; 2016-11-19; ears
paul; 2017-04-02; dilapidated
joe; 2016-11-21; night
joe; 2016-11-20; girl
paul; 2017-04-01; crumpled
paul; 2017-04-03; bellow")

答案 1 :(得分:1)

在博客上试用group_by

df %>% arrange(blogger, date) %>% 
group_by(blogger) %>% 
mutate(linenumber = row_number()) %>%
ungroup()

答案 2 :(得分:1)

我们可以使用rowid

中的便捷功能(data.table)来完成此操作
library(data.table)
setDT(d)[, linenumber := rowid(blogger)]
d   
#   blogger        date         word linenumber
#1:     joe  2016-11-19         ears          1
#2:    paul  2017-04-02  dilapidated          1
#3:     joe  2016-11-21        night          2
#4:     joe  2016-11-20         girl          3
#5:    paul  2017-04-01     crumpled          2
#6:    paul  2017-04-03       bellow          3