简体   繁体   English

根据 R 中其他向量的条件,将值分配给 dataframe 中的向量

[英]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.

相关问题 根据 R 中两个其他向量中的匹配值在一个向量中分配值 - Assign values in one vector based on matching values in two other vectors in R 根据其他向量的值,概率在 R 中生成随机向量 - generate random vector in R based on values of other vectors, probability 从向量中选取一个数字并根据R中的多个条件分配给列 - Picking a number from vector and assign to column based on multiple conditions in R 根据其他向量有条件地生成具有值的向量 - Generating a vector with values based conditionally from other vectors R:基于不同长度的另一数据帧中的条件,将值分配给矢量集 - R: assign values to set of vectors based on conditions in another data frame of different length 根据文件名向R中的数据帧分配ID向量? - Assign an ID vector to a dataframe in R, based on filename? R 根据多个条件分配值 - R Assign Values based on multiple conditions 从 dataframe 创建一个向量,该向量根据 r 中的另一列区分值 - Creating a Vector from a dataframe that discriminates values based on another column in r 如何根据另一个向量中的值删除 R 中数据帧中的列? - How to remove columns in a dataframe in R based on the values from another vector? R:从具有 x 和 y 索引向量的矩阵或数据框中选择值向量 - R: Selecting a vector of values from a matrix or dataframe with vectors of x and y indices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM