将列添加到dataframes,从1添加到现有分组行的唯一长度
以下是我的示例:将列添加到dataframes,从1添加到现有分组行的唯一长度,r,dataframe,dplyr,col,R,Dataframe,Dplyr,Col,以下是我的示例: df = read.table(text = 'colA 22 22 22 45 45 11 11 87 90 110 32 32', header = TRUE) 我只需要在colA的基础上添加一个新的col,其值从1到colA的唯一长度 预期产出: colA newCol 22 1 22 1 22 1 45 2 45 2 11 3 11 3
df = read.table(text = 'colA
22
22
22
45
45
11
11
87
90
110
32
32', header = TRUE)
我只需要在colA的基础上添加一个新的col,其值从1到colA的唯一长度
预期产出:
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))
感谢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
虽然索引不是按照出现的顺序排列的
您也可以使用系数:
df$newcol = as.numeric(factor(df$colA,levels=unique(df$colA)))
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
虽然索引不是按照出现的顺序排列的
您也可以使用系数:
df$newcol = as.numeric(factor(df$colA,levels=unique(df$colA)))
另一种选择:您可以利用因子与基础整数相关联的事实。首先创建与列具有相同级别的新因子变量,然后将其转换为数值
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
newCol另一个选项:您可以利用因子与基础整数相关联的事实。首先创建与列具有相同级别的新因子变量,然后将其转换为数值
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
newCol是cola的值,如您的示例所示,或者您可能有一个类似22 45 22的序列?你能回到一个值吗?它们是聚集的。感谢您的示例中的cola集群值,或者您可能有22 45 22这样的序列吗?你能回到一个值吗?它们是聚集的。谢谢