I am trying to run a simple multiplication of data. The third column (ID) of the same data based on the value of scalar B, along with the scalar of the frame column, respectively. Frame Somehow I have some (order, sort?) Problem - so far the result is definitely wrong: here are several attempts:
mydf $ result = subset (mydf, myid == 123, multiplyme ) * 0.6+ subset (mydf, myid == 124, multiplyme) * 0.4 I also tried to use% syntax% but it also did not succeed. I know that I can connect to MySQL and R for example, but in this case I want to use at least (basic) r or plywood here. Just for those who prefer to code on my Blala, here is how I do it in SQL:
SELECT MIN (When case = 123 THEN multiplyme * 0.6 END) MIN ( Case when MYID = 124 THN multiply * 0.4 END) from the result of mytable group; Thx for any help / R code suggestions in advance! Please note that I have more than 2 IDs!
Assume that you only have Myid in 123 or 124:
mydf $ result <- mydf $ multiplyme * ifelse (mydf $ myid == 123,0.6,0.4) If you have myid an additional ifelse and a default case.
Edit:
Since you have an additional variable in myid , I will describe the detail.
mydf $ result <- mydf $ multiplyme * ifelse (mydf $ myid == 123,0.6, ifelse (mydf $ myid == 124,0.4,0)) If you want to change 0 to the end of 1 if you want to change the value of multiply if you want to use a different multi for multiple values then The statement can be extended in a series of ifelse statement.
However, in the form of an MBQ comment below, if you start having the switch statement if it is unwieldy:
mydf $ result <- mydf $ Multiplyme * sapply (mydf $ myid, switch (x) switch (as.character (x), "123" = 0.6, "124" = 0.4)) This will probably slow down, Because it will loop while ifelse is vectorised. / P>
Comments
Post a Comment