[英]assign values to vector in a dataframe based on conditions from other vectors in R
so I have a partially empty dataframe and I need to assign values to a vector that we may call "C" based on different outcomes of an other vector which we may call B. The values to assign to C are taken, in some determined cases, from a third vector, "A".所以我有一个部分为空的 dataframe 并且我需要根据我们可能称为 B 的其他向量的不同结果为我们可能称为“C”的向量分配值。在某些确定的情况下,会采用分配给 C 的值,来自第三个向量“A”。 How can I do that?我怎样才能做到这一点? I tried if statements and for loops but don't know how to do them properly.我尝试了 if 语句和 for 循环,但不知道如何正确执行它们。 Here's summarized my problem.这里总结了我的问题。 Thank you for your answers谢谢您的回答
A = (1,2,3,4,5,6,7,8,9,10)
B = (1,0,1,2,1,0,0,2,0,1)
C = 0 if B = 0
=A(same row) if B = 1
=-A(same row) if B =-1
An option is to get the sign
of 'B' and multiply with 'A'一种选择是获得“B”的sign
并与“A”相乘
C <- sign(B) * A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.