简体   繁体   English

为每组字符分配自定义因子值

[英]Assign customized factor values for each group of characters

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 : 数据帧的col +因子向量:

df$charac : 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 : 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")

You could do this in base R: 你可以在基地R做到这一点:

df$factor<- as.factor(df$test_vector)
levels(df$factor) <- levels(vec_factor)

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

So you first make a column of type factor and then replace the levels of which with the levels of vec_factor . 因此,您首先创建一个类型因子列,然后将其级别替换为vec_factor的级别。


OR (thanks to @alexis_laz for pointing this out) 或者(感谢@alexis_laz指出这一点)

df$factor <- factor(df$test_vector, labels = levels(vec_factor))

You can use rleid from data.table : 您可以使用rleiddata.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

We can do 我们可以做的

df1$factor <- as.character(vec_factor)[as.integer(df1[[1]])+1]
df1$factor
#[1] "39" "39" "39" "42" "42" "76" "76" "76" "89" "68" "68" "68"

Or use match 或者使用match

df1$factor <- with(df1, vec_factor[match(test_vector, unique(test_vector))])
df1$factor
#[1] 39 39 39 42 42 76 76 76 89 68 68 68
#Levels: 39 42 68 76 89

NOTE: Both the methods are in base R 注意:两种方法都在base R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM