I have a simple dataframe where I have value in log2 scale.
I'm trying to add a column to the dataframe containing R,G,B code proportional to value in the first column. Also if the value is greater or lower than a value, I want to put a treshold. In this case min is -5 max 5. I did it in awk, but I don't find the solution in R My awk code :
awk -v maxlogratio=5 -v FS='\t' -v OFS='\t' '/^chr/{{r=0;g=0;if($9<1){{r=-255*(log($9)/log(2))/maxlogratio;if(r>255){{r=255}}}};if($9>1){{g=255*(log($9)/log(2))/maxlogratio;if(g>255){{g=255}}}};print $0,r","g",0"}}'
The only difference here is that my value where not in log2 scale (it's why I have log(x)/log(2)
R code:
activity_rgb=data.frame(activity=seq(from=-6,to = 6,by = 1))%>%
mutate(rgb=ifelse(activity<0,c(-255*(activity)/5,0,0),c(0,255*(activity),0)))
I got this :
activity rgb
1 -6 306
2 -5 255
3 -4 204
4 -3 153
5 -2 102
6 -1 51
7 0 -255
8 1 0
9 2 255
10 3 510
11 4 765
12 5 1020
13 6 1275
I expect this :
activity rgb
-6 255,0,0
-5 255,0,0
-4 204,0,0
-3 153,0,0
-2 102,0,0
-1 51,0,0
0 0,0,0
1 0,51,0
2 0,102,0
3 0,153,0
4 0,204,0
5 0,255,0
6 0,255,0
So I need to paste value in column rgb, but I don't know how to do it.
So finally I did something to got r,g,b
But I still cannot fix the min and value to 255 for -5 / 5
activity_rgb=data.frame(activity=seq(from=-6,to = 6,by = 1))%>%
mutate(rgb=ifelse(activity<0,paste(-255*(activity)/5,0,0,sep = ","),paste(0,255*(activity)/5,0,sep = ",")))
activity_rgb
activity rgb
1 -6 306,0,0
2 -5 255,0,0
3 -4 204,0,0
4 -3 153,0,0
5 -2 102,0,0
6 -1 51,0,0
7 0 0,0,0
8 1 0,51,0
9 2 0,102,0
10 3 0,153,0
11 4 0,204,0
12 5 0,255,0
13 6 0,306,0