根据R

时间:2019-01-18 11:21:50

标签: r dataframe row

我有一个复杂的问题,答案无处不在。

假设我具有以下数据框:

   individual      gen_check    acc       loss
1  nnn/nn/nn/nn    2            0.9889    0.0112
2  nnn/n/nn        2            0.7845    0.3451
3  nnn/nn/nn/nn    2            0.564     0.4231

我想做的是当我按个人=“ nnn / nn / nn / nn”和gen_check = 2进行过滤时更新第一行的gen_check值,并且我想将gen_check值更新为3。 / p>

我尝试了以下表达式,但它同时修改了第一列和第三列,但我想更新第一列。

fitness_calculations <- within(fitness_calculations, gen_check[individual == "nnn/nn/nn/nn" & gen_check == 2] <- 3)

1 个答案:

答案 0 :(得分:1)

我们创建索引以及条件是否为duplicated

i1 <- with(fitness_calculations, individual == "nnn/nn/nn/nn" & gen_check ==  2)
i2 <- !duplicated(i1) & i1

fitness_calculations$gen_check[i2] <- 3
fitness_calculations
#    individual gen_check    acc   loss
#1 nnn/nn/nn/nn         3 0.9889 0.0112
#2     nnn/n/nn         2 0.7845 0.3451
#3 nnn/nn/nn/nn         2 0.5640 0.4231

或者另一种选择是用which包装并仅提取第一个索引

i2 <- which(i1)[1]
fitness_calculations$gen_check[i2] <- 3

数据

fitness_calculations <- structure(list(individual = c("nnn/nn/nn/nn",
 "nnn/n/nn", "nnn/nn/nn/nn"
 ), gen_check = c(2L, 2L, 2L), acc = c(0.9889, 0.7845, 0.564), 
loss = c(0.0112, 0.3451, 0.4231)), class = "data.frame", row.names = c("1", 
 "2", "3"))