第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為每個數(shù)據(jù)組中的行創(chuàng)建一個序號(計數(shù)器

為每個數(shù)據(jù)組中的行創(chuàng)建一個序號(計數(shù)器

互換的青春 2019-07-08 12:14:21
為每個數(shù)據(jù)組中的行創(chuàng)建一個序號(計數(shù)器我們?nèi)绾卧跀?shù)據(jù)的每一組中生成唯一的標識號?以下是按“PersonId”分組的一些數(shù)據(jù):personid date measurement1         x     231         x     322         y     213         x     233         z     233         y     23我希望為“PersonId”定義的每個子集中的每一行添加一個id列,其值總是以1..這是我想要的輸出:personid date measurement id1         x     23         11         x     32         22         y     21         13         x     23         13         z     23         23         y     23         3我很感謝你的幫助。
查看完整描述

3 回答

?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

被誤導的名字ave()函數(shù),帶參數(shù)FUN=seq_along,就能很好地完成這一任務(wù)-即使你personid列沒有嚴格的排序。


df <- read.table(text = "personid date measurement

1         x     23

1         x     32

2         y     21

3         x     23

3         z     23

3         y     23", header=TRUE)


## First with your data.frame

ave(df$personid, df$personid, FUN=seq_along)

# [1] 1 2 1 1 2 3


## Then with another, in which personid is *not* in order

df2 <- df[c(2:6, 1),]

ave(df2$personid, df2$personid, FUN=seq_along)

# [1] 1 1 1 2 3 2


查看完整回答
反對 回復 2019-07-08
?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

一些dplyr替代品,使用方便函數(shù)row_number和n.


library(dplyr)

df %>% group_by(personid) %>% mutate(id = row_number())

df %>% group_by(personid) %>% mutate(id = 1:n())

df %>% group_by(personid) %>% mutate(id = seq_len(n()))

df %>% group_by(personid) %>% mutate(id = seq_along(personid))

您也可以使用getanID從包裝splitstackshape..注意,輸入數(shù)據(jù)集作為data.table.


getanID(data = df, id.vars = "personid")

#    personid date measurement .id

# 1:        1    x          23   1

# 2:        1    x          32   2

# 3:        2    y          21   1

# 4:        3    x          23   1

# 5:        3    z          23   2

# 6:        3    y          23   3


查看完整回答
反對 回復 2019-07-08
?
溫溫醬

TA貢獻1752條經(jīng)驗 獲得超4個贊

使用data.table,并假設(shè)您希望通過date在personid子集


library(data.table)

DT <- data.table(Data)


DT[,id := order(date), by  = personid]


##    personid date measurement id

## 1:        1    x          23  1

## 2:        1    x          32  2

## 3:        2    y          21  1

## 4:        3    x          23  1

## 5:        3    z          23  3

## 6:        3    y          23  2

如果你不想date


DT[, id := 1:.N, by = personid]


##    personid date measurement id

## 1:        1    x          23  1

## 2:        1    x          32  2

## 3:        2    y          21  1

## 4:        3    x          23  1

## 5:        3    z          23  2

## 6:        3    y          23  3

以下任何一項都將有效


DT[, id := seq_along(measurement), by =  personid]

DT[, id := seq_along(date), by =  personid]

使用的等效命令plyr


library(plyr)

# ordering by date

ddply(Data, .(personid), mutate, id = order(date))

# in original order

ddply(Data, .(personid), mutate, id = seq_along(date))

ddply(Data, .(personid), mutate, id = seq_along(measurement))


查看完整回答
反對 回復 2019-07-08
  • 3 回答
  • 0 關(guān)注
  • 695 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號