用字符串替换R中Dataframe中的特殊数字

时间:2016-07-21 18:33:21

标签: r dataframe

我需要替换R中的数字向量中的特定值。我有一个如下数据框。

X1 = 1:20   
X2 = c(letters[1:20])   
df = data.frame(X1, X2) 
df <- df[sample(nrow(df)),]
df
   X1 X2
1  2  b
2  18 r
3  16 p
4  7  g
5  3  c
6  1  a
7  14 n
8  10 j
9  8  h
10 11 k
11 17 q
12 6  f
13 15 o
14 13 m
15 19 s
16 5  e
17 4  d
18 9  i
19 12 l
20 20 t

在这个特定的数据框中,我需要X1中的所有特定数值,字符串使得1:5 = c(“w”),6:10 = c(“x”),11:15 = c (“y”)和16:20 = c(“z”)。所以上面的数据框看起来如下:

   X1 X2
1  w  b
2  z  r
3  z  p
4  x  g
5  w  c
6  w  a
7  y  n
8  x  j
9  x  h
10 y  k
11 z  q
12 x  f
13 y  o
14 y  m
15 z  s
16 w  e
17 w  d
18 x  i
19 y  l
20 z  t

谢谢!

2 个答案:

答案 0 :(得分:0)

我们可以使用findInterval

df$X1 <-  with(df, findInterval(X1, c(1, 6, 11, 16)))
df$X1
#[1] 1 4 4 2 1 1 3 2 2 3 4 2 3 3 4 1 1 2 3 4

注意:这会返回OP职位中显示的预期输出。

基于新输出

v1 <- setNames(1:4, c("w", "x", "y", "z"))
df$X1 <- names(v1[with(df, findInterval(X1, c(1, 6, 11, 16)))])
df$X1
#[1] "w" "z" "z" "x" "w" "w" "y" "x" "x" "y" "z" "x" "y" "y" "z" "w" "w" "x" "y" "z"

答案 1 :(得分:0)

以下是使用chartr函数进行字符转换的新所需输出的新解决方案:

(from <- paste(letters[1:20], collapse = ""))
# [1] "abcdefghijklmnopqrst"
(to <- paste(rep(c("w", "x", "y", "z"), each = 5), collapse = ""))
# [1] "wwwwwxxxxxyyyyyzzzzz"

# chartr will translate the characters in its first argument
# to the corresponding characters in its second argument
# when it finds instances of those characters in its third argument
df$X1 <- chartr(from, to, df$X2)
#[1] "w" "z" "z" "x" "w" "w" "y" "x" "x" "y" "z" "x" "y" "y" "z" "w" "w" "x" "y" "z"

另一个解决方案是使用命名向量的查找表:

to <- rep(c("w", "x", "y", "z"), each = 5)
names(to) <- paste(letters[1:20])
to 
# a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t 
#"w" "w" "w" "w" "w" "x" "x" "x" "x" "x" "y" "y" "y" "y" "y" "z" "z" "z" "z" "z"

df$X1 <- to[df$X2]
#[1] "w" "z" "z" "x" "w" "w" "y" "x" "x" "y" "z" "x" "y" "y" "z" "w" "w" "x" "y" "z"