So I have this data frame of a condition
column and a value
column. Suppose I want to change one of the values for which the condition is satisfied, but not all of the values for which the condition is satisfied. The idea here is that I would like to change the values one by one (or select one randomly).
df <- data.frame(condition = c(TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE),
value = 8:1)
I would like to store this in a new column. Suppose I randomly select row 2: The condition is TRUE and the original value is 7. I change it to 0 (or some other value). The desired output in this case is:
> df
condition value
1 TRUE 8
2 TRUE 7
3 TRUE 6
4 TRUE 5
5 FALSE 4
6 FALSE 3
7 FALSE 2
8 FALSE 1
I suppose I could do this with an if statement, a third column of group-row-numbers and the sample function, but is there a nicer way? In particular, I need to make sure I do not generate a new column in case the condition is not satisfied for any of the values. Otherwise I am wasting a lot of computing time later on in the analysis. I usually do things inside the tidyverse if I can, but I do not mind if there is a nice solution without using the package (or using a different package). Thanks!
Aucun commentaire:
Enregistrer un commentaire