2017-05-24 35 views
0

這裏是我的例子DF:從1到dataframes添加列獨特的長度現有分組的行

df = read.table(text = 'colA 
22 
22 
22 
45 
45 
11 
11 
87 
90 
110 
32 
32', header = TRUE) 

我只需要1基礎上添加可樂值的新關口,以可樂的獨特長度。

預期輸出:

colA newCol 
    22  1 
    22  1 
    22  1 
    45  2 
    45  2 
    11  3 
    11  3 
    87  4 
    90  5 
    110 6 
    32  7 
    32  7 

這裏是我的嘗試沒有更迭:

library(dplyr) 
new_df = df %>% 
    group_by(colA) %>% 
    mutate(newCol = seq(1, length(unique(df$colA)), by = 1)) 

感謝

+0

可樂聚集在你的例子中,或者你可能有一個序列像22 45 22?你能回到價值嗎? – G5W

+0

它們是聚類的。謝謝 – aaaaa

回答

1
newcol = c(1, 1+cumsum(diff(df$colA) != 0)) 
[1] 1 1 1 2 2 3 3 4 5 6 7 7 
+0

謝謝大家 – aaaaa

1

dplyr包有一個函數來獲取組指標:

df$newcol = group_indices(df,colA) 

這將返回:

colA newcol 
1 22  2 
2 22  2 
3 22  2 
4 45  4 
5 45  4 
6 11  1 
7 11  1 
8 87  5 
9 90  6 
10 110  7 
11 32  3 
12 32  3 

雖然根據出現的順序的索引未被排序。

你也可以做到這一點使用factor

df$newcol = as.numeric(factor(df$colA,levels=unique(df$colA))) 
1

另一種選擇:可以在所有因素都與底層整數相關的事實,利用。首先創建一個與列相同級別的新因子變量,然後將其轉換爲數字。

newCol <- factor(df$colA, 
    levels = unique(df$colA)) 

df$newCol <- as.numeric(newCol) 
df 

    colA newCol 
1 22  1 
2 22  1 
3 22  1 
4 45  2 
5 45  2 
6 11  3 
7 11  3 
8 87  4 
9 90  5 
10 110  6 
11 32  7 
12 32  7