Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据嵌套for循环中的条件创建新列_R_For Loop_Nested_Subset_Lapply - Fatal编程技术网

根据嵌套for循环中的条件创建新列

根据嵌套for循环中的条件创建新列,r,for-loop,nested,subset,lapply,R,For Loop,Nested,Subset,Lapply,我正在尝试在我的数据集中创建一个新列,告诉我一个产品的收入是否在所有3个月都有0,在所有3个月都有一些0,或者在所有3个月都没有0 我已经提供了NewColumn作为我想要的结果 data$ZEROES <- 0 data$ZEROES2 <- 0 for (i in unique(data$product_id)){ for (j in unique(data$Revenue)){ n[j] <-ifelse(all(data$Value == 0),

我正在尝试在我的数据集中创建一个新列,告诉我一个产品的收入是否在所有3个月都有0,在所有3个月都有一些0,或者在所有3个月都没有0

我已经提供了
NewColumn
作为我想要的结果

data$ZEROES <- 0
data$ZEROES2 <- 0
for (i in unique(data$product_id)){
    for (j in unique(data$Revenue)){
        n[j] <-ifelse(all(data$Value == 0)," ALL 0", 
        ifelse(any(data$Value == 0),"Some 0", 
        ifelse(all(data$Value != 0), "None 0", "Blank")))
    data$ZEROES[j] <-n[j]
    data$ZEROES2[i] <-long$ZEROES[j]
    }
} 

product_ id Date         Revenue           Value    NewColumn
1           January       in               0           Some 0   
1           February      in               1           Some 0 
1           March         in               0           Some 0 
1           January       out              0           All 0 
1           February      out              0           All 0 
1           March         out              0           All 0 
2           January       in               1           No 0 
2           February      in               2           No 0 
2           March         in               3           No 0 
2           January       out              1           Some 0 
2           February      out              1           Some 0 
2           March         out              0           Some 0 

在base R中,您可以创建一个自定义函数,然后使用
ave
在每组中进行计算:

f <- function(x) if(all(x)) 3 else if(any(x)) 2 else 1
c("None","Some","All")[with(dat, ave(Value==0, list(product_id,Revenue), FUN=f))]
# [1] "Some" "Some" "Some" "All"  "All"  "All"  "None" "None" "None" "Some"
#[11] "Some" "Some"

f在base R中,您可以创建一个自定义函数,然后使用
ave
在每组中进行计算:

f <- function(x) if(all(x)) 3 else if(any(x)) 2 else 1
c("None","Some","All")[with(dat, ave(Value==0, list(product_id,Revenue), FUN=f))]
# [1] "Some" "Some" "Some" "All"  "All"  "All"  "None" "None" "None" "Some"
#[11] "Some" "Some"

f@最近的邮件的解决方案很简洁。以下是两种备选解决方案:

  • 基准R:

    do.call("rbind", by(dat, list(dat$product_id, dat$Revenue), FUN = function(df) {
      within(df, NewColumn <- ifelse(all(Value == 0), "All 0",
                                     ifelse(all(Value != 0), "No 0", "Some 0")))
    }))
    #    product_id     Date Revenue Value NewColumn
    # 1           1  January      in     0    Some 0
    # 2           1 February      in     1    Some 0
    # 3           1    March      in     0    Some 0
    # 7           2  January      in     1      No 0
    # 8           2 February      in     2      No 0
    # 9           2    March      in     3      No 0
    # 4           1  January     out     0     All 0
    # 5           1 February     out     0     All 0
    # 6           1    March     out     0     All 0
    # 10          2  January     out     1    Some 0
    # 11          2 February     out     1    Some 0
    # 12          2    March     out     0    Some 0
    

  • @LateMail的解决方案很简洁。以下是两种备选解决方案:

  • 基准R:

    do.call("rbind", by(dat, list(dat$product_id, dat$Revenue), FUN = function(df) {
      within(df, NewColumn <- ifelse(all(Value == 0), "All 0",
                                     ifelse(all(Value != 0), "No 0", "Some 0")))
    }))
    #    product_id     Date Revenue Value NewColumn
    # 1           1  January      in     0    Some 0
    # 2           1 February      in     1    Some 0
    # 3           1    March      in     0    Some 0
    # 7           2  January      in     1      No 0
    # 8           2 February      in     2      No 0
    # 9           2    March      in     3      No 0
    # 4           1  January     out     0     All 0
    # 5           1 February     out     0     All 0
    # 6           1    March     out     0     All 0
    # 10          2  January     out     1    Some 0
    # 11          2 February     out     1    Some 0
    # 12          2    March     out     0    Some 0
    

  • 使用
    数据表

    library(data.table)
    setDT(df1)[, NewColumn := c("No 0", "Some 0", "All 0")[(all(!Value) + 
                            any(!Value))+1], .(product_id, Revenue)]
    #    product_id     Date Revenue Value NewColumn
    # 1:          1  January      in     0    Some 0
    # 2:          1 February      in     1    Some 0
    # 3:          1    March      in     0    Some 0
    # 4:          1  January     out     0     All 0
    # 5:          1 February     out     0     All 0
    # 6:          1    March     out     0     All 0
    # 7:          2  January      in     1      No 0
    # 8:          2 February      in     2      No 0
    # 9:          2    March      in     3      No 0
    #10:          2  January     out     1    Some 0
    #11:          2 February     out     1    Some 0
    #12:          2    March     out     0    Some 0
    

    使用
    数据表

    library(data.table)
    setDT(df1)[, NewColumn := c("No 0", "Some 0", "All 0")[(all(!Value) + 
                            any(!Value))+1], .(product_id, Revenue)]
    #    product_id     Date Revenue Value NewColumn
    # 1:          1  January      in     0    Some 0
    # 2:          1 February      in     1    Some 0
    # 3:          1    March      in     0    Some 0
    # 4:          1  January     out     0     All 0
    # 5:          1 February     out     0     All 0
    # 6:          1    March     out     0     All 0
    # 7:          2  January      in     1      No 0
    # 8:          2 February      in     2      No 0
    # 9:          2    March      in     3      No 0
    #10:          2  January     out     1    Some 0
    #11:          2 February     out     1    Some 0
    #12:          2    March     out     0    Some 0
    

    上帝禁止你们为其他人添加回答中使用的数据。我通常会尝试将其包括在内(作为OP的提醒),但这次忘记了,我很高兴@flightless13wings注意到了:-)感谢@rawr编辑我的初始问题并添加数据。其中一个答案解决了您的问题吗?如果是这样的话,通常通过勾选答案左边的复选标记来接受答案。上帝禁止你们在回答其他好的电话时添加您在回答中使用的数据,我通常会尝试将其包括在内(作为OP的提醒),但这次忘了,我很高兴@flightless13wings注意到了:-)感谢@rawr编辑了我最初的问题并添加了数据。其中一个答案解决了你的问题吗?如果是这样的话,通常通过勾选答案左边的复选标记来接受答案。谢谢您的帮助。我会用我的真实数据试试。谢谢你的帮助。我会用我的真实数据试试。谢谢!那真的很有帮助。谢谢!这真的很有帮助。