R 如何使用来自其他两列的数据和一些决策初始化数据帧中的新列?

R 如何使用来自其他两列的数据和一些决策初始化数据帧中的新列?,r,R,我有一个数据框,我正试图添加一个新列,它是根据我试图在函数中放置的一些简单决定计算出来的 calculateNewValue <- function(a, b) { if(a == b) result <- 4 if(a >= b * 2) result <- 2; if(a > b) result <- 3; if(a < b) result <- 5

我有一个数据框,我正试图添加一个新列,它是根据我试图在函数中放置的一些简单决定计算出来的

calculateNewValue <- function(a, b)
{
    if(a == b)
        result <- 4
    if(a >= b * 2)
        result <- 2;
    if(a > b)
        result <- 3;
    if(a < b)
        result <- 5;
    if(a * 2 <= b)
        result <- 6;
    return(result);    
}
data.set$newCol <- calculateNewValue(data.set$colA, data.set$colB);
根据我的功能,我希望在newCol中看到的结果是:

4
3
5
6
然而,我实际得到的结果是:

4
4
4
4

我这里缺少什么?

我看不出你的功能有什么问题。您需要以迭代的方式将其应用于数据帧

使用
Map
mappy
功能,您可以执行以下操作:

# using Map function
df$newCol <- unlist(Map(calculateNewValue, df$colA, df$colB))
print(df)

  Name colA colB newCol
1   S1    4    4      4
2   S2    4    3      3
3   S3    4    5      5
4   S4    4    8      6

# another one using mapply
df$newCol <- mapply(calculateNewValue, df$colA, df$colB)
#使用Map函数

df$newCol我看你的功能没有问题。您需要以迭代的方式将其应用于数据帧

使用
Map
mappy
功能,您可以执行以下操作:

# using Map function
df$newCol <- unlist(Map(calculateNewValue, df$colA, df$colB))
print(df)

  Name colA colB newCol
1   S1    4    4      4
2   S2    4    3      3
3   S3    4    5      5
4   S4    4    8      6

# another one using mapply
df$newCol <- mapply(calculateNewValue, df$colA, df$colB)
#使用Map函数

df$newCol如果尝试使用向量运行函数,则会发现问题。按照您编写ir的方式,它只将一个元素与另一个元素进行比较

calculateNewValue(c(4,4), c(4,3))
[1] 4
Warning messages:
1: In if (a == b) result <- 4 :
  the condition has length > 1 and only the first element will be used
2: In if (a >= b * 2) result <- 2 :
  the condition has length > 1 and only the first element will be used
3: In if (a > b) result <- 3 :
  the condition has length > 1 and only the first element will be used
4: In if (a < b) result <- 5 :
  the condition has length > 1 and only the first element will be used
5: In if (a * 2 <= b) result <- 6 :
  the condition has length > 1 and only the first element will be used
calculateNewValue(c(4,4),c(4,3))
[1] 4
警告信息:
1:在if(a==b)结果1中,只使用第一个元素
2:在if(a>=b*2)结果1中,仅使用第一个元素
3:在if(a>b)结果1中,仅使用第一个元素
4:在if(a5:在if(a*2中,如果您尝试使用向量运行函数,则会发现问题。按照您编写ir的方式,它只会将一个元素与另一个元素进行比较

calculateNewValue(c(4,4), c(4,3))
[1] 4
Warning messages:
1: In if (a == b) result <- 4 :
  the condition has length > 1 and only the first element will be used
2: In if (a >= b * 2) result <- 2 :
  the condition has length > 1 and only the first element will be used
3: In if (a > b) result <- 3 :
  the condition has length > 1 and only the first element will be used
4: In if (a < b) result <- 5 :
  the condition has length > 1 and only the first element will be used
5: In if (a * 2 <= b) result <- 6 :
  the condition has length > 1 and only the first element will be used
calculateNewValue(c(4,4),c(4,3))
[1] 4
警告信息:
1:在if(a==b)结果1中,只使用第一个元素
2:在if(a>=b*2)结果1中,仅使用第一个元素
3:在if(a>b)结果1中,仅使用第一个元素
4:在if(a5:如果(a*2
ifelse
是矢量化的:

calculateNewValue <- function(a, b)
{
    ifelse(a == b, 4,
      ifelse(a >= b * 2, 2,
        ifelse(a > b, 3,
          ifelse(a * 2 < b, 6,
            ifelse(a < b, 5)))))
}    

# now this should work fine:
data.set$newCol <- calculateNewValue(data.set$colA, data.set$colB)
calculateNewValue=b*2,2,
如果是(a>b,3,
如果是(a*2数据集$newCol
ifelse
已矢量化:

calculateNewValue <- function(a, b)
{
    ifelse(a == b, 4,
      ifelse(a >= b * 2, 2,
        ifelse(a > b, 3,
          ifelse(a * 2 < b, 6,
            ifelse(a < b, 5)))))
}    

# now this should work fine:
data.set$newCol <- calculateNewValue(data.set$colA, data.set$colB)
calculateNewValue=b*2,2,
如果是(a>b,3,
如果是(a*2data.set$newCol仅供参考,
ifelse()
是矢量化的,您可以执行
矢量化(calculateNewValue)(data.set$colA,data.set$colB)
函数的编写方式,您需要一些
其他
。当
b
为正时,您的
if(a>=b*2)
结果将被
if(a>b)
结果覆盖。您可以通过将第一个
if
更改为
else if
,或者通过更改
结果FYI来修复,
ifelse()
是向量化的。您可以执行
向量化(calculateNewValue)(data.set$colA,data.set$colB)
函数的编写方式,您需要一些
else
。当
b
为正时,您的
if(a>=b*2)
结果将被
if(a>b)
结果覆盖。您可以通过将第一个
if
更改为
else if
,或者更改
结果来修复,非常感谢您如此简单地为我将其分解!完全按照您指定的方式使用mapply行,我的窍门是
unlist(Map…
mapply
相同,非常感谢您为我如此简单地将其分解!完全按照您指定的方式使用mappy行,对我来说
unlist(Map…
的技巧与
mappy