Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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
按位置dplyr仅对某些行进行行和_R_Dplyr - Fatal编程技术网

按位置dplyr仅对某些行进行行和

按位置dplyr仅对某些行进行行和,r,dplyr,R,Dplyr,我有一个数据框,其中的日期将随着某些列名的变化而变化。我想把它作为一个新的列添加到末尾。我需要对每一行中NA的数字进行计数。理想情况下,我希望在dplyr中执行此操作,它最初来自SQL server,因此我使用dbplyr作为数据帧的开始。下面是DF的复制 repro <- structure(list(UPC_Sell_Unit_Desc = c("MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX

我有一个数据框,其中的日期将随着某些列名的变化而变化。我想把它作为一个新的列添加到末尾。我需要对每一行中
NA
的数字进行计数。理想情况下,我希望在
dplyr
中执行此操作,它最初来自SQL server,因此我使用
dbplyr
作为数据帧的开始。下面是DF的复制

repro <- structure(list(UPC_Sell_Unit_Desc = c("MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1"), `Region Manager_desc` = c("Debbie Russell","Debbie Russell", "Debbie Russell", "Debbie Russell", "Debbie Russell","Debbie Russell", "Debbie Russell", "Jim Fiene", "Lou Valdes","Lou Valdes"), Area_desc = c("Area 101", "Area 101", "Area 101","Area 101", "Area 101", "Area 101", "Area 101", "Area 209", "Area 301","Area 301"), Location_ID = c(2358L, 2358L, 2367L, 2367L, 2368L,2368L, 2369L, 5060L, 9002L, 9002L), `2021-05-09` = c(17, 8, 12,4, 5, 9, 8, 5, 7, 4), `2021-05-10` = c(14, 6, 15, 8, 3, 18, 4,7, 10, 15), `2021-05-11` = c(14, 6, 15, 1, NA, 5, 5, 8, 14, 3), `2021-05-12` = c(9, 3, 9, 4, 1, 12, 4, 7, 7, 11), `2021-05-13` = c(10,13, 16, 4, 2, 9, 7, 8, 12, 9), `2021-05-14` = c(26, 8, 16, 3,1, 10, 6, 4, 16, 13), `2021-05-15` = c(15, 8, 12, 3, 2, 3, 11,9, 11, 10)), row.names = c(NA, -10L), groups = structure(list(`Region Manager_desc` = c("Debbie Russell", "Debbie Russell","Debbie Russell", "Debbie Russell", "Debbie Russell", "Debbie Russell","Debbie Russell", "Jim Fiene", "Lou Valdes", "Lou Valdes"), Area_desc = c("Area 101", "Area 101", "Area 101", "Area 101","Area 101", "Area 101", "Area 101", "Area 209", "Area 301","Area 301"), Location_ID = c(2358L, 2358L, 2367L, 2367L,2368L, 2368L, 2369L, 5060L, 9002L, 9002L), UPC_Sell_Unit_Desc = c("MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1", "MARL GOLD KSB/PACK/000002820000384/1","MARL KSF NM BX  KING/PACK/000002820000357/1"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of","vctrs_vctr", "list"))), row.names = c(NA, -10L), class = c("tbl_df","tbl", "data.frame"), .drop = TRUE), class = c("grouped_df","tbl_df", "tbl", "data.frame"))

reproFYI,您的可复制示例是一个分组数据帧,这就是为什么我首先将其传递给
ungroup
。如果它不是分组数据帧,那么您就不需要该管道

library(dplyr)

repro %>% 
  ungroup() %>% 
  mutate(isna = rowSums(is.na(.)))
或者您可以按行使用

repro %>% 
  rowwise() %>% 
  mutate(isna = sum(is.na(c_across()))) %>% 
  ungroup()
这里的
ungroup
是因为
rowwise
输出一种分组数据帧。跨
c_的默认值是跨所有列(例如
everything()
),但您可以使用其他选项作为列子集的参数


rowSums
速度更快,但是
rowwise
允许您使用没有行变量的函数。

FYI,您的可复制示例是分组数据帧,这就是我首先将其传递给
ungroup
的原因。如果它不是分组数据帧,那么您就不需要该管道

library(dplyr)

repro %>% 
  ungroup() %>% 
  mutate(isna = rowSums(is.na(.)))
或者您可以按行使用

repro %>% 
  rowwise() %>% 
  mutate(isna = sum(is.na(c_across()))) %>% 
  ungroup()
这里的
ungroup
是因为
rowwise
输出一种分组数据帧。跨
c_的默认值是跨所有列(例如
everything()
),但您可以使用其他选项作为列子集的参数


rowSums
更快,但是
rowwise
允许您使用没有行变量的函数。

我很确定分组就是我的问题所在。它现在工作得很好。谢谢你的快速回答。我很确定分组就是我的问题所在。它现在工作得很好。谢谢你的快速回答。