I would like to know how to change the smallest non-zero value in group if the count of a condition in the group is 1.
For example, given the data frame:
df1 <- data.frame(x = unlist(map(letters[1:3], function(i) rep(i,4))),
y = rep('var',12),
z = c(c(10,0,'x',40), c(1,2,3,6),c(1,'x','x',6)))
df1
x y z
1 a var 10
2 a var 0
3 a var x
4 a var 40
5 b var 1
6 b var 2
7 b var 3
8 b var 6
9 c var 1
10 c var x
11 c var x
12 c var 6
I would like a[1,3] to change to x as there is only one "x" in the group a from col x, and the 10 is the smallest non-zero value in that group as to obtain the data frame:
x y z
1 a var x
2 a var 0
3 a var x
4 a var 40
5 b var 1
6 b var 2
7 b var 3
8 b var 6
9 c var 1
10 c var x
11 c var x
12 c var 6
Thanks!