为每组字符分配自定义因子值(Assign customized factor values for each group of characters)

我的数据框中有一列包含一些字符和一系列因子。 我想为每组值分配一个因子,以便第一组字符获得第一个因子,第二组获得第二个因子等。

数据帧的col +因子向量:

df$charac :

charac 1 0 2 0 3 0 4 1 5 1 6 2 7 2 8 2 9 3 10 4 11 4 12 4

vec_factor :

[1] 39 42 76 89 68 Levels: 39 42 68 76 89

预期结果:

charac factor 1 0 39 2 0 39 3 0 39 4 1 42 5 1 42 6 2 76 7 2 76 8 2 76 9 3 89 10 4 68 11 4 68 12 4 68

数据:

矢量因素:

structure(c(1L, 2L, 4L, 5L, 3L), .Label = c("39", "42", "68", "76", "89"), class = "factor")

字符组:

structure(list(test_vector = c("0", "0", "0", "1", "1", "2", "2", "2", "3", "4", "4", "4")), .Names = "test_vector", row.names = c(NA, -12L), class = "data.frame")

I have one column of my dataframe that contains some characters and a vector of factors. I would like for each group of value to assign a factor so that the first group of characters gets the first factor, the second group the second factor etc.

Col of the dataframe + vector of factors :

df$charac :

charac 1 0 2 0 3 0 4 1 5 1 6 2 7 2 8 2 9 3 10 4 11 4 12 4

vec_factor :

[1] 39 42 76 89 68 Levels: 39 42 68 76 89

Results expected :

charac factor 1 0 39 2 0 39 3 0 39 4 1 42 5 1 42 6 2 76 7 2 76 8 2 76 9 3 89 10 4 68 11 4 68 12 4 68

Datas :

Vector of factors :

structure(c(1L, 2L, 4L, 5L, 3L), .Label = c("39", "42", "68", "76", "89"), class = "factor")

col of characters :

structure(list(test_vector = c("0", "0", "0", "1", "1", "2", "2", "2", "3", "4", "4", "4")), .Names = "test_vector", row.names = c(NA, -12L), class = "data.frame")

最满意答案

您可以使用rleid的data.table :

library(data.table) df$factor<-vec_factor[rleid(df$test_vector)]

结果

df test_vector factor 1 0 39 2 0 39 3 0 39 4 1 42 5 1 42 6 2 76 7 2 76 8 2 76 9 3 89 10 4 68 11 4 68 12 4 68

You can use rleid from data.table:

library(data.table) df$factor<-vec_factor[rleid(df$test_vector)]

Result

df test_vector factor 1 0 39 2 0 39 3 0 39 4 1 42 5 1 42 6 2 76 7 2 76 8 2 76 9 3 89 10 4 68 11 4 68 12 4 68

更多推荐