在data.frame中设置值,以引用data.frame的不同列中的列名称

时间:2015-10-15 09:06:51

标签: r

描述我的需求非常困难,所以我将提出一个视觉示例。

我有一个类似于:

的data.frame
foo <- data.frame(
id=c("blah", "foo", "bar"), 
minplayers=c(2,3,1), 
maxplayers=c(4,4,3), 
players1.cash=c(10,20,10), 
players2.cash=c(0,10,10),
players3.cash=c(1,0,0),
players4.cash=c(8,8,9)
)

给出以下内容:

+-----+-----+-----+---------------+---------------+---------------+---------------+
|id   | min | max | players1.cash | players2.cash | players3.cash | players4.cash |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|blah |  2  |  4  |     10        |      0        |       1       |       8       |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|foo  |  3  |  4  |     20        |      10       |       0       |       8       |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|bar  |  1  |  3  |     10        |      10       |       0       |       9       |
+-----+-----+-----+---------------+---------------+---------------+---------------+

如果n {min,max}间隔(对于该行),我需要将每行的所有玩家 n .cash值设置为NA 。在图片中:

+-----+-----+-----+---------------+---------------+---------------+---------------+
|id   | min | max | players1.cash | players2.cash | players3.cash | players4.cash |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|blah |  2  |  4  |     NA        |      0        |       1       |       8       |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|foo  |  3  |  4  |     NA        |      NA       |       0       |       8       |
+-----+-----+-----+---------------+---------------+---------------+---------------+
|bar  |  1  |  3  |     10        |      10       |       0       |       NA      |
+-----+-----+-----+---------------+---------------+---------------+---------------+

换句话说:我只希望玩家编号(1到4)在每行的最小/最大边界内。如果是这样,我想保留玩家价值,如果没有,就把NA。

1 个答案:

答案 0 :(得分:3)

您可以使用vectorized方法:

ix = grep("^players(\\d+)\\.cash", names(foo))
numbers = as.numeric(gsub("^players(\\d+)\\.cash", "\\1", names(foo)[ix]))

m = matrix(numbers, ncol=length(ix), nrow=nrow(foo), byrow=T)
foo[ix][!(m>=foo$minplayers & m<=foo$maxplayers)] <-NA

#> foo
#    id minplayers maxplayers players1.cash players2.cash players3.cash players4.cash
#1 blah          2          4            NA             0             1             8
#2  foo          3          4            NA            NA             0             8
#3  bar          1          3            10            10             0            NA